forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneratorTrait.php
527 lines (448 loc) · 13.6 KB
/
GeneratorTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\CLI;
use Config\Generators;
use Throwable;
/**
* GeneratorTrait contains a collection of methods
* to build the commands that generates a file.
*/
trait GeneratorTrait
{
/**
* Component Name
*
* @var string
*/
protected $component;
/**
* File directory
*
* @var string
*/
protected $directory;
/**
* (Optional) View template path
*
* We use special namespaced paths like:
* `CodeIgniter\Commands\Generators\Views\cell.tpl.php`.
*/
protected ?string $templatePath = null;
/**
* View template name for fallback
*
* @var string
*/
protected $template;
/**
* Language string key for required class names.
*
* @var string
*/
protected $classNameLang = '';
/**
* Namespace to use for class.
* Leave null to use the default namespace.
*/
protected ?string $namespace = null;
/**
* Whether to require class name.
*
* @internal
*
* @var bool
*/
private $hasClassName = true;
/**
* Whether to sort class imports.
*
* @internal
*
* @var bool
*/
private $sortImports = true;
/**
* Whether the `--suffix` option has any effect.
*
* @internal
*
* @var bool
*/
private $enabledSuffixing = true;
/**
* The params array for easy access by other methods.
*
* @internal
*
* @var array<int|string, string|null>
*/
private $params = [];
/**
* Execute the command.
*
* @param array<int|string, string|null> $params
*
* @deprecated use generateClass() instead
*/
protected function execute(array $params): void
{
$this->generateClass($params);
}
/**
* Generates a class file from an existing template.
*
* @param array<int|string, string|null> $params
*/
protected function generateClass(array $params): void
{
$this->params = $params;
// Get the fully qualified class name from the input.
$class = $this->qualifyClassName();
// Get the file path from class name.
$target = $this->buildPath($class);
// Check if path is empty.
if ($target === '') {
return;
}
$this->generateFile($target, $this->buildContent($class));
}
/**
* Generate a view file from an existing template.
*
* @param string $view namespaced view name that is generated
* @param array<int|string, string|null> $params
*/
protected function generateView(string $view, array $params): void
{
$this->params = $params;
$target = $this->buildPath($view);
// Check if path is empty.
if ($target === '') {
return;
}
$this->generateFile($target, $this->buildContent($view));
}
/**
* Handles writing the file to disk, and all of the safety checks around that.
*
* @param string $target file path
*/
private function generateFile(string $target, string $content): void
{
if ($this->getOption('namespace') === 'CodeIgniter') {
// @codeCoverageIgnoreStart
CLI::write(lang('CLI.generator.usingCINamespace'), 'yellow');
CLI::newLine();
if (
CLI::prompt(
'Are you sure you want to continue?',
['y', 'n'],
'required',
) === 'n'
) {
CLI::newLine();
CLI::write(lang('CLI.generator.cancelOperation'), 'yellow');
CLI::newLine();
return;
}
CLI::newLine();
// @codeCoverageIgnoreEnd
}
$isFile = is_file($target);
// Overwriting files unknowingly is a serious annoyance, So we'll check if
// we are duplicating things, If 'force' option is not supplied, we bail.
if (! $this->getOption('force') && $isFile) {
CLI::error(
lang('CLI.generator.fileExist', [clean_path($target)]),
'light_gray',
'red',
);
CLI::newLine();
return;
}
// Check if the directory to save the file is existing.
$dir = dirname($target);
if (! is_dir($dir)) {
mkdir($dir, 0755, true);
}
helper('filesystem');
// Build the class based on the details we have, We'll be getting our file
// contents from the template, and then we'll do the necessary replacements.
if (! write_file($target, $content)) {
// @codeCoverageIgnoreStart
CLI::error(
lang('CLI.generator.fileError', [clean_path($target)]),
'light_gray',
'red',
);
CLI::newLine();
return;
// @codeCoverageIgnoreEnd
}
if ($this->getOption('force') && $isFile) {
CLI::write(
lang('CLI.generator.fileOverwrite', [clean_path($target)]),
'yellow',
);
CLI::newLine();
return;
}
CLI::write(
lang('CLI.generator.fileCreate', [clean_path($target)]),
'green',
);
CLI::newLine();
}
/**
* Prepare options and do the necessary replacements.
*
* @param string $class namespaced classname or namespaced view.
*
* @return string generated file content
*/
protected function prepare(string $class): string
{
return $this->parseTemplate($class);
}
/**
* Change file basename before saving.
*
* Useful for components where the file name has a date.
*/
protected function basename(string $filename): string
{
return basename($filename);
}
/**
* Parses the class name and checks if it is already qualified.
*/
protected function qualifyClassName(): string
{
$class = $this->normalizeInputClassName();
// Gets the namespace from input. Don't forget the ending backslash!
$namespace = $this->getNamespace() . '\\';
if (str_starts_with($class, $namespace)) {
return $class; // @codeCoverageIgnore
}
$directoryString = ($this->directory !== null) ? $this->directory . '\\' : '';
return $namespace . $directoryString . str_replace('/', '\\', $class);
}
/**
* Normalize input classname.
*/
private function normalizeInputClassName(): string
{
// Gets the class name from input.
$class = $this->params[0] ?? CLI::getSegment(2);
if ($class === null && $this->hasClassName) {
// @codeCoverageIgnoreStart
$nameLang = $this->classNameLang !== ''
? $this->classNameLang
: 'CLI.generator.className.default';
$class = CLI::prompt(lang($nameLang), null, 'required');
CLI::newLine();
// @codeCoverageIgnoreEnd
}
helper('inflector');
$component = singular($this->component);
/**
* @see https://regex101.com/r/a5KNCR/2
*/
$pattern = sprintf('/([a-z][a-z0-9_\/\\\\]+)(%s)$/i', $component);
if (preg_match($pattern, $class, $matches) === 1) {
$class = $matches[1] . ucfirst($matches[2]);
}
if (
$this->enabledSuffixing && $this->getOption('suffix')
&& preg_match($pattern, $class) !== 1
) {
$class .= ucfirst($component);
}
// Trims input, normalize separators, and ensure that all paths are in Pascalcase.
return ltrim(
implode(
'\\',
array_map(
pascalize(...),
explode('\\', str_replace('/', '\\', trim($class))),
),
),
'\\/',
);
}
/**
* Gets the generator view as defined in the `Config\Generators::$views`,
* with fallback to `$template` when the defined view does not exist.
*
* @param array<string, mixed> $data
*/
protected function renderTemplate(array $data = []): string
{
try {
$template = $this->templatePath ?? config(Generators::class)->views[$this->name];
return view($template, $data, ['debug' => false]);
} catch (Throwable $e) {
log_message('error', (string) $e);
return view(
"CodeIgniter\\Commands\\Generators\\Views\\{$this->template}",
$data,
['debug' => false],
);
}
}
/**
* Performs pseudo-variables contained within view file.
*
* @param string $class namespaced classname or namespaced view.
* @param list<string> $search
* @param list<string> $replace
* @param array<string, bool|string|null> $data
*
* @return string generated file content
*/
protected function parseTemplate(
string $class,
array $search = [],
array $replace = [],
array $data = [],
): string {
// Retrieves the namespace part from the fully qualified class name.
$namespace = trim(
implode(
'\\',
array_slice(explode('\\', $class), 0, -1),
),
'\\',
);
$search[] = '<@php';
$search[] = '{namespace}';
$search[] = '{class}';
$replace[] = '<?php';
$replace[] = $namespace;
$replace[] = str_replace($namespace . '\\', '', $class);
return str_replace($search, $replace, $this->renderTemplate($data));
}
/**
* Builds the contents for class being generated, doing all
* the replacements necessary, and alphabetically sorts the
* imports for a given template.
*/
protected function buildContent(string $class): string
{
$template = $this->prepare($class);
if (
$this->sortImports
&& preg_match(
'/(?P<imports>(?:^use [^;]+;$\n?)+)/m',
$template,
$match,
)
) {
$imports = explode("\n", trim($match['imports']));
sort($imports);
return str_replace(trim($match['imports']), implode("\n", $imports), $template);
}
return $template;
}
/**
* Builds the file path from the class name.
*
* @param string $class namespaced classname or namespaced view.
*/
protected function buildPath(string $class): string
{
$namespace = $this->getNamespace();
// Check if the namespace is actually defined and we are not just typing gibberish.
$base = service('autoloader')->getNamespace($namespace);
if (! $base = reset($base)) {
CLI::error(
lang('CLI.namespaceNotDefined', [$namespace]),
'light_gray',
'red',
);
CLI::newLine();
return '';
}
$realpath = realpath($base);
$base = ($realpath !== false) ? $realpath : $base;
$file = $base . DIRECTORY_SEPARATOR
. str_replace(
'\\',
DIRECTORY_SEPARATOR,
trim(str_replace($namespace . '\\', '', $class), '\\'),
) . '.php';
return implode(
DIRECTORY_SEPARATOR,
array_slice(
explode(DIRECTORY_SEPARATOR, $file),
0,
-1,
),
) . DIRECTORY_SEPARATOR . $this->basename($file);
}
/**
* Gets the namespace from the command-line option,
* or the default namespace if the option is not set.
* Can be overridden by directly setting $this->namespace.
*/
protected function getNamespace(): string
{
return $this->namespace ?? trim(
str_replace(
'/',
'\\',
$this->getOption('namespace') ?? APP_NAMESPACE,
),
'\\',
);
}
/**
* Allows child generators to modify the internal `$hasClassName` flag.
*
* @return $this
*/
protected function setHasClassName(bool $hasClassName)
{
$this->hasClassName = $hasClassName;
return $this;
}
/**
* Allows child generators to modify the internal `$sortImports` flag.
*
* @return $this
*/
protected function setSortImports(bool $sortImports)
{
$this->sortImports = $sortImports;
return $this;
}
/**
* Allows child generators to modify the internal `$enabledSuffixing` flag.
*
* @return $this
*/
protected function setEnabledSuffixing(bool $enabledSuffixing)
{
$this->enabledSuffixing = $enabledSuffixing;
return $this;
}
/**
* Gets a single command-line option. Returns TRUE if the option exists,
* but doesn't have a value, and is simply acting as a flag.
*/
protected function getOption(string $name): bool|string|null
{
if (! array_key_exists($name, $this->params)) {
return CLI::getOption($name);
}
return $this->params[$name] ?? true;
}
}