From e3e5b657f280901bb1d76892d5ffbcd2df3e3347 Mon Sep 17 00:00:00 2001 From: Aleksei Zarubin <12220926+alexzarbn@users.noreply.github.com> Date: Fri, 14 Jun 2024 16:16:20 +0500 Subject: [PATCH] fix: generating specs on laravel 11 --- .../Model/BaseModelComponentBuilder.php | 10 +-- .../Model/ModelResourceComponentBuilder.php | 6 +- src/Specs/Builders/PropertyBuilder.php | 11 ++- src/Specs/Managers/SchemaManager.php | 70 ++++++++++--------- 4 files changed, 51 insertions(+), 46 deletions(-) diff --git a/src/Specs/Builders/Components/Model/BaseModelComponentBuilder.php b/src/Specs/Builders/Components/Model/BaseModelComponentBuilder.php index 1e4de572..40826905 100644 --- a/src/Specs/Builders/Components/Model/BaseModelComponentBuilder.php +++ b/src/Specs/Builders/Components/Model/BaseModelComponentBuilder.php @@ -48,15 +48,15 @@ protected function getPropertiesFromSchema(Model $resourceModel, array $excluded return collect($columns) ->filter( - function (Column $column) use ($excludedColumns) { - return !in_array($column->getName(), $excludedColumns, true); + function (array $column) use ($excludedColumns) { + return !in_array($column['name'], $excludedColumns, true); } )->filter( - function (Column $column) use ($resourceModel) { - return $resourceModel->isFillable($column->getName()); + function (array $column) use ($resourceModel) { + return $resourceModel->isFillable($column['name']); } )->map( - function (Column $column) use ($resourceModel) { + function (array $column) use ($resourceModel) { $propertyClass = $this->schemaManager->resolveSchemaPropertyClass($column, $resourceModel); return $this->propertyBuilder->build($column, $propertyClass); diff --git a/src/Specs/Builders/Components/Model/ModelResourceComponentBuilder.php b/src/Specs/Builders/Components/Model/ModelResourceComponentBuilder.php index d96fbedb..954c19d9 100644 --- a/src/Specs/Builders/Components/Model/ModelResourceComponentBuilder.php +++ b/src/Specs/Builders/Components/Model/ModelResourceComponentBuilder.php @@ -48,11 +48,11 @@ protected function getPropertiesFromSchema(Model $resourceModel): array return collect($columns) ->filter( - function (Column $column) use ($resourceModel) { - return !$resourceModel->isFillable($column->getName()); + function (array $column) use ($resourceModel) { + return !$resourceModel->isFillable($column['name']); } )->map( - function (Column $column) use ($resourceModel) { + function (array $column) use ($resourceModel) { $propertyClass = $this->schemaManager->resolveSchemaPropertyClass($column, $resourceModel); return $this->propertyBuilder->build($column, $propertyClass); diff --git a/src/Specs/Builders/PropertyBuilder.php b/src/Specs/Builders/PropertyBuilder.php index 8761a229..26b41129 100644 --- a/src/Specs/Builders/PropertyBuilder.php +++ b/src/Specs/Builders/PropertyBuilder.php @@ -4,23 +4,22 @@ namespace Orion\Specs\Builders; -use Doctrine\DBAL\Schema\Column; use Orion\ValueObjects\Specs\Schema\SchemaProperty; class PropertyBuilder { /** - * @param Column $column - * @param string|SchemaProperty $concretePropertyClass + * @param array $column + * @param string $concretePropertyClass * * @return SchemaProperty */ - public function build(Column $column, string $concretePropertyClass): SchemaProperty + public function build(array $column, string $concretePropertyClass): SchemaProperty { /** @var SchemaProperty $property */ $property = new $concretePropertyClass(); - $property->name = $column->getName(); - $property->nullable = !$column->getNotnull(); + $property->name = $column['name']; + $property->nullable = !$column['nullable']; return $property; } diff --git a/src/Specs/Managers/SchemaManager.php b/src/Specs/Managers/SchemaManager.php index 2e16da77..c754eb6b 100644 --- a/src/Specs/Managers/SchemaManager.php +++ b/src/Specs/Managers/SchemaManager.php @@ -6,7 +6,6 @@ use Doctrine\DBAL\Exception; use Doctrine\DBAL\Schema\Column; -use Doctrine\DBAL\Types\Types; use Illuminate\Database\Eloquent\Model; use Orion\ValueObjects\Specs\Schema\Properties\AnySchemaProperty; use Orion\ValueObjects\Specs\Schema\Properties\ArraySchemaProperty; @@ -27,61 +26,68 @@ class SchemaManager public function getSchemaColumns(Model $resourceModel): array { $table = $resourceModel->getConnection()->getTablePrefix().$resourceModel->getTable(); - $schema = $resourceModel->getConnection()->getDoctrineSchemaManager(); - - $databasePlatform = $schema->getDatabasePlatform(); - $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); $database = null; + if (strpos($table, '.')) { [$database, $table] = explode('.', $table); } - return $schema->listTableColumns($table, $database) ?? []; + if ((float) app()->version() < 11.0) { + $schema = $resourceModel->getConnection()->getDoctrineSchemaManager(); + + $databasePlatform = $schema->getDatabasePlatform(); + $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); + + return collect($schema->listTableColumns($table, $database))->map(function(Column $column) { + return [ + 'name' => $column->getName(), + 'type' => $column->getType()->getName(), + 'nullable' => !$column->getNotnull() + ]; + })->toArray(); + } + + return $resourceModel->getConnection()->getSchemaBuilder()->getColumns($table); } /** - * @param Column $column + * @param array $column * @param Model $resourceModel * @return string */ - public function resolveSchemaPropertyClass(Column $column, Model $resourceModel): string + public function resolveSchemaPropertyClass(array $column, Model $resourceModel): string { - if (in_array($column->getName(), $resourceModel->getDates(), true)) { + if (in_array($column['name'], $resourceModel->getDates(), true)) { return DateTimeSchemaProperty::class; } - switch ($column->getType()->getName()) { - case Types::BIGINT: - case Types::INTEGER: - case Types::SMALLINT: + switch ($column['type']) { + case strpos($column['type'], 'int') !== false: return IntegerSchemaProperty::class; - case Types::FLOAT: - case Types::DECIMAL: + case 'float': + case 'decimal': return NumberSchemaProperty::class; - case Types::BOOLEAN: + case strpos($column['type'], 'bool') !== false: return BooleanSchemaProperty::class; - case Types::STRING: - case Types::TEXT: - case Types::ASCII_STRING: - case Types::GUID: - case Types::TIME_MUTABLE: - case Types::TIME_IMMUTABLE: + case strpos($column['type'], 'char') !== false: + case strpos($column['type'], 'time') !== false: + case 'text': + case 'string': + case 'guid': return StringSchemaProperty::class; - case Types::DATE_MUTABLE: - case Types::DATE_IMMUTABLE: + case 'date': + case 'date_immutable': return DateSchemaProperty::class; - case Types::DATETIME_MUTABLE: - case Types::DATETIME_IMMUTABLE: + case 'datetime': + case 'datetime_immutable': return DateTimeSchemaProperty::class; - case Types::ARRAY: - case Types::SIMPLE_ARRAY: + case 'array': return ArraySchemaProperty::class; - case Types::OBJECT: - case Types::JSON: + case strpos($column['type'], 'json') !== false: return ObjectSchemaProperty::class; - case Types::BINARY: - case Types::BLOB: + case 'binary': + case 'blob': return BinarySchemaProperty::class; default: return AnySchemaProperty::class;