From d9808c1ac193450d0ab3f42b19b0b018a22a2265 Mon Sep 17 00:00:00 2001 From: mscherer Date: Mon, 1 Jan 2024 20:07:09 +0100 Subject: [PATCH] Cleanup --- src/Command/ModelCommand.php | 56 ++++++++++++++++-------------- templates/bake/Template/index.twig | 2 +- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/Command/ModelCommand.php b/src/Command/ModelCommand.php index 535e0239..331702c3 100644 --- a/src/Command/ModelCommand.php +++ b/src/Command/ModelCommand.php @@ -29,9 +29,12 @@ use Cake\Database\Schema\CachedCollection; use Cake\Database\Schema\TableSchema; use Cake\Database\Schema\TableSchemaInterface; +use Cake\Database\Type\EnumType; +use Cake\Database\TypeFactory; use Cake\Datasource\ConnectionManager; use Cake\ORM\Table; use Cake\Utility\Inflector; +use ReflectionEnum; use function Cake\Core\pluginSplit; /** @@ -1466,7 +1469,10 @@ protected function getEnumDefinitions(TableSchemaInterface $schema): array foreach ($schema->columns() as $column) { $columnSchema = $schema->getColumn($column); - if (!in_array($columnSchema['type'], ['string', 'integer', 'tinyinteger', 'smallinteger'], true)) { + if ( + !in_array($columnSchema['type'], ['string', 'integer', 'tinyinteger', 'smallinteger'], true) + && !str_starts_with($columnSchema['type'], 'enum-') + ) { continue; } @@ -1476,39 +1482,33 @@ protected function getEnumDefinitions(TableSchemaInterface $schema): array $enumsDefinitionString = trim(mb_substr($columnSchema['comment'], strpos($columnSchema['comment'], '[enum]') + 6)); $isInt = in_array($columnSchema['type'], ['integer', 'tinyinteger', 'smallinteger'], true); + if (str_starts_with($columnSchema['type'], 'enum-')) { + $dbType = TypeFactory::build($columnSchema['type']); + if ($dbType instanceof EnumType) { + $class = $dbType->getEnumClassName(); + /** @var \BackedEnum $enum */ + $rEnum = new ReflectionEnum($class); + $rBackingType = $rEnum->getBackingType(); + $type = (string)$rBackingType; + if ($type === 'int') { + $isInt = true; + } + } + } $enumsDefinition = EnumParser::parseCases($enumsDefinitionString, $isInt); if (!$enumsDefinition) { continue; } - $enums[$column] = $enumsDefinition; + $enums[$column] = [ + 'type' => $isInt ? 'int' : 'string', + 'cases' => $enumsDefinition, + ]; } return $enums; } - /** - * @param string $enumsDefinitionString - * @return array - */ - protected function parseEnumsDefinition(string $enumsDefinitionString): array - { - $enumCases = explode(',', $enumsDefinitionString); - - $definition = []; - foreach ($enumCases as $enumCase) { - $key = $value = trim($enumCase); - if (str_contains($key, ':')) { - $value = trim(mb_substr($key, strpos($key, ':') + 1)); - $key = mb_substr($key, 0, strpos($key, ':')); - } - - $definition[$key] = mb_strtolower($value); - } - - return $definition; - } - /** * @param \Cake\ORM\Table $model * @param array $data @@ -1525,7 +1525,7 @@ protected function bakeEnums(Table $model, array $data, Arguments $args, Console $entity = $this->_entityName($model->getAlias()); - foreach ($enums as $column => $enum) { + foreach ($enums as $column => $data) { $enumCommand = new EnumCommand(); $name = $entity . Inflector::camelize($column); @@ -1533,14 +1533,16 @@ protected function bakeEnums(Table $model, array $data, Arguments $args, Console $name = $this->plugin . '.' . $name; } + $enumCases = $data['cases']; + $cases = []; - foreach ($enum as $k => $v) { + foreach ($enumCases as $k => $v) { $cases[] = $k . ':' . $v; } $args = new Arguments( [$name, implode(',', $cases)], - ['int' => false] + $args->getOptions(), + ['int' => $data['type'] === 'int'] + $args->getOptions(), ['name', 'cases'] ); $enumCommand->execute($args, $io); diff --git a/templates/bake/Template/index.twig b/templates/bake/Template/index.twig index 2b0210ad..0c630067 100644 --- a/templates/bake/Template/index.twig +++ b/templates/bake/Template/index.twig @@ -50,7 +50,7 @@ {% if isKey is not same as(true) %} {% set columnData = Bake.columnData(field, schema) %} {% if columnData.type starts with 'enum-' %} - {{ field }}->label()) ?> + {{ field }} === null ? '' : h(${{ singularVar }}->{{ field }}->label()) ?> {% elseif columnData.type not in ['integer', 'float', 'decimal', 'biginteger', 'smallinteger', 'tinyinteger'] %} {{ field }}) ?> {% elseif columnData.null %}