-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a1b5b8
commit d9808c1
Showing
2 changed files
with
30 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
Check failure on line 1490 in src/Command/ModelCommand.php GitHub Actions / cs-stan / Coding Standard & Static Analysis
|
||
$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<int|string, string> | ||
*/ | ||
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<string, mixed> $data | ||
|
@@ -1525,22 +1525,24 @@ 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); | ||
if ($this->plugin) { | ||
$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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters