|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Scaffolder\Command; |
|
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\Scaffolder\Declaration\ControllerDeclaration; |
|
12
|
|
|
|
|
13
|
|
|
#[AsCommand(name: 'create:controller', description: 'Create controller declaration')] |
|
14
|
|
|
class ControllerCommand extends AbstractCommand |
|
15
|
|
|
{ |
|
16
|
|
|
#[Argument(description: 'Controller name')] |
|
17
|
|
|
#[Question(question: 'What would you like to name the Controller?')] |
|
18
|
|
|
private string $name; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
#[Option(name: 'action', shortcut: 'a', description: 'Pre-create controller action')] |
|
21
|
|
|
private array $actions = []; |
|
22
|
|
|
|
|
23
|
|
|
#[Option(name: 'prototype', shortcut: 'p', description: 'Add \Spiral\Prototype\Traits\PrototypeTrait to controller')] |
|
24
|
|
|
private bool $usePrototype = false; |
|
25
|
|
|
|
|
26
|
|
|
#[Option(shortcut: 'c', description: 'Optional comment to add as class header')] |
|
27
|
|
|
private ?string $comment = null; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
#[Option(description: 'Optional, specify a custom namespace')] |
|
30
|
|
|
private ?string $namespace = null; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
4 |
|
public function perform(): int |
|
33
|
|
|
{ |
|
34
|
4 |
|
$declaration = $this->createDeclaration(ControllerDeclaration::class); |
|
35
|
|
|
|
|
36
|
4 |
|
foreach ($this->actions as $action) { |
|
37
|
2 |
|
$declaration->addAction($action); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
4 |
|
if ($this->usePrototype) { |
|
41
|
1 |
|
$declaration->addPrototypeTrait(); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
4 |
|
$this->writeDeclaration($declaration); |
|
45
|
|
|
|
|
46
|
4 |
|
return self::SUCCESS; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|