Skip to content

Commit

Permalink
fix: generating specs on laravel 11
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzarbn committed Jun 14, 2024
1 parent 97215d3 commit e3e5b65
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions src/Specs/Builders/PropertyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
70 changes: 38 additions & 32 deletions src/Specs/Managers/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit e3e5b65

Please sign in to comment.