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