1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Gii\Generator\Controller; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Strings\Inflector; |
8
|
|
|
use Yiisoft\Strings\StringHelper; |
9
|
|
|
use Yiisoft\Validator\Rule\Each; |
10
|
|
|
use Yiisoft\Validator\Rule\Regex; |
11
|
|
|
use Yiisoft\Validator\Rule\Required; |
12
|
|
|
use Yiisoft\Yii\Gii\Generator\AbstractGeneratorCommand; |
13
|
|
|
use Yiisoft\Yii\Gii\Validator\NewClassRule; |
14
|
|
|
use Yiisoft\Yii\Gii\Validator\TemplateRule; |
15
|
|
|
|
16
|
|
|
final class ControllerCommand extends AbstractGeneratorCommand |
17
|
|
|
{ |
18
|
|
|
public function __construct( |
19
|
|
|
private string $controllerNamespace = 'App\\Controller', |
20
|
|
|
#[Required] |
21
|
|
|
#[Regex( |
22
|
|
|
pattern: '/^[A-Z][\w]*Controller$/', |
23
|
|
|
message: 'Only word characters are allowed, and the class name must start with a capital letter and end with "Controller".' |
24
|
|
|
)] |
25
|
|
|
#[NewClassRule] |
26
|
|
|
/** |
27
|
|
|
* @var string the controller class name |
28
|
|
|
*/ |
29
|
|
|
private string $controllerClass = '', |
30
|
|
|
/** |
31
|
|
|
* @var string|null the controller's views path |
32
|
|
|
*/ |
33
|
|
|
private ?string $viewsPath = null, |
34
|
|
|
#[Regex( |
35
|
|
|
pattern: '/^[\w\\\\]*$/', |
36
|
|
|
message: 'Only word characters and backslashes are allowed.', |
37
|
|
|
skipOnEmpty: true, |
38
|
|
|
)] |
39
|
|
|
/** |
40
|
|
|
* @var string|null the base class of the controller or null if no parent class present |
41
|
|
|
*/ |
42
|
|
|
private ?string $baseClass = null, |
43
|
|
|
#[Each([ |
44
|
|
|
new Regex( |
45
|
|
|
pattern: '/^[a-z][a-z0-9\\-,\\s]*$/', |
46
|
|
|
message: 'Only a-z, 0-9, dashes (-), spaces and commas are allowed.' |
47
|
|
|
), |
48
|
|
|
]) |
49
|
|
|
] |
50
|
|
|
/** |
51
|
|
|
* @var string[] list of action IDs |
52
|
|
|
*/ |
53
|
|
|
private array $actions = ['index'], |
54
|
|
|
#[Required(message: 'A code template must be selected.')] |
55
|
|
|
#[TemplateRule] |
56
|
|
|
protected string $template = 'default', |
57
|
|
|
) { |
58
|
|
|
parent::__construct($template); |
59
|
|
|
sort($this->actions); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string the controller ID |
64
|
|
|
*/ |
65
|
|
|
public function getControllerID(): string |
66
|
|
|
{ |
67
|
|
|
$name = StringHelper::baseName($this->controllerClass); |
68
|
|
|
return (new Inflector())->pascalCaseToId(substr($name, 0, -10)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getControllerClass(): string |
72
|
|
|
{ |
73
|
|
|
return $this->controllerClass; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getActions(): array |
77
|
|
|
{ |
78
|
|
|
return $this->actions; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getViewsPath(): ?string |
82
|
|
|
{ |
83
|
|
|
return $this->viewsPath; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getControllerNamespace(): string |
87
|
|
|
{ |
88
|
|
|
return $this->controllerNamespace; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getBaseClass(): ?string |
92
|
|
|
{ |
93
|
|
|
return $this->baseClass; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public static function getAttributeLabels(): array |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'baseClass' => 'Base Class', |
100
|
|
|
'controllerClass' => 'Controller Class', |
101
|
|
|
'viewsPath' => 'Views Path', |
102
|
|
|
'actions' => 'Action IDs', |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public static function hints(): array |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
|
|
'controllerClass' => 'This is the name of the controller class to be generated. You should |
110
|
|
|
provide a fully qualified namespaced class (e.g. <code>App\Controller\PostController</code>), |
111
|
|
|
and class name should be in CamelCase ending with the word <code>Controller</code>. Make sure the class |
112
|
|
|
is using the same namespace as specified by your application\'s controllerNamespace property.', |
113
|
|
|
'actions' => 'Provide one or multiple action IDs to generate empty action method(s) in the controller. Separate multiple action IDs with commas or spaces. |
114
|
|
|
Action IDs should be in lower case. For example: |
115
|
|
|
<ul> |
116
|
|
|
<li><code>index</code> generates <code>index()</code></li> |
117
|
|
|
<li><code>create-order</code> generates <code>createOrder()</code></li> |
118
|
|
|
</ul>', |
119
|
|
|
'viewsPath' => 'Specify the directory for storing the view scripts for the controller. You may use path alias here, e.g., |
120
|
|
|
<code>/var/www/app/controllers/views/order</code>, <code>@app/views/order</code>. If not set, it will default |
121
|
|
|
to <code>@app/views/ControllerID</code>', |
122
|
|
|
'baseClass' => 'This is the class that the new controller class will extend from. Please make sure the class exists and can be autoloaded.', |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|