|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BlitzPHP\Cli\Traits; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Loader\Services; |
|
15
|
|
|
use BlitzPHP\View\Adapters\NativeAdapter; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* GeneratorTrait contient une collection de méthodes |
|
19
|
|
|
* pour construire les commandes qui génèrent un fichier. |
|
20
|
|
|
*/ |
|
21
|
|
|
trait GeneratorTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Nom du composant |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $component; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Répertoire de fichiers |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $directory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Nom de la vue du template |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $template; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Chemin dans du dossier dans lequelle les vues de generation sont cherchees |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $templatePath = SYST_PATH . 'Cli/Commands/Generators/Views'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Clé de chaîne de langue pour les noms de classe requis. |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $classNameLang = ''; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* S'il faut exiger le nom de la classe. |
|
60
|
|
|
* |
|
61
|
|
|
* @internal |
|
62
|
|
|
* |
|
63
|
|
|
* @var bool |
|
64
|
|
|
*/ |
|
65
|
|
|
private $hasClassName = true; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* S'il faut trier les importations de classe. |
|
69
|
|
|
* |
|
70
|
|
|
* @internal |
|
71
|
|
|
* |
|
72
|
|
|
* @var bool |
|
73
|
|
|
*/ |
|
74
|
|
|
private $sortImports = true; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Indique si l'option `--suffix` a un effet. |
|
78
|
|
|
* |
|
79
|
|
|
* @internal |
|
80
|
|
|
* |
|
81
|
|
|
* @var bool |
|
82
|
|
|
*/ |
|
83
|
|
|
private $enabledSuffixing = true; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Le tableau params pour un accès facile par d'autres méthodes. |
|
87
|
|
|
* |
|
88
|
|
|
* @internal |
|
89
|
|
|
* |
|
90
|
|
|
* @var array |
|
91
|
|
|
*/ |
|
92
|
|
|
private $params = []; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Exécute la commande. |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function runGeneration(array $params): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->params = $params; |
|
100
|
|
|
|
|
101
|
|
|
// Recupere le FQCN. |
|
102
|
|
|
$class = $this->qualifyClassName(); |
|
103
|
|
|
|
|
104
|
|
|
// Recupere le chemin du fichier a partir du nom de la classe. |
|
105
|
|
|
$target = $this->buildPath($class); |
|
106
|
|
|
|
|
107
|
|
|
if (empty($target)) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->generateFile($target, $this->buildContent($class)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Handles writing the file to disk, and all of the safety checks around that. |
|
116
|
|
|
*/ |
|
117
|
|
|
private function generateFile(string $target, string $content): void |
|
118
|
|
|
{ |
|
119
|
|
|
if ($this->option('namespace') === 'BlitzPHP') { |
|
|
|
|
|
|
120
|
|
|
// @codeCoverageIgnoreStart |
|
121
|
|
|
$this->colorize(lang('CLI.generator.usingBlitzNamespace'), 'yellow'); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
if (! $this->confirm('Are you sure you want to continue?')) { |
|
|
|
|
|
|
124
|
|
|
$this->eol()->colorize(lang('CLI.generator.cancelOperation'), 'yellow'); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
return; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$this->eol(); |
|
130
|
|
|
// @codeCoverageIgnoreEnd |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$isFile = is_file($target); |
|
134
|
|
|
|
|
135
|
|
|
// Écraser des fichiers sans le savoir est une gêne sérieuse, nous allons donc vérifier si nous dupliquons des choses, |
|
136
|
|
|
// si l'option "forcer" n'est pas fournie, nous renvoyons. |
|
137
|
|
|
if (! $this->option('force') && $isFile) { |
|
138
|
|
|
$this->io->error(lang('CLI.generator.fileExist', [clean_path($target)]), true); |
|
139
|
|
|
|
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Vérifie si le répertoire pour enregistrer le fichier existe. |
|
144
|
|
|
$dir = dirname($target); |
|
145
|
|
|
|
|
146
|
|
|
if (! is_dir($dir)) { |
|
147
|
|
|
mkdir($dir, 0755, true); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
helper('filesystem'); |
|
151
|
|
|
|
|
152
|
|
|
// Construisez la classe en fonction des détails dont nous disposons. |
|
153
|
|
|
// Nous obtiendrons le contenu de notre fichier à partir du modèle, |
|
154
|
|
|
// puis nous effectuerons les remplacements nécessaires. |
|
155
|
|
|
if (! write_file($target, $content)) { |
|
156
|
|
|
// @codeCoverageIgnoreStart |
|
157
|
|
|
$this->io->error(lang('CLI.generator.fileError', [clean_path($target)]), true); |
|
158
|
|
|
|
|
159
|
|
|
return; |
|
160
|
|
|
// @codeCoverageIgnoreEnd |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if ($this->option('force') && $isFile) { |
|
164
|
|
|
$this->colorize(lang('CLI.generator.fileOverwrite', [clean_path($target)]), 'yellow'); |
|
165
|
|
|
|
|
166
|
|
|
return; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$this->colorize(lang('CLI.generator.fileCreate', [clean_path($target)]), 'green'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Préparez les options et effectuez les remplacements nécessaires. |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function prepare(string $class): string |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->parseTemplate($class); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Changez le nom de base du fichier avant de l'enregistrer. |
|
182
|
|
|
* |
|
183
|
|
|
* Utile pour les composants dont le nom de fichier comporte une date. |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function basename(string $filename): string |
|
186
|
|
|
{ |
|
187
|
|
|
return basename($filename); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Analyse le nom de la classe et vérifie s'il est déjà qualifié. |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function qualifyClassName(): string |
|
194
|
|
|
{ |
|
195
|
|
|
// Obtient le nom de la classe à partir de l'entrée. |
|
196
|
|
|
$class = $this->params[0] ?? $this->params['name'] ?? null; |
|
197
|
|
|
|
|
198
|
|
|
if ($class === null && $this->hasClassName) { |
|
199
|
|
|
// @codeCoverageIgnoreStart |
|
200
|
|
|
$nameLang = $this->classNameLang ?: 'CLI.generator.className.default'; |
|
201
|
|
|
$class = $this->prompt(lang($nameLang)); |
|
|
|
|
|
|
202
|
|
|
$this->eol(); |
|
203
|
|
|
// @codeCoverageIgnoreEnd |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
helper('inflector'); |
|
207
|
|
|
|
|
208
|
|
|
$component = singular($this->component); |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @see https://regex101.com/r/a5KNCR/1 |
|
212
|
|
|
*/ |
|
213
|
|
|
$pattern = sprintf('/([a-z][a-z0-9_\/\\\\]+)(%s)/i', $component); |
|
214
|
|
|
|
|
215
|
|
|
if (preg_match($pattern, $class, $matches) === 1) { |
|
216
|
|
|
$class = $matches[1] . ucfirst($matches[2]); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
if ($this->enabledSuffixing && $this->getOption('suffix') && ! strripos($class, $component)) { |
|
|
|
|
|
|
220
|
|
|
$class .= ucfirst($component); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
// Coupe l'entrée, normalise les séparateurs et s'assure que tous les chemins sont en Pascalcase. |
|
224
|
|
|
$class = ltrim(implode('\\', array_map('pascalize', explode('\\', str_replace('/', '\\', trim($class))))), '\\/'); |
|
225
|
|
|
|
|
226
|
|
|
// Obtient l'espace de noms à partir de l'entrée. N'oubliez pas la barre oblique inverse finale ! |
|
227
|
|
|
$namespace = trim(str_replace('/', '\\', $this->getOption('namespace', APP_NAMESPACE)), '\\') . '\\'; |
|
228
|
|
|
|
|
229
|
|
|
if (strncmp($class, $namespace, strlen($namespace)) === 0) { |
|
230
|
|
|
return $class; // @codeCoverageIgnore |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $namespace . $this->directory . '\\' . str_replace('/', '\\', $class); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Obtient la vue du générateur |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function renderTemplate(array $data = []): string |
|
240
|
|
|
{ |
|
241
|
|
|
$viewer = new NativeAdapter([], $this->templatePath, false); |
|
242
|
|
|
|
|
243
|
|
|
return $viewer->setData($data)->render($this->template); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Exécute les pseudo-variables contenues dans le fichier de vue. |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function parseTemplate(string $class, array $search = [], array $replace = [], array $data = []): string |
|
250
|
|
|
{ |
|
251
|
|
|
// Récupère la partie de l'espace de noms à partir du nom de classe complet. |
|
252
|
|
|
$namespace = trim(implode('\\', array_slice(explode('\\', $class), 0, -1)), '\\'); |
|
253
|
|
|
$search[] = '<@php'; |
|
254
|
|
|
$search[] = '{namespace}'; |
|
255
|
|
|
$search[] = '{class}'; |
|
256
|
|
|
$replace[] = '<?php'; |
|
257
|
|
|
$replace[] = $namespace; |
|
258
|
|
|
$replace[] = str_replace($namespace . '\\', '', $class); |
|
259
|
|
|
|
|
260
|
|
|
return str_replace($search, $replace, $this->renderTemplate($data)); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Construit le contenu de la classe générée, effectue tous les remplacements nécessaires et |
|
265
|
|
|
* trie par ordre alphabétique les importations pour un modèle donné. |
|
266
|
|
|
*/ |
|
267
|
|
|
protected function buildContent(string $class): string |
|
268
|
|
|
{ |
|
269
|
|
|
$template = $this->prepare($class); |
|
270
|
|
|
|
|
271
|
|
|
if ($this->sortImports && preg_match('/(?P<imports>(?:^use [^;]+;$\n?)+)/m', $template, $match)) { |
|
272
|
|
|
$imports = explode("\n", trim($match['imports'])); |
|
273
|
|
|
sort($imports); |
|
274
|
|
|
|
|
275
|
|
|
return str_replace(trim($match['imports']), implode("\n", $imports), $template); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
return $template; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Construit le chemin du fichier à partir du nom de la classe. |
|
283
|
|
|
*/ |
|
284
|
|
|
protected function buildPath(string $class): string |
|
285
|
|
|
{ |
|
286
|
|
|
$namespace = trim(str_replace('/', '\\', $this->option('namespace', APP_NAMESPACE)), '\\'); |
|
287
|
|
|
|
|
288
|
|
|
$base = Services::autoloader()->getNamespace($namespace); |
|
289
|
|
|
|
|
290
|
|
|
if (! $base = reset($base)) { |
|
291
|
|
|
$this->io->error(lang('CLI.namespaceNotDefined', [$namespace]), true); |
|
292
|
|
|
|
|
293
|
|
|
return ''; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$base = realpath($base) ?: $base; |
|
297
|
|
|
$file = $base . DS . str_replace('\\', DS, trim(str_replace($namespace . '\\', '', $class), '\\')) . '.php'; |
|
298
|
|
|
|
|
299
|
|
|
return implode(DS, array_slice(explode(DS, $file), 0, -1)) . DS . $this->basename($file); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Permet aux générateurs enfants de modifier le drapeau interne `$hasClassName`. |
|
304
|
|
|
*/ |
|
305
|
|
|
protected function setHasClassName(bool $hasClassName): self |
|
306
|
|
|
{ |
|
307
|
|
|
$this->hasClassName = $hasClassName; |
|
308
|
|
|
|
|
309
|
|
|
return $this; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Permet aux générateurs enfants de modifier le drapeau interne `$sortImports`. |
|
314
|
|
|
*/ |
|
315
|
|
|
protected function setSortImports(bool $sortImports): self |
|
316
|
|
|
{ |
|
317
|
|
|
$this->sortImports = $sortImports; |
|
318
|
|
|
|
|
319
|
|
|
return $this; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Permet aux générateurs enfants de modifier le drapeau interne `$enabledSuffixing`. |
|
324
|
|
|
*/ |
|
325
|
|
|
protected function setEnabledSuffixing(bool $enabledSuffixing): self |
|
326
|
|
|
{ |
|
327
|
|
|
$this->enabledSuffixing = $enabledSuffixing; |
|
328
|
|
|
|
|
329
|
|
|
return $this; |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|