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\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
|
|
|
public static function getName(): string |
18
|
|
|
{ |
19
|
|
|
return 'Controller'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function getDescription(): string |
23
|
|
|
{ |
24
|
|
|
return 'This generator helps you to quickly generate a new controller class with |
25
|
|
|
one or several controller actions and their corresponding views.'; |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
public static function getId(): string |
29
|
|
|
{ |
30
|
1 |
|
return 'controller'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function requiredTemplates(): 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 |
|
$files[] = (new CodeFile( |
52
|
1 |
|
$this->getControllerFile($command), |
53
|
1 |
|
$this->render($command, 'controller') |
54
|
1 |
|
))->withBasePath($rootPath); |
55
|
|
|
|
56
|
1 |
|
foreach ($command->getActions() as $action) { |
57
|
1 |
|
$files[] = (new CodeFile( |
58
|
1 |
|
$this->getViewFile($command, $action), |
59
|
1 |
|
$this->render($command, 'view', ['action' => $action]) |
60
|
1 |
|
))->withBasePath($rootPath); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return $files; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string the controller class file path |
68
|
|
|
*/ |
69
|
1 |
|
private function getControllerFile(ControllerCommand $command): string |
70
|
|
|
{ |
71
|
1 |
|
return $this->aliases->get( |
72
|
1 |
|
sprintf('%s/%s.php', $this->getDirectory() ?? '', $command->getControllerClass()) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $action the action ID |
78
|
|
|
* |
79
|
|
|
* @return string the action view file path |
80
|
|
|
*/ |
81
|
1 |
|
public function getViewFile(ControllerCommand $command, string $action): string |
82
|
|
|
{ |
83
|
1 |
|
if (empty($command->getViewsPath())) { |
84
|
1 |
|
return $this->aliases->get( |
85
|
1 |
|
"@views/{$command->getControllerID()}/$action.php" |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->aliases->get(str_replace('\\', '/', $command->getViewsPath()) . "/$action.php"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function getCommandClass(): string |
93
|
|
|
{ |
94
|
|
|
return ControllerCommand::class; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|