Skip to content

Commit

Permalink
fix: Corrected SchemaFactoryMakeCommand.php to work with Laravel 11 (#15
Browse files Browse the repository at this point in the history
)

* fix: Corrected SchemaFactoryMakeCommand.php to work with Laravel 11

* fix: Update SchemaFactoryMakeCommand.php to work with Laravel 11

Changed way to get $default and $notNull, to be more widely compatible

* fix: Update SchemaFactoryMakeCommand.php to work with Laravel 11

Changed way to get $default and $notNull, to be more widely compatible and tested working

* fix: Update SchemaFactoryMakeCommand to improve new code readability

Refactored the code to use the schema builder to retrieve the columns of the table and iterate over each column to find the specific one we're interested in. This change ensures compatibility with Laravel 11 and provides a more widely compatible and tested solution.
  • Loading branch information
capimichi authored Jul 13, 2024
1 parent c96beaf commit b0f57cf
Showing 1 changed file with 69 additions and 47 deletions.
116 changes: 69 additions & 47 deletions src/Console/SchemaFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vyuldashev\LaravelOpenApi\Console;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\BooleanType;
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Types\DateType;
Expand All @@ -15,55 +14,78 @@
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;


class SchemaFactoryMakeCommand extends GeneratorCommand
{
protected $name = 'openapi:make-schema';
protected $description = 'Create a new Schema factory class';
protected $type = 'Schema';

protected function buildClass($name)
{
$output = parent::buildClass($name);
$output = str_replace('DummySchema', Str::replaceLast('Schema', '', class_basename($name)), $output);

if ($model = $this->option('model')) {
return $this->buildModel($output, $model);
}

return $output;
}

protected function buildModel($output, $model)
{
$appVersion = explode('.', app()::VERSION);
$namespace = $appVersion[0] >= 8 ? $this->laravel->getNamespace().'Models\\' : $this->laravel->getNamespace();
$namespace = $appVersion[0] >= 8 ? $this->laravel->getNamespace() . 'Models\\' : $this->laravel->getNamespace();
$model = Str::start($model, $namespace);

if (! is_a($model, Model::class, true)) {
if (!is_a($model, Model::class, true)) {
throw new InvalidArgumentException('Invalid model');
}

/** @var Model $model */
$model = app($model);

$columns = SchemaFacade::connection($model->getConnectionName())->getColumnListing(config('database.connections.'.config('database.default').'.prefix', '').$model->getTable());
$columns = SchemaFacade::connection($model->getConnectionName())->getColumnListing(config('database.connections.' . config('database.default') . '.prefix', '') . $model->getTable());
$connection = $model->getConnection();

$definition = 'return Schema::object(\''.class_basename($model).'\')'.PHP_EOL;
$definition .= ' ->properties('.PHP_EOL;


$tableName = config('database.connections.' . config('database.default') . '.prefix', '') . $model->getTable();

$connection->getSchemaGrammar();

$definition = 'return Schema::object(\'' . class_basename($model) . '\')' . PHP_EOL;
$definition .= ' ->properties(' . PHP_EOL;

$properties = collect($columns)
->map(static function ($column) use ($model, $connection) {
/** @var Column $column */
$column = $connection->getDoctrineColumn(config('database.connections.'.config('database.default').'.prefix', '').$model->getTable(), $column);
$name = $column->getName();
$default = $column->getDefault();
$notNull = $column->getNotnull();

switch (get_class($column->getType())) {
->map(static function ($columnName) use ($model, $connection, $tableName) {
$columnType = null;
$default = null;
$notNull = false;

// Use the schema builder to get the columns of the table
$tableColumns = $connection->getSchemaBuilder()->getColumns($tableName);

// Iterate over each column in the table
foreach ($tableColumns as $column) {
// Check if the current column is the one we're interested in
if ($column['name'] == $columnName) {
// If it is, then retrieve its type, default value, and nullability
// If these values are not set, provide a default value
$columnType = $column['type'] ?? "";
$defaultValue = $column['default'] ?? null;
$isNotNull = ($column['nullable'] ?? false) === false;

// Once we've found our column, we don't need to check the others
break;
}
}

$name = $columnName;

switch ($columnType) {
case IntegerType::class:
$format = 'Schema::integer(%s)->default(%s)';
$args = [$name, $notNull ? (int) $default : null];
$args = [$name, $notNull ? (int)$default : null];
break;
case BooleanType::class:
$format = 'Schema::boolean(%s)->default(%s)';
Expand All @@ -79,63 +101,63 @@ protected function buildModel($output, $model)
break;
case DecimalType::class:
$format = 'Schema::number(%s)->format(Schema::FORMAT_FLOAT)->default(%s)';
$args = [$name, $notNull ? (float) $default : null];
$args = [$name, $notNull ? (float)$default : null];
break;
default:
$format = 'Schema::string(%s)->default(%s)';
$args = [$name, $default];
break;
}

$args = array_map(static function ($value) {
if ($value === null) {
return 'null';
}

if (is_numeric($value)) {
return $value;
}

return '\''.$value.'\'';
return '\'' . $value . '\'';
}, $args);

$indentation = str_repeat(' ', 4);

return sprintf($indentation.$format, ...$args);
return sprintf($indentation . $format, ...$args);
})
->implode(','.PHP_EOL);

$definition .= $properties.PHP_EOL;
->implode(',' . PHP_EOL);
$definition .= $properties . PHP_EOL;
$definition .= ' );';

return str_replace('DummyDefinition', $definition, $output);
}

protected function getStub(): string
{
if ($this->option('model')) {
return __DIR__.'/stubs/schema.model.stub';
return __DIR__ . '/stubs/schema.model.stub';
}

return __DIR__.'/stubs/schema.stub';
return __DIR__ . '/stubs/schema.stub';
}

protected function getDefaultNamespace($rootNamespace): string
{
return $rootNamespace.'\OpenApi\Schemas';
return $rootNamespace . '\OpenApi\Schemas';
}

protected function qualifyClass($name): string
{
$name = parent::qualifyClass($name);

if (Str::endsWith($name, 'Schema')) {
return $name;
}

return $name.'Schema';
return $name . 'Schema';
}

protected function getOptions(): array
{
return [
Expand Down

0 comments on commit b0f57cf

Please sign in to comment.