Skip to content

Commit

Permalink
Revert "remove doctrine (#17)"
Browse files Browse the repository at this point in the history
This reverts commit c589173.
  • Loading branch information
TartanLeGrand authored Aug 1, 2024
1 parent c589173 commit fbf93af
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"require": {
"php": "^8.0",
"ext-json": "*",
"doctrine/dbal": "^2.6|^3.0",
"goldspecdigital/oooas": "^2.7.1",
"laravel/framework": "^11.0",
"laravel/framework": "5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"phpdocumentor/reflection-docblock": "^5.0"
},
"require-dev": {
Expand Down
72 changes: 40 additions & 32 deletions src/Console/SchemaFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Vyuldashev\LaravelOpenApi\Console;

use Doctrine\DBAL\Types\BooleanType;
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Types\DateType;
use Doctrine\DBAL\Types\DecimalType;
use Doctrine\DBAL\Types\IntegerType;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema as SchemaFacade;
Expand Down Expand Up @@ -41,60 +46,63 @@ protected function buildModel($output, $model)
/** @var Model $model */
$model = app($model);

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

$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 (array $column) {
$columnType = $column['type_name'];
$default = $column['default'] ?? null;
$notNull = ! $column['nullable'];
$name = $column['name'];
->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 'integer':
case 'bigint':
case 'smallint':
case IntegerType::class:
$format = 'Schema::integer(%s)->default(%s)';
$args = [$name, $notNull ? (int)$default : null];
break;
case 'boolean':
case BooleanType::class:
$format = 'Schema::boolean(%s)->default(%s)';
$args = [$name, $notNull ? $default : null];
break;
case 'date':
case 'date_immutable':
case DateType::class:
$format = 'Schema::string(%s)->format(Schema::FORMAT_DATE)->default(%s)';
$args = [$name, $notNull ? $default : null];
break;
case 'datetime':
case 'datetime_immutable':
case 'datetimetz':
case 'datetimetz_immutable':
case DateTimeType::class:
$format = 'Schema::string(%s)->format(Schema::FORMAT_DATE_TIME)->default(%s)';
$args = [$name, $notNull ? $default : null];
break;
case 'decimal':
case 'float':
case DecimalType::class:
$format = 'Schema::number(%s)->format(Schema::FORMAT_FLOAT)->default(%s)';
$args = [$name, $notNull ? (float)$default : null];
break;
case 'array':
case 'json':
$format = 'Schema::array(%s)->default(%s)';
$args = [$name, $notNull ? (array)$default : null];
break;
case 'guid':
case 'uuid':
$format = 'Schema::string(%s)->format(Schema::FORMAT_UUID)->default(%s)';
$args = [$name, $notNull ? (array)$default : null];
break;
case 'binary':
$format = 'Schema::string(%s)->format(Schema::FORMAT_BINARY)->default(%s)';
$args = [$name, $notNull ? $default : null];
break;
default:
$format = 'Schema::string(%s)->default(%s)';
$args = [$name, $default];
Expand Down

0 comments on commit fbf93af

Please sign in to comment.