1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Gii\Generator\ActiveRecord; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Aliases\Aliases; |
9
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
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
|
8 |
|
public function __construct( |
22
|
|
|
Aliases $aliases, |
23
|
|
|
ValidatorInterface $validator, |
24
|
|
|
ParametersProvider $parametersProvider, |
25
|
|
|
private readonly ConnectionInterface $connection, |
26
|
|
|
) { |
27
|
8 |
|
parent::__construct($aliases, $validator, $parametersProvider); |
28
|
|
|
} |
29
|
|
|
|
30
|
8 |
|
public static function getId(): string |
31
|
|
|
{ |
32
|
8 |
|
return 'active-record'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function getName(): string |
36
|
|
|
{ |
37
|
|
|
return 'Active Record'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function getDescription(): string |
41
|
|
|
{ |
42
|
|
|
return ''; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function getRequiredTemplates(): array |
46
|
|
|
{ |
47
|
1 |
|
return [ |
48
|
1 |
|
'model.php', |
49
|
1 |
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function doGenerate(GeneratorCommandInterface $command): array |
53
|
|
|
{ |
54
|
2 |
|
if (!$command instanceof Command) { |
55
|
|
|
throw new InvalidArgumentException(); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
$files = []; |
59
|
|
|
|
60
|
2 |
|
$rootPath = $this->aliases->get('@root'); |
61
|
|
|
|
62
|
2 |
|
$properties = []; |
63
|
2 |
|
if ($schema = $this->connection->getTableSchema($command->getTableName(), true)) { |
64
|
2 |
|
foreach ($schema->getColumns() as $columnSchema) { |
65
|
2 |
|
$properties[] = [ |
66
|
2 |
|
'name' => $columnSchema->getName(), |
67
|
2 |
|
'type' => match ($columnSchema->getPhpType()) { |
68
|
2 |
|
'integer' => 'int', |
69
|
2 |
|
default => 'string', |
70
|
2 |
|
}, |
71
|
2 |
|
'isAllowNull' => $columnSchema->isAllowNull(), |
72
|
2 |
|
'defaultValue' => $columnSchema->getDefaultValue(), |
73
|
2 |
|
]; |
74
|
|
|
} |
75
|
|
|
} |
76
|
2 |
|
$path = $this->getControllerFile($command); |
77
|
2 |
|
$codeFile = (new CodeFile( |
78
|
2 |
|
$path, |
79
|
2 |
|
$this->render($command, 'model.php', ['properties' => $properties]) |
80
|
2 |
|
))->withBasePath($rootPath); |
81
|
2 |
|
$files[$codeFile->getId()] = $codeFile; |
82
|
|
|
|
83
|
2 |
|
return $files; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string the controller class file path |
88
|
|
|
*/ |
89
|
2 |
|
private function getControllerFile(Command $command): string |
90
|
|
|
{ |
91
|
2 |
|
$directory = '@src/Model/'; |
92
|
|
|
|
93
|
2 |
|
return $this->aliases->get( |
94
|
2 |
|
str_replace( |
95
|
2 |
|
['\\', '//'], |
96
|
2 |
|
'/', |
97
|
2 |
|
sprintf( |
98
|
2 |
|
'%s/%s.php', |
99
|
2 |
|
$directory, |
100
|
2 |
|
$command->getModelName(), |
101
|
2 |
|
), |
102
|
2 |
|
), |
103
|
2 |
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
public static function getCommandClass(): string |
107
|
|
|
{ |
108
|
4 |
|
return Command::class; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|