| Total Complexity | 11 |
| Total Lines | 90 |
| Duplicated Lines | 0 % |
| Coverage | 90.2% |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
| 1 | <?php |
||
| 19 | final class Generator extends AbstractGenerator |
||
| 20 | { |
||
| 21 | 8 | public function __construct( |
|
| 28 | } |
||
| 29 | |||
| 30 | 8 | public static function getId(): string |
|
| 31 | { |
||
| 32 | 8 | return 'active-record'; |
|
| 33 | } |
||
| 34 | |||
| 35 | public static function getName(): string |
||
| 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 |
|
| 101 | 2 | ), |
|
| 102 | 2 | ), |
|
| 103 | 2 | ); |
|
| 104 | } |
||
| 105 | |||
| 106 | 2 | public static function getCommandClass(): string |
|
| 109 | } |
||
| 110 | } |
||
| 111 |