1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Scaffolder\Declaration; |
6
|
|
|
|
7
|
|
|
use Spiral\Console\Attribute\Argument; |
8
|
|
|
use Spiral\Console\Attribute\AsCommand; |
9
|
|
|
use Spiral\Console\Attribute\Option; |
10
|
|
|
use Spiral\Console\Attribute\Question; |
11
|
|
|
use Spiral\Console\Command; |
12
|
|
|
use Spiral\Scaffolder\Config\ScaffolderConfig; |
13
|
|
|
|
14
|
|
|
final class CommandDeclaration extends AbstractDeclaration implements HasInstructions |
15
|
|
|
{ |
16
|
|
|
public const TYPE = 'command'; |
17
|
|
|
|
18
|
6 |
|
public function __construct( |
19
|
|
|
ScaffolderConfig $config, |
20
|
|
|
string $name, |
21
|
|
|
?string $comment = null, |
22
|
|
|
?string $namespace = null, |
23
|
|
|
private readonly ?string $alias = null, |
24
|
|
|
private readonly ?string $description = null, |
25
|
|
|
) { |
26
|
6 |
|
parent::__construct($config, $name, $comment, $namespace); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
public function addArgument(string $name): void |
30
|
|
|
{ |
31
|
1 |
|
$this->class |
32
|
1 |
|
->addProperty($name) |
33
|
1 |
|
->setPrivate() |
34
|
1 |
|
->setType('string') |
35
|
1 |
|
->addAttribute(Argument::class, [ |
36
|
1 |
|
'description' => 'Argument description', |
37
|
1 |
|
]) |
38
|
1 |
|
->addAttribute(Question::class, [ |
39
|
1 |
|
'question' => \sprintf('What would you like to name the %s argument?', $name), |
40
|
1 |
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function addOption(string $name): void |
44
|
|
|
{ |
45
|
1 |
|
$this->class |
46
|
1 |
|
->addProperty($name) |
47
|
1 |
|
->setPrivate() |
48
|
1 |
|
->setType('bool') |
49
|
1 |
|
->addAttribute(Option::class, [ |
50
|
1 |
|
'description' => 'Argument description', |
51
|
1 |
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Declare default command body. |
56
|
|
|
*/ |
57
|
6 |
|
public function declare(): void |
58
|
|
|
{ |
59
|
6 |
|
$this->namespace->addUse(Command::class); |
60
|
6 |
|
$this->namespace->addUse(AsCommand::class); |
61
|
6 |
|
$this->namespace->addUse(Argument::class); |
62
|
6 |
|
$this->namespace->addUse(Option::class); |
63
|
6 |
|
$this->namespace->addUse(Question::class); |
64
|
|
|
|
65
|
6 |
|
$this->class->setExtends(Command::class); |
66
|
6 |
|
$this->class->setFinal(); |
67
|
|
|
|
68
|
6 |
|
$commandDefinition = [ |
69
|
6 |
|
'name' => $this->alias, |
70
|
6 |
|
]; |
71
|
|
|
|
72
|
6 |
|
if ($this->description) { |
73
|
3 |
|
$commandDefinition['description'] = $this->description; |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
$this->class->addAttribute(AsCommand::class, $commandDefinition); |
77
|
|
|
|
78
|
6 |
|
$this->class |
79
|
6 |
|
->addMethod('__invoke') |
80
|
6 |
|
->setReturnType('int') |
81
|
6 |
|
->setBody( |
82
|
6 |
|
<<<'PHP' |
83
|
|
|
// Put your command logic here |
84
|
|
|
$this->info('Command logic is not implemented yet'); |
85
|
|
|
|
86
|
|
|
return self::SUCCESS; |
87
|
6 |
|
PHP, |
88
|
6 |
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getInstructions(): array |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
\sprintf('Use the following command to run your command: \'<comment>php app.php %s</comment>\'', $this->alias), |
95
|
|
|
'Read more about user Commands in the documentation: https://spiral.dev/docs/console-commands', |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|