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
|
1 |
|
public function perform(ScaffolderConfig $config, DirectoriesInterface $dirs, Console $console): int |
22
|
|
|
{ |
23
|
1 |
|
$this->output->title('Scaffolder commands'); |
|
|
|
|
24
|
1 |
|
$this->writeln( |
25
|
1 |
|
'Scaffolder enables developers to quickly and easily generate application code for various classes, using a set of console commands', |
26
|
1 |
|
); |
27
|
|
|
|
28
|
1 |
|
$this->newLine(); |
29
|
|
|
|
30
|
1 |
|
$table = $this->table(['Command', 'Target']); |
31
|
1 |
|
$rootDir = $dirs->get('root'); |
32
|
1 |
|
$available = $config->getDeclarations(); |
33
|
|
|
|
34
|
1 |
|
$i = 0; |
35
|
1 |
|
foreach ($available as $name) { |
36
|
1 |
|
$command = 'create:' . $name; |
37
|
|
|
|
38
|
1 |
|
if (!$console->getApplication()->has($command)) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
$command = $console->getApplication()->get($command); |
43
|
|
|
|
44
|
1 |
|
if ($i > 0) { |
45
|
1 |
|
$table->addRow(new TableSeparator()); |
46
|
|
|
} |
47
|
1 |
|
$declaration = $config->getDeclaration($name); |
48
|
|
|
|
49
|
1 |
|
$options = []; |
50
|
1 |
|
foreach ($declaration['options'] ?? [] as $key => $value) { |
51
|
1 |
|
$options[] = $key . ': <fg=yellow>' . \json_encode(\str_replace($rootDir, '', $value)) . '</>'; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$file = \str_replace($rootDir, '', $config->classFilename($name, $this->name)); |
55
|
1 |
|
$namespace = $config->classNamespace($name, $this->name); |
56
|
1 |
|
$table->addRow([ |
57
|
1 |
|
$command->getName() . "\n<fg=gray>{$command->getDescription()}</>", |
58
|
1 |
|
<<<TARGET |
59
|
1 |
|
path: <fg=green>/$file</> |
60
|
1 |
|
namespace: <fg=yellow>$namespace</> |
61
|
1 |
|
TARGET |
62
|
1 |
|
. |
63
|
1 |
|
($options !== [] ? "\n" . \implode("\n", $options) : ''), |
64
|
1 |
|
]); |
65
|
|
|
|
66
|
1 |
|
$i++; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$randomName = $available[\array_rand($available)]; |
70
|
1 |
|
$this->writeln( |
71
|
1 |
|
"<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
|
1 |
|
); |
73
|
|
|
|
74
|
1 |
|
$table->render(); |
75
|
|
|
|
76
|
1 |
|
$this->writeln( |
77
|
1 |
|
'<info>Use `<fg=yellow>php app.php create:*** --help</>` command to see available options.</info>', |
78
|
1 |
|
); |
79
|
|
|
|
80
|
1 |
|
$this->writeln('Read more about scaffolder in <fg=yellow>https://spiral.dev/docs/basics-scaffolding</> documentation section.'); |
81
|
|
|
|
82
|
1 |
|
return self::SUCCESS; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|