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
|
|
|
public function optionOrm($useOrm = true) |
38
|
|
|
{ |
39
|
|
|
$this->isUseORM = $useOrm; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function execute() |
43
|
|
|
{ |
44
|
|
|
parent::execute(); |
45
|
|
|
|
46
|
|
|
if (empty($this->optionFilename)) { |
47
|
|
|
output()->write( |
|
|
|
|
48
|
|
|
(new Format()) |
49
|
|
|
->setContextualClass(Format::DANGER) |
50
|
|
|
->setString(language()->getLine('CLI_MAKE_MODEL_E_FILENAME')) |
51
|
|
|
->setNewLinesAfter(1) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (strpos($this->optionPath, 'Models') === false) { |
58
|
|
|
$filePath = $this->optionPath . 'Models' . DIRECTORY_SEPARATOR . $this->optionFilename; |
59
|
|
|
} else { |
60
|
|
|
$filePath = $this->optionPath . $this->optionFilename; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ( ! is_dir(dirname($filePath))) { |
64
|
|
|
mkdir(dirname($filePath), 0777, true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (is_file($filePath)) { |
68
|
|
|
output()->write( |
69
|
|
|
(new Format()) |
70
|
|
|
->setContextualClass(Format::DANGER) |
71
|
|
|
->setString(language()->getLine('CLI_MAKE_MODEL_E_EXISTS', [$filePath])) |
72
|
|
|
->setNewLinesAfter(1) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$className = prepare_class_name(pathinfo($filePath, PATHINFO_FILENAME)); |
79
|
|
|
@list($namespaceDirectory, $subNamespace) = explode('Models', dirname($filePath)); |
80
|
|
|
|
81
|
|
|
$classNamespace = loader()->getDirNamespace( |
82
|
|
|
$namespaceDirectory |
83
|
|
|
) . 'Models' . (empty($subNamespace) |
84
|
|
|
? null |
85
|
|
|
: str_replace( |
86
|
|
|
'/', |
87
|
|
|
'\\', |
88
|
|
|
$subNamespace |
89
|
|
|
)) . '\\'; |
90
|
|
|
|
91
|
|
|
$isUseORM = empty($this->isUseORM) ? false : true; |
92
|
|
|
|
93
|
|
|
$vars[ 'CREATE_DATETIME' ] = date('d/m/Y H:m'); |
|
|
|
|
94
|
|
|
$vars[ 'NAMESPACE' ] = trim($classNamespace, '\\'); |
95
|
|
|
$vars[ 'PACKAGE' ] = '\\' . trim($classNamespace, '\\'); |
96
|
|
|
$vars[ 'CLASS' ] = $className; |
97
|
|
|
$vars[ 'FILEPATH' ] = $filePath; |
98
|
|
|
$vars[ 'EXTEND' ] = $isUseORM |
99
|
|
|
? 'Orm' |
100
|
|
|
: 'Reactor'; |
101
|
|
|
|
102
|
|
|
$phpTemplate = <<<PHPTEMPLATE |
103
|
|
|
<?php |
104
|
|
|
/** |
105
|
|
|
* Created by O2System Reactor File Generator. |
106
|
|
|
* DateTime: CREATE_DATETIME |
107
|
|
|
*/ |
108
|
|
|
|
109
|
|
|
// ------------------------------------------------------------------------ |
110
|
|
|
|
111
|
|
|
namespace NAMESPACE; |
112
|
|
|
|
113
|
|
|
// ------------------------------------------------------------------------ |
114
|
|
|
|
115
|
|
|
use O2System\Reactor\Models\Sql\Model; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Class CLASS |
119
|
|
|
* |
120
|
|
|
* @package PACKAGE |
121
|
|
|
*/ |
122
|
|
|
class CLASS extends Model |
123
|
|
|
{ |
124
|
|
|
public \$table = 'table_name'; |
125
|
|
|
} |
126
|
|
|
PHPTEMPLATE; |
127
|
|
|
|
128
|
|
|
$fileContent = str_replace(array_keys($vars), array_values($vars), $phpTemplate); |
129
|
|
|
file_put_contents($filePath, $fileContent); |
130
|
|
|
|
131
|
|
|
if (is_file($filePath)) { |
132
|
|
|
output()->write( |
133
|
|
|
(new Format()) |
134
|
|
|
->setContextualClass(Format::SUCCESS) |
135
|
|
|
->setString(language()->getLine('CLI_MAKE_MODEL_S_MAKE', [$filePath])) |
136
|
|
|
->setNewLinesAfter(1) |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |