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