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