1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Gii\Generator\ActiveRecord; |
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
|
|
|
public static function getId(): string |
18
|
|
|
{ |
19
|
|
|
return 'active-record'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function getName(): string |
23
|
|
|
{ |
24
|
|
|
return 'Active Record'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function getDescription(): string |
28
|
|
|
{ |
29
|
|
|
return ''; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getRequiredTemplates(): array |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
'model.php', |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function doGenerate(GeneratorCommandInterface $command): array |
40
|
|
|
{ |
41
|
|
|
if (!$command instanceof Command) { |
42
|
|
|
throw new InvalidArgumentException(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$files = []; |
46
|
|
|
|
47
|
|
|
$rootPath = $this->aliases->get('@root'); |
48
|
|
|
|
49
|
|
|
$path = $this->getControllerFile($command); |
50
|
|
|
$codeFile = (new CodeFile( |
51
|
|
|
$path, |
52
|
|
|
$this->render($command, 'model.php') |
53
|
|
|
))->withBasePath($rootPath); |
54
|
|
|
$files[$codeFile->getId()] = $codeFile; |
55
|
|
|
|
56
|
|
|
return $files; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string the controller class file path |
61
|
|
|
*/ |
62
|
|
|
private function getControllerFile(Command $command): string |
63
|
|
|
{ |
64
|
|
|
$directory = '@src/Model/'; |
65
|
|
|
|
66
|
|
|
return $this->aliases->get( |
67
|
|
|
str_replace( |
68
|
|
|
['\\', '//'], |
69
|
|
|
'/', |
70
|
|
|
sprintf( |
71
|
|
|
'%s/%s.php', |
72
|
|
|
$directory, |
73
|
|
|
$command->getModelName(), |
74
|
|
|
), |
75
|
|
|
), |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function getCommandClass(): string |
80
|
|
|
{ |
81
|
|
|
return Command::class; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|