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