|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System PHP Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Cli\Commanders\Make; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Reactor\Cli\Commanders\Make; |
|
19
|
|
|
use O2System\Kernel\Cli\Writers\Format; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Model |
|
23
|
|
|
* |
|
24
|
|
|
* @package O2System\Reactor\Cli\Commanders\Make |
|
25
|
|
|
*/ |
|
26
|
|
|
class Model extends Make |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Model::$commandDescription |
|
30
|
|
|
* |
|
31
|
|
|
* Command description. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $commandDescription = 'CLI_MAKE_MODEL_DESC'; |
|
36
|
|
|
|
|
37
|
|
|
// ------------------------------------------------------------------------ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Model::optionOrm |
|
41
|
|
|
* |
|
42
|
|
|
* @param bool $useOrm |
|
43
|
|
|
*/ |
|
44
|
|
|
public function optionOrm($useOrm = true) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->isUseORM = $useOrm; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// ------------------------------------------------------------------------ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Model::execute |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \ReflectionException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function execute() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->__callOptions(); |
|
59
|
|
|
|
|
60
|
|
|
if (empty($this->optionFilename)) { |
|
61
|
|
|
output()->write( |
|
|
|
|
|
|
62
|
|
|
(new Format()) |
|
63
|
|
|
->setContextualClass(Format::DANGER) |
|
64
|
|
|
->setString(language()->getLine('CLI_MAKE_MODEL_E_FILENAME')) |
|
65
|
|
|
->setNewLinesAfter(1) |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (strpos($this->optionPath, 'Models') === false) { |
|
72
|
|
|
$filePath = $this->optionPath . 'Models' . DIRECTORY_SEPARATOR . $this->optionFilename; |
|
73
|
|
|
} else { |
|
74
|
|
|
$filePath = $this->optionPath . $this->optionFilename; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ( ! is_dir(dirname($filePath))) { |
|
78
|
|
|
mkdir(dirname($filePath), 0777, true); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (is_file($filePath)) { |
|
82
|
|
|
output()->write( |
|
83
|
|
|
(new Format()) |
|
84
|
|
|
->setContextualClass(Format::DANGER) |
|
85
|
|
|
->setString(language()->getLine('CLI_MAKE_MODEL_E_EXISTS', [$filePath])) |
|
86
|
|
|
->setNewLinesAfter(1) |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$className = prepare_class_name(pathinfo($filePath, PATHINFO_FILENAME)); |
|
93
|
|
|
@list($namespaceDirectory, $subNamespace) = explode('Models', dirname($filePath)); |
|
94
|
|
|
|
|
95
|
|
|
$classNamespace = loader()->getDirNamespace( |
|
96
|
|
|
$namespaceDirectory |
|
97
|
|
|
) . 'Models' . (empty($subNamespace) |
|
98
|
|
|
? null |
|
99
|
|
|
: str_replace( |
|
100
|
|
|
'/', |
|
101
|
|
|
'\\', |
|
102
|
|
|
$subNamespace |
|
103
|
|
|
)) . '\\'; |
|
104
|
|
|
|
|
105
|
|
|
$isUseORM = empty($this->isUseORM) ? false : true; |
|
106
|
|
|
|
|
107
|
|
|
$vars[ 'CREATE_DATETIME' ] = date('d/m/Y H:m'); |
|
|
|
|
|
|
108
|
|
|
$vars[ 'NAMESPACE' ] = trim($classNamespace, '\\'); |
|
109
|
|
|
$vars[ 'PACKAGE' ] = '\\' . trim($classNamespace, '\\'); |
|
110
|
|
|
$vars[ 'CLASS' ] = $className; |
|
111
|
|
|
$vars[ 'FILEPATH' ] = $filePath; |
|
112
|
|
|
$vars[ 'EXTEND' ] = $isUseORM |
|
113
|
|
|
? 'Orm' |
|
114
|
|
|
: 'Reactor'; |
|
115
|
|
|
|
|
116
|
|
|
$phpTemplate = <<<PHPTEMPLATE |
|
117
|
|
|
<?php |
|
118
|
|
|
/** |
|
119
|
|
|
* Created by O2System Reactor File Generator. |
|
120
|
|
|
* DateTime: CREATE_DATETIME |
|
121
|
|
|
*/ |
|
122
|
|
|
|
|
123
|
|
|
// ------------------------------------------------------------------------ |
|
124
|
|
|
|
|
125
|
|
|
namespace NAMESPACE; |
|
126
|
|
|
|
|
127
|
|
|
// ------------------------------------------------------------------------ |
|
128
|
|
|
|
|
129
|
|
|
use O2System\Reactor\Models\Sql\Model; |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Class CLASS |
|
133
|
|
|
* |
|
134
|
|
|
* @package PACKAGE |
|
135
|
|
|
*/ |
|
136
|
|
|
class CLASS extends Model |
|
137
|
|
|
{ |
|
138
|
|
|
public \$table = 'table_name'; |
|
139
|
|
|
} |
|
140
|
|
|
PHPTEMPLATE; |
|
141
|
|
|
|
|
142
|
|
|
$fileContent = str_replace(array_keys($vars), array_values($vars), $phpTemplate); |
|
143
|
|
|
file_put_contents($filePath, $fileContent); |
|
144
|
|
|
|
|
145
|
|
|
if (is_file($filePath)) { |
|
146
|
|
|
output()->write( |
|
147
|
|
|
(new Format()) |
|
148
|
|
|
->setContextualClass(Format::SUCCESS) |
|
149
|
|
|
->setString(language()->getLine('CLI_MAKE_MODEL_S_MAKE', [$filePath])) |
|
150
|
|
|
->setNewLinesAfter(1) |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
} |