|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Scaffolder\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Boot\DirectoriesInterface; |
|
8
|
|
|
use Spiral\Console\Attribute\Argument; |
|
9
|
|
|
use Spiral\Console\Attribute\AsCommand; |
|
10
|
|
|
use Spiral\Console\Command; |
|
11
|
|
|
use Spiral\Console\Console; |
|
12
|
|
|
use Spiral\Scaffolder\Config\ScaffolderConfig; |
|
13
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
|
14
|
|
|
|
|
15
|
|
|
#[AsCommand(name: 'scaffolder:info', description: 'Show information about available scaffolder commands.')] |
|
16
|
|
|
final class InfoCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
#[Argument(description: 'Class name')] |
|
19
|
|
|
private string $name = 'Example'; |
|
20
|
|
|
|
|
21
|
|
|
public function perform(ScaffolderConfig $config, DirectoriesInterface $dirs, Console $console): int |
|
22
|
|
|
{ |
|
23
|
|
|
$this->output->title('Scaffolder commands'); |
|
|
|
|
|
|
24
|
|
|
$this->writeln( |
|
25
|
|
|
'Scaffolder enables developers to quickly and easily generate application code for various classes, using a set of console commands', |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
$this->newLine(); |
|
29
|
|
|
|
|
30
|
|
|
$table = $this->table(['Command', 'Target']); |
|
31
|
|
|
$rootDir = $dirs->get('root'); |
|
32
|
|
|
$available = $config->getDeclarations(); |
|
33
|
|
|
|
|
34
|
|
|
$i = 0; |
|
35
|
|
|
foreach ($available as $name) { |
|
36
|
|
|
$command = 'create:' . $name; |
|
37
|
|
|
|
|
38
|
|
|
if (!$console->getApplication()->has($command)) { |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$command = $console->getApplication()->get($command); |
|
43
|
|
|
|
|
44
|
|
|
if ($i > 0) { |
|
45
|
|
|
$table->addRow(new TableSeparator()); |
|
46
|
|
|
} |
|
47
|
|
|
$declaration = $config->getDeclaration($name); |
|
48
|
|
|
|
|
49
|
|
|
$options = []; |
|
50
|
|
|
foreach ($declaration['options'] ?? [] as $key => $value) { |
|
51
|
|
|
$options[] = $key . ': <fg=yellow>' . \json_encode(\str_replace($rootDir, '', $value)) . '</>'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$file = \str_replace($rootDir, '', $config->classFilename($name, $this->name)); |
|
55
|
|
|
$namespace = $config->classNamespace($name, $this->name); |
|
56
|
|
|
$table->addRow([ |
|
57
|
|
|
$command->getName() . "\n<fg=gray>{$command->getDescription()}</>", |
|
58
|
|
|
<<<TARGET |
|
59
|
|
|
path: <fg=green>/$file</> |
|
60
|
|
|
namespace: <fg=yellow>$namespace</> |
|
61
|
|
|
TARGET |
|
62
|
|
|
. |
|
63
|
|
|
($options !== [] ? "\n" . \implode("\n", $options) : ''), |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
$i++; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$randomName = $available[\array_rand($available)]; |
|
70
|
|
|
$this->writeln( |
|
71
|
|
|
"<info>Use `<fg=yellow>php app.php create:{$randomName} {$this->name}</>` command to generate desired class. Below is a list of available commands:</info>", |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$table->render(); |
|
75
|
|
|
|
|
76
|
|
|
$this->writeln( |
|
77
|
|
|
'<info>Use `<fg=yellow>php app.php create:*** --help</>` command to see available options.</info>', |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$this->writeln('Read more about scaffolder in <fg=yellow>https://spiral.dev/docs/basics-scaffolding</> documentation section.'); |
|
81
|
|
|
|
|
82
|
|
|
return self::SUCCESS; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|