Skip to content

Commit 0d82ada

Browse files
committed
Cli command bugs fixes
1 parent 9f38c25 commit 0d82ada

29 files changed

+414
-509
lines changed

src/Cli/Commanders/Make.php

+28-16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// ------------------------------------------------------------------------
1717

1818
use O2System\Framework\Cli\Commander;
19+
use O2System\Spl\Traits\Collectors\FilePathCollectorTrait;
1920

2021
/**
2122
* Class Make
@@ -24,6 +25,8 @@
2425
*/
2526
class Make extends Commander
2627
{
28+
use FilePathCollectorTrait;
29+
2730
/**
2831
* Make::$commandVersion
2932
*
@@ -76,7 +79,7 @@ class Make extends Commander
7679
*
7780
* @var string
7881
*/
79-
protected $optionPath;
82+
protected $optionPath = PATH_APP;
8083

8184
/**
8285
* Make::$filename
@@ -89,6 +92,22 @@ class Make extends Commander
8992

9093
// ------------------------------------------------------------------------
9194

95+
/**
96+
* Make::__reconstruct
97+
*/
98+
public function __construct()
99+
{
100+
parent::__construct();
101+
102+
$this->setFileDirName('PhpTemplateFiles');
103+
$this->addFilePaths([
104+
PATH_FRAMEWORK . 'Config' . DIRECTORY_SEPARATOR,
105+
PATH_APP . 'Config' . DIRECTORY_SEPARATOR,
106+
]);
107+
}
108+
109+
// ------------------------------------------------------------------------
110+
92111
/**
93112
* Make::optionPath
94113
*
@@ -97,14 +116,16 @@ class Make extends Commander
97116
public function optionPath($path)
98117
{
99118
$path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
100-
$path = PATH_ROOT . str_replace(PATH_ROOT, '', $path);
119+
$path = PATH_APP . str_replace([PATH_APP, PATH_ROOT], '', $path);
101120

102121
if (pathinfo($path, PATHINFO_EXTENSION)) {
103122
$this->optionFilename(pathinfo($path, PATHINFO_FILENAME));
104123
$path = dirname($path);
105124
}
106125

107126
$this->optionPath = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
127+
128+
return $this;
108129
}
109130

110131
// ------------------------------------------------------------------------
@@ -120,6 +141,8 @@ public function optionFilename($name)
120141
$this->optionFilename = prepare_filename($name) . '.php';
121142

122143
$this->optionPath = empty($this->optionPath) ? modules()->top()->getRealPath() : $this->optionPath;
144+
145+
return $this;
123146
}
124147

125148
// ------------------------------------------------------------------------
@@ -132,6 +155,8 @@ public function optionFilename($name)
132155
public function optionName($name)
133156
{
134157
$this->optionFilename($name);
158+
159+
return $this;
135160
}
136161

137162
// ------------------------------------------------------------------------
@@ -144,20 +169,7 @@ public function optionName($name)
144169
public function optionNamespace($namespace)
145170
{
146171
$this->namespace = $namespace;
147-
}
148-
149-
// ------------------------------------------------------------------------
150-
151-
/**
152-
* Make::getPhpTemplateFile
153-
*
154-
* @param string $filename
155-
*/
156-
public function getPhpTemplateFile($filename)
157-
{
158-
$directories = [
159-
PATH_FRAMEWORK . 'Config' . DIRECTORY_SEPARATOR . 'PhpTemplateFiles',
160-
];
161172

173+
return $this;
162174
}
163175
}

src/Cli/Commanders/Make/App.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function execute()
8181

8282
if ( ! is_dir($modulePath)) {
8383
mkdir($modulePath, 0777, true);
84+
85+
// Make default structure
86+
foreach (['Config', 'Controllers', 'Helpers', 'Http','Languages', 'Models', 'Presenters'] as $defaultDir) {
87+
mkdir($modulePath . $defaultDir . DIRECTORY_SEPARATOR, 0777, true);
88+
}
8489
} else {
8590
output()->write(
8691
(new Format())
@@ -121,8 +126,10 @@ public function execute()
121126
$this->optionFilename = prepare_filename($this->optionName) . '.php';
122127

123128
(new Controller())
124-
->optionPath($this->optionPath)
125-
->optionFilename($this->optionFilename);
129+
->isHttp()
130+
->optionPath($this->optionPath . 'Http' . DIRECTORY_SEPARATOR)
131+
->optionFilename($this->optionFilename)
132+
->execute();
126133

127134
if (is_dir($modulePath)) {
128135
output()->write(

src/Cli/Commanders/Make/Commander.php

+11-36
Original file line numberDiff line numberDiff line change
@@ -78,55 +78,30 @@ public function execute()
7878
}
7979

8080
$className = prepare_class_name(pathinfo($filePath, PATHINFO_FILENAME));
81-
@list($namespaceDirectory, $subNamespace) = explode('Commanders', dirname($filePath));
81+
@list($namespaceDirectory, $subNamespace) = explode('Commanders', str_replace(['\\','/'], '\\', dirname($filePath)));
82+
$subNamespace = rtrim($subNamespace, '\\');
8283

8384
$classNamespace = loader()->getDirNamespace(
8485
$namespaceDirectory
8586
) . 'Commanders' . (empty($subNamespace)
8687
? null
87-
: str_replace(
88-
'/',
89-
'\\',
90-
$subNamespace
91-
)) . '\\';
88+
: $subNamespace) . '\\';
9289

9390
$vars[ 'CREATE_DATETIME' ] = date('d/m/Y H:m');
9491
$vars[ 'NAMESPACE' ] = trim($classNamespace, '\\');
9592
$vars[ 'PACKAGE' ] = '\\' . trim($classNamespace, '\\');
9693
$vars[ 'CLASS' ] = $className;
9794
$vars[ 'FILEPATH' ] = $filePath;
9895

99-
$phpTemplate = <<<PHPTEMPLATE
100-
<?php
101-
/**
102-
* Created by O2System Framework File Generator.
103-
* DateTime: CREATE_DATETIME
104-
*/
105-
106-
// ------------------------------------------------------------------------
107-
108-
namespace NAMESPACE;
109-
110-
// ------------------------------------------------------------------------
111-
112-
use O2System\Framework\Http\Commander;
96+
$phpTemplateFilePaths = $this->getFilePaths(true);
11397

114-
/**
115-
* Class CLASS
116-
*
117-
* @package PACKAGE
118-
*/
119-
class CLASS extends Commander
120-
{
121-
/**
122-
* CLASS::index
123-
*/
124-
public function index()
125-
{
126-
// TODO: Change the autogenerated stub
127-
}
128-
}
129-
PHPTEMPLATE;
98+
foreach($phpTemplateFilePaths as $phpTemplateFilePath)
99+
{
100+
if(is_file($phpTemplateFilePath . 'Commander.tpl')) {
101+
$phpTemplate = file_get_contents($phpTemplateFilePath . 'Commander.tpl');
102+
break;
103+
}
104+
}
130105

131106
$fileContent = str_replace(array_keys($vars), array_values($vars), $phpTemplate);
132107
file_put_contents($filePath, $fileContent);

src/Cli/Commanders/Make/Config.php

+9-11
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,15 @@ public function execute()
8181
$vars[ 'CONFIG' ] = '$' . camelcase(pathinfo($filePath, PATHINFO_FILENAME));
8282
$vars[ 'FILEPATH' ] = $filePath;
8383

84-
$phpTemplate = <<<PHPTEMPLATE
85-
<?php
86-
/**
87-
* Created by O2System Framework File Generator.
88-
* DateTime: CREATE_DATETIME
89-
*/
90-
91-
// ------------------------------------------------------------------------
92-
93-
CONFIG = [];
94-
PHPTEMPLATE;
84+
$phpTemplateFilePaths = $this->getFilePaths(true);
85+
86+
foreach($phpTemplateFilePaths as $phpTemplateFilePath)
87+
{
88+
if(is_file($phpTemplateFilePath . 'Config.tpl')) {
89+
$phpTemplate = file_get_contents($phpTemplateFilePath . 'Config.tpl');
90+
break;
91+
}
92+
}
9593

9694
$fileContent = str_replace(array_keys($vars), array_values($vars), $phpTemplate);
9795
file_put_contents($filePath, $fileContent);

src/Cli/Commanders/Make/Controller.php

+50-41
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
*/
2626
class Controller extends Make
2727
{
28+
/**
29+
* Controller::$reconstruct
30+
*
31+
* @var bool
32+
*/
33+
protected $http = false;
34+
2835
/**
2936
* Controller::$commandDescription
3037
*
@@ -36,6 +43,20 @@ class Controller extends Make
3643

3744
// ------------------------------------------------------------------------
3845

46+
/**
47+
* Controller::addReconstruct
48+
*
49+
* @return static
50+
*/
51+
public function isHttp()
52+
{
53+
$this->http = true;
54+
55+
return $this;
56+
}
57+
58+
// ------------------------------------------------------------------------
59+
3960
/**
4061
* Controller::execute
4162
*
@@ -56,8 +77,21 @@ public function execute()
5677
exit(EXIT_ERROR);
5778
}
5879

59-
if (strpos($this->optionPath, 'Controllers') === false) {
60-
$filePath = $this->optionPath . 'Controllers' . DIRECTORY_SEPARATOR . $this->optionFilename;
80+
$controllerDir = 'Controllers';
81+
$controllerTemplateFile = 'Controller.tpl';
82+
$messageSuccess = 'CLI_MAKE_CONTROLLER_S_MAKE';
83+
$messageError = 'CLI_MAKE_CONTROLLER_E_MAKE';
84+
85+
if($this->http) {
86+
$controllerDir = 'Http';
87+
$controllerTemplateFile = 'HttpController.tpl';
88+
$messageSuccess = 'CLI_MAKE_HTTP_CONTROLLER_S_MAKE';
89+
$messageError = 'CLI_MAKE_HTTP_CONTROLLER_S_MAKE';
90+
$this->optionFilename = 'Controller.php';
91+
}
92+
93+
if (strpos($this->optionPath, $controllerDir) === false) {
94+
$filePath = $this->optionPath . $controllerDir . DIRECTORY_SEPARATOR . $this->optionFilename;
6195
} else {
6296
$filePath = $this->optionPath . $this->optionFilename;
6397
}
@@ -72,63 +106,38 @@ public function execute()
72106
output()->write(
73107
(new Format())
74108
->setContextualClass(Format::DANGER)
75-
->setString(language()->getLine('CLI_MAKE_CONTROLLER_E_EXISTS', [$filePath]))
109+
->setString(language()->getLine($messageError, [$filePath]))
76110
->setNewLinesAfter(1)
77111
);
78112

79113
exit(EXIT_ERROR);
80114
}
81115

82116
$className = prepare_class_name(pathinfo($filePath, PATHINFO_FILENAME));
83-
@list($namespaceDirectory, $subNamespace) = explode('Controllers', dirname($filePath));
117+
@list($namespaceDirectory, $subNamespace) = explode($controllerDir, str_replace(['\\','/'], '\\', dirname($filePath)));
118+
$subNamespace = rtrim($subNamespace, '\\');
84119

85120
$classNamespace = loader()->getDirNamespace(
86121
$namespaceDirectory
87-
) . 'Controllers' . (empty($subNamespace)
122+
) . $controllerDir . (empty($subNamespace)
88123
? null
89-
: str_replace(
90-
'/',
91-
'\\',
92-
$subNamespace
93-
)) . '\\';
124+
: $subNamespace) . '\\';
94125

95126
$vars[ 'CREATE_DATETIME' ] = date('d/m/Y H:m');
96127
$vars[ 'NAMESPACE' ] = trim($classNamespace, '\\');
97128
$vars[ 'PACKAGE' ] = '\\' . trim($classNamespace, '\\');
98129
$vars[ 'CLASS' ] = $className;
99130
$vars[ 'FILEPATH' ] = $filePath;
100131

101-
$phpTemplate = <<<PHPTEMPLATE
102-
<?php
103-
/**
104-
* Created by O2System Framework File Generator.
105-
* DateTime: CREATE_DATETIME
106-
*/
107-
108-
// ------------------------------------------------------------------------
109-
110-
namespace NAMESPACE;
132+
$phpTemplateFilePaths = $this->getFilePaths(true);
111133

112-
// ------------------------------------------------------------------------
113-
114-
use O2System\Framework\Http\Controller;
115-
116-
/**
117-
* Class CLASS
118-
*
119-
* @package PACKAGE
120-
*/
121-
class CLASS extends Controller
122-
{
123-
/**
124-
* CLASS::index
125-
*/
126-
public function index()
127-
{
128-
// TODO: Change the autogenerated stub
129-
}
130-
}
131-
PHPTEMPLATE;
134+
foreach($phpTemplateFilePaths as $phpTemplateFilePath)
135+
{
136+
if(is_file($phpTemplateFilePath .$controllerTemplateFile)) {
137+
$phpTemplate = file_get_contents($phpTemplateFilePath . $controllerTemplateFile);
138+
break;
139+
}
140+
}
132141

133142
$fileContent = str_replace(array_keys($vars), array_values($vars), $phpTemplate);
134143
file_put_contents($filePath, $fileContent);
@@ -137,7 +146,7 @@ public function index()
137146
output()->write(
138147
(new Format())
139148
->setContextualClass(Format::SUCCESS)
140-
->setString(language()->getLine('CLI_MAKE_CONTROLLER_S_MAKE', [$filePath]))
149+
->setString(language()->getLine($messageSuccess, [$filePath]))
141150
->setNewLinesAfter(1)
142151
);
143152

0 commit comments

Comments
 (0)