|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Gii\Generator\CRUD; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\ActiveRecord\ActiveRecordFactory; |
|
9
|
|
|
use Yiisoft\Aliases\Aliases; |
|
10
|
|
|
use Yiisoft\Strings\StringHelper; |
|
11
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
|
12
|
|
|
use Yiisoft\Yii\Gii\Component\CodeFile\CodeFile; |
|
13
|
|
|
use Yiisoft\Yii\Gii\Generator\AbstractGenerator; |
|
14
|
|
|
use Yiisoft\Yii\Gii\GeneratorCommandInterface; |
|
15
|
|
|
use Yiisoft\Yii\Gii\ParametersProvider; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This generator will generate a controller and one or a few action view files. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class Generator extends AbstractGenerator |
|
21
|
|
|
{ |
|
22
|
|
|
public function __construct( |
|
23
|
|
|
Aliases $aliases, |
|
24
|
|
|
ValidatorInterface $validator, |
|
25
|
|
|
ParametersProvider $parametersProvider, |
|
26
|
|
|
private readonly ActiveRecordFactory $activeRecordFactory, |
|
27
|
|
|
) { |
|
28
|
|
|
parent::__construct($aliases, $validator, $parametersProvider); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function getId(): string |
|
32
|
|
|
{ |
|
33
|
|
|
return 'crud'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function getName(): string |
|
37
|
|
|
{ |
|
38
|
|
|
return 'CRUD'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function getDescription(): string |
|
42
|
|
|
{ |
|
43
|
|
|
return 'This generator helps you to quickly generate a new controller class with |
|
44
|
|
|
one or several controller actions and their corresponding views.'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getRequiredTemplates(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
'controller.php', |
|
51
|
|
|
'view.php', |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function doGenerate(GeneratorCommandInterface $command): array |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$command instanceof Command) { |
|
58
|
|
|
throw new InvalidArgumentException(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$files = []; |
|
62
|
|
|
|
|
63
|
|
|
$rootPath = $this->aliases->get('@root'); |
|
64
|
|
|
|
|
65
|
|
|
$codeFile = (new CodeFile( |
|
66
|
|
|
$this->getControllerFile($command), |
|
67
|
|
|
$this->render($command, 'controller.php') |
|
68
|
|
|
))->withBasePath($rootPath); |
|
69
|
|
|
$files[$codeFile->getId()] = $codeFile; |
|
70
|
|
|
|
|
71
|
|
|
//$actions = $command->getActions(); |
|
72
|
|
|
$actions = ['index', 'view']; |
|
73
|
|
|
$model = $this->activeRecordFactory->createAR($command->getModel()); |
|
74
|
|
|
foreach ($actions as $action) { |
|
75
|
|
|
$codeFile = (new CodeFile( |
|
76
|
|
|
$this->getViewFile($command, $action), |
|
77
|
|
|
$this->render($command, $action . '.php', ['action' => $action, 'model' => $model]) |
|
78
|
|
|
))->withBasePath($rootPath); |
|
79
|
|
|
$files[$codeFile->getId()] = $codeFile; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $files; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string the controller class file path |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getControllerFile(Command $command): string |
|
89
|
|
|
{ |
|
90
|
|
|
$directory = empty($command->getDirectory()) ? '@src/Controller/' : $command->getDirectory(); |
|
91
|
|
|
|
|
92
|
|
|
return $this->aliases->get( |
|
93
|
|
|
str_replace( |
|
94
|
|
|
['\\', '//'], |
|
95
|
|
|
'/', |
|
96
|
|
|
sprintf( |
|
97
|
|
|
'%s/%s.php', |
|
98
|
|
|
$directory, |
|
99
|
|
|
StringHelper::baseName($command->getModel()) . 'Controller', |
|
100
|
|
|
), |
|
101
|
|
|
), |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $action the action ID |
|
107
|
|
|
* |
|
108
|
|
|
* @return string the action view file path |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getViewFile(Command $command, string $action): string |
|
111
|
|
|
{ |
|
112
|
|
|
$directory = empty($command->getViewsPath()) ? '@views/' : $command->getViewsPath(); |
|
113
|
|
|
|
|
114
|
|
|
return $this->aliases->get( |
|
115
|
|
|
str_replace( |
|
116
|
|
|
['\\', '//'], |
|
117
|
|
|
'/', |
|
118
|
|
|
sprintf( |
|
119
|
|
|
'%s/%s/%s.php', |
|
120
|
|
|
$directory, |
|
121
|
|
|
StringHelper::lowercase(StringHelper::baseName($command->getModel())), |
|
122
|
|
|
$action, |
|
123
|
|
|
), |
|
124
|
|
|
), |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public static function getCommandClass(): string |
|
129
|
|
|
{ |
|
130
|
|
|
return Command::class; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|