|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @file MakeCommand.php |
|
34
|
|
|
* |
|
35
|
|
|
* The Make Command base class |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Framework\Console |
|
38
|
|
|
* @author Platine Developers team |
|
39
|
|
|
* @copyright Copyright (c) 2020 |
|
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
41
|
|
|
* @link http://www.iacademy.cf |
|
42
|
|
|
* @version 1.0.0 |
|
43
|
|
|
* @filesource |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
declare(strict_types=1); |
|
47
|
|
|
|
|
48
|
|
|
namespace Platine\Framework\Console; |
|
49
|
|
|
|
|
50
|
|
|
use Platine\Console\Command\Command; |
|
51
|
|
|
use Platine\Filesystem\Filesystem; |
|
52
|
|
|
use Platine\Framework\App\Application; |
|
53
|
|
|
use Platine\Stdlib\Helper\Path; |
|
54
|
|
|
use Platine\Stdlib\Helper\Str; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @class MakeCommand |
|
58
|
|
|
* @package Platine\Framework\Console |
|
59
|
|
|
*/ |
|
60
|
|
|
abstract class MakeCommand extends Command |
|
61
|
|
|
{ |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The Application instance |
|
65
|
|
|
* @var Application |
|
66
|
|
|
*/ |
|
67
|
|
|
protected Application $application; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The file system to use |
|
71
|
|
|
* @var Filesystem |
|
72
|
|
|
*/ |
|
73
|
|
|
protected Filesystem $filesystem; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The application root name space |
|
77
|
|
|
* @var string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected string $rootNamespace; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* The type of class |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected string $type = ''; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* The class full name given by user |
|
89
|
|
|
* @var string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected string $name = ''; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Create new instance |
|
95
|
|
|
* @param Application $application |
|
96
|
|
|
* @param Filesystem $filesystem |
|
97
|
|
|
*/ |
|
98
|
|
|
public function __construct( |
|
99
|
|
|
Application $application, |
|
100
|
|
|
Filesystem $filesystem |
|
101
|
|
|
) { |
|
102
|
|
|
parent::__construct('make', 'Command to generate class skeleton'); |
|
103
|
|
|
$this->application = $application; |
|
104
|
|
|
$this->filesystem = $filesystem; |
|
105
|
|
|
$this->rootNamespace = $application->getNamespace(); |
|
106
|
|
|
|
|
107
|
|
|
$this->addArgument('name', 'The full class name (can include root namespace', null, true); |
|
108
|
|
|
$this->addOption('-f|--force', 'Overwrite existing files.', false, false); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritodc} |
|
113
|
|
|
*/ |
|
114
|
|
|
public function execute() |
|
115
|
|
|
{ |
|
116
|
|
|
$writer = $this->io()->writer(); |
|
117
|
|
|
$name = $this->getArgumentValue('name'); |
|
118
|
|
|
$cleanName = str_replace('/', '\\', $name); |
|
119
|
|
|
|
|
120
|
|
|
$this->name = $name; |
|
121
|
|
|
|
|
122
|
|
|
$writer->boldGreen(sprintf( |
|
123
|
|
|
'Generation of new %s class [%s]', |
|
124
|
|
|
$this->type, |
|
125
|
|
|
$cleanName |
|
126
|
|
|
), true)->eol(); |
|
127
|
|
|
|
|
128
|
|
|
$className = $this->getFullClassName($name); |
|
|
|
|
|
|
129
|
|
|
$path = $this->getPath(); |
|
130
|
|
|
$namespace = $this->getNamespace(); |
|
131
|
|
|
|
|
132
|
|
|
if ($this->fileExists() && !$this->getOptionValue('force')) { |
|
133
|
|
|
$writer->red(sprintf( |
|
134
|
|
|
'File [%s] already exists.', |
|
135
|
|
|
$path |
|
136
|
|
|
), true); |
|
137
|
|
|
|
|
138
|
|
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$writer->bold('Class: '); |
|
142
|
|
|
$writer->boldBlueBgBlack($className, true); |
|
143
|
|
|
|
|
144
|
|
|
$writer->bold('Path: '); |
|
145
|
|
|
$writer->boldBlueBgBlack($path, true); |
|
146
|
|
|
|
|
147
|
|
|
$writer->bold('Namespace: '); |
|
148
|
|
|
$writer->boldBlueBgBlack($namespace, true); |
|
149
|
|
|
|
|
150
|
|
|
$this->createParentDirectory($path); |
|
151
|
|
|
$this->createClass(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Return the the class template |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
abstract public function getClassTemplate(): string; |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Return the real path for the given name |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getPath(): string |
|
165
|
|
|
{ |
|
166
|
|
|
$class = Str::replaceFirst($this->rootNamespace, '', $this->name); |
|
167
|
|
|
|
|
168
|
|
|
$path = sprintf( |
|
169
|
|
|
'%s/src/%s.php', |
|
170
|
|
|
$this->application->getAppPath(), |
|
171
|
|
|
str_replace('\\', '/', $class) |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
return Path::normalizePathDS($path); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Return the class name with the root name space |
|
179
|
|
|
* @param string $name |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function getFullClassName(string $name): string |
|
183
|
|
|
{ |
|
184
|
|
|
$classClean = ltrim($name, '/\\'); |
|
185
|
|
|
$class = str_replace('/', '\\', $classClean); |
|
186
|
|
|
|
|
187
|
|
|
if (Str::startsWith($this->rootNamespace, $class)) { |
|
188
|
|
|
return $class; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$fullyClass = $this->getFullClassName(sprintf( |
|
192
|
|
|
'%s\\%s', |
|
193
|
|
|
trim($this->rootNamespace, '\\'), |
|
194
|
|
|
$class |
|
195
|
|
|
)); |
|
196
|
|
|
|
|
197
|
|
|
return $fullyClass; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Return the full name space for the given class |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function getNamespace(): string |
|
205
|
|
|
{ |
|
206
|
|
|
$class = str_replace('/', '\\', $this->name); |
|
207
|
|
|
|
|
208
|
|
|
return $this->rootNamespace . trim(implode( |
|
209
|
|
|
'\\', |
|
210
|
|
|
array_slice(explode('\\', $class), 0, -1) |
|
211
|
|
|
), '\\'); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Whether the file for the given name already exists |
|
216
|
|
|
* @return bool |
|
217
|
|
|
*/ |
|
218
|
|
|
protected function fileExists(): bool |
|
219
|
|
|
{ |
|
220
|
|
|
$className = $this->getFullClassName($this->name); |
|
|
|
|
|
|
221
|
|
|
$path = $this->getPath(); |
|
222
|
|
|
|
|
223
|
|
|
return $this->filesystem->file($path)->exists(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Create the class for the given name |
|
228
|
|
|
* @return void |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function createClass(): void |
|
231
|
|
|
{ |
|
232
|
|
|
$template = $this->getClassTemplate(); |
|
233
|
|
|
$path = $this->getPath(); |
|
234
|
|
|
|
|
235
|
|
|
$file = $this->filesystem->file($path); |
|
236
|
|
|
|
|
237
|
|
|
$content = $this->replaceNamespace($template); |
|
238
|
|
|
$content = $this->replaceClasses($content); |
|
239
|
|
|
$file->write($content); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Return the short class name |
|
244
|
|
|
* @return string |
|
245
|
|
|
*/ |
|
246
|
|
|
protected function getShortClassName(): string |
|
247
|
|
|
{ |
|
248
|
|
|
$namespace = $this->getNamespace(); |
|
249
|
|
|
|
|
250
|
|
|
return Str::replaceFirst( |
|
251
|
|
|
$namespace . '\\', |
|
252
|
|
|
'', |
|
253
|
|
|
$this->getFullClassName($this->name) |
|
254
|
|
|
); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Create the class parent(s) directory if it does not exist |
|
259
|
|
|
* @param string $path |
|
260
|
|
|
* @return void |
|
261
|
|
|
*/ |
|
262
|
|
|
protected function createParentDirectory(string $path): void |
|
263
|
|
|
{ |
|
264
|
|
|
$file = $this->filesystem->file($path); |
|
265
|
|
|
$location = $file->getLocation(); |
|
266
|
|
|
if (!empty($location)) { |
|
267
|
|
|
$directory = $this->filesystem->directory($location); |
|
268
|
|
|
if (!$directory->exists()) { |
|
269
|
|
|
$directory->create('', 0775, true); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Replace the name space |
|
276
|
|
|
* @param string $content |
|
277
|
|
|
* @return string |
|
278
|
|
|
*/ |
|
279
|
|
|
protected function replaceNamespace(string $content): string |
|
280
|
|
|
{ |
|
281
|
|
|
$namespace = $this->getNamespace(); |
|
282
|
|
|
return str_replace('%namespace%', $namespace, $content); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Replace the classes |
|
287
|
|
|
* @param string $content |
|
288
|
|
|
* @return string |
|
289
|
|
|
*/ |
|
290
|
|
|
protected function replaceClasses(string $content): string |
|
291
|
|
|
{ |
|
292
|
|
|
$shortClassName = $this->getShortClassName(); |
|
293
|
|
|
$fullClassName = $this->getFullClassName($this->name); |
|
294
|
|
|
|
|
295
|
|
|
$replaced = str_replace('%classname%', $shortClassName, $content); |
|
296
|
|
|
|
|
297
|
|
|
return str_replace('%fullclassname%', $fullClassName, $replaced); |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|