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\GeneratorCommandInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This generator will generate a controller and one or a few action view files. |
14
|
|
|
*/ |
15
|
|
|
final class Generator extends AbstractGenerator |
16
|
|
|
{ |
17
|
8 |
|
public static function getId(): string |
18
|
|
|
{ |
19
|
8 |
|
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
|
1 |
|
public function getRequiredTemplates(): array |
34
|
|
|
{ |
35
|
1 |
|
return [ |
36
|
1 |
|
'controller.php', |
37
|
1 |
|
'view.php', |
38
|
1 |
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
public function doGenerate(GeneratorCommandInterface $command): array |
42
|
|
|
{ |
43
|
2 |
|
if (!$command instanceof Command) { |
44
|
|
|
throw new InvalidArgumentException(); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
$files = []; |
48
|
|
|
|
49
|
2 |
|
$rootPath = $this->aliases->get('@root'); |
50
|
|
|
|
51
|
2 |
|
$codeFile = (new CodeFile( |
52
|
2 |
|
$this->getControllerFile($command), |
53
|
2 |
|
$this->render($command, 'controller.php') |
54
|
2 |
|
))->withBasePath($rootPath); |
55
|
2 |
|
$files[$codeFile->getId()] = $codeFile; |
56
|
|
|
|
57
|
2 |
|
foreach ($command->getActions() as $action) { |
58
|
2 |
|
$codeFile = (new CodeFile( |
59
|
2 |
|
$this->getViewFile($command, $action), |
60
|
2 |
|
$this->render($command, 'view.php', ['action' => $action]) |
61
|
2 |
|
))->withBasePath($rootPath); |
62
|
2 |
|
$files[$codeFile->getId()] = $codeFile; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return $files; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string the controller class file path |
70
|
|
|
*/ |
71
|
2 |
|
private function getControllerFile(Command $command): string |
72
|
|
|
{ |
73
|
2 |
|
$directory = empty($command->getDirectory()) ? '@src/Controller/' : $command->getDirectory(); |
74
|
|
|
|
75
|
2 |
|
return $this->aliases->get( |
76
|
2 |
|
str_replace( |
77
|
2 |
|
['\\', '//'], |
78
|
2 |
|
'/', |
79
|
2 |
|
sprintf( |
80
|
2 |
|
'%s/%s.php', |
81
|
2 |
|
$directory, |
82
|
2 |
|
$command->getControllerClass(), |
83
|
2 |
|
), |
84
|
2 |
|
), |
85
|
2 |
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $action the action ID |
90
|
|
|
* |
91
|
|
|
* @return string the action view file path |
92
|
|
|
*/ |
93
|
2 |
|
public function getViewFile(Command $command, string $action): string |
94
|
|
|
{ |
95
|
2 |
|
$directory = empty($command->getViewsPath()) ? '@views/' : $command->getViewsPath(); |
96
|
|
|
|
97
|
2 |
|
return $this->aliases->get( |
98
|
2 |
|
str_replace( |
99
|
2 |
|
['\\', '//'], |
100
|
2 |
|
'/', |
101
|
2 |
|
sprintf( |
102
|
2 |
|
'%s/%s/%s.php', |
103
|
2 |
|
$directory, |
104
|
2 |
|
$command->getControllerID(), |
105
|
2 |
|
$action, |
106
|
2 |
|
), |
107
|
2 |
|
), |
108
|
2 |
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
public static function getCommandClass(): string |
112
|
|
|
{ |
113
|
2 |
|
return Command::class; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|