From 0eb6c08f0f14c38c9ab40e2d5fe239d040bb25d7 Mon Sep 17 00:00:00 2001 From: Fernando Piovezan Date: Mon, 22 Apr 2024 07:58:00 -0300 Subject: [PATCH 1/4] Adjust packages version --- composer.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index b08a8f29..ab3a8ebc 100644 --- a/composer.json +++ b/composer.json @@ -23,18 +23,18 @@ }], "require": { "php": "^8.1.0", - "illuminate/support": "^10.0", - "illuminate/console": "^10.0", + "illuminate/support": "^10.0|^11.0", + "illuminate/console": "^10.0|^11.0", "laracasts/flash": "^3.2.2", "laravelcollective/html": "^6.4", - "symfony/var-exporter": "^6.2.5" + "symfony/var-exporter": "^6.2.5|^7.0" }, "require-dev": { "phpunit/phpunit": "^10.0.7", "mockery/mockery": "^1.5.1", - "orchestra/testbench": "^8.0.0", - "pestphp/pest": "2.x-dev", - "pestphp/pest-plugin-laravel": "2.x-dev" + "orchestra/testbench": "^8.0.0|^9.0", + "pestphp/pest": "2.x-dev|^2.34", + "pestphp/pest-plugin-laravel": "2.x-dev|^2.3" }, "autoload": { "psr-4": { From fc8aa701fe42fcfefeb39e7501dd80dde68ee92f Mon Sep 17 00:00:00 2001 From: Fernando Piovezan Date: Mon, 22 Apr 2024 07:58:20 -0300 Subject: [PATCH 2/4] Create helpers methods --- src/helpers.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/helpers.php b/src/helpers.php index 72c2a321..c7190093 100755 --- a/src/helpers.php +++ b/src/helpers.php @@ -74,3 +74,27 @@ function create_resource_route_names($name, $isScaffold = false): array return $result; } } + +if (!function_exists('get_field_length')) { + function get_field_length($type): string + { + preg_match('/\(\s*(\d+(?:,\s*\d+)*)\s*\)/', $type, $matches); + return $matches[1] ?? 0; + } +} + +if (!function_exists('get_field_precision')) { + function get_field_precision($length): int + { + $precision = explode(',', $length); + return $precision[0] ?? 0; + } +} + +if (!function_exists('get_field_scale')) { + function get_field_scale($length): int + { + $precision = explode(',', $length); + return $precision[1] ?? 0; + } +} From 51ab08acab235ee22893df150abf1c7a2857ed1d Mon Sep 17 00:00:00 2001 From: Fernando Piovezan Date: Mon, 22 Apr 2024 07:59:02 -0300 Subject: [PATCH 3/4] remove dependency on doctrine --- src/Generators/ModelGenerator.php | 6 +- src/Utils/TableFieldsGenerator.php | 92 +++++++++++++----------------- 2 files changed, 44 insertions(+), 54 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 14c97c9b..80ca1b24 100755 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -203,8 +203,10 @@ protected function generateRules(): array $rule[] = 'string'; // Enforce a maximum string length if possible. - if ((int) $field->fieldDetails->getLength() > 0) { - $rule[] = 'max:'.$field->fieldDetails->getLength(); + + $length = get_field_length($field->fieldDetails['type']); + if ((int) $length > 1) { + $rule[] = 'max:'.$length; } break; } diff --git a/src/Utils/TableFieldsGenerator.php b/src/Utils/TableFieldsGenerator.php index 59571e73..ddb2250a 100755 --- a/src/Utils/TableFieldsGenerator.php +++ b/src/Utils/TableFieldsGenerator.php @@ -5,7 +5,9 @@ use DB; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Column; +use Illuminate\Support\Facades\Schema; use Illuminate\Support\Str; +use InfyOm\Generator\Common\GeneratorConfig; use InfyOm\Generator\Common\GeneratorField; use InfyOm\Generator\Common\GeneratorFieldRelation; @@ -59,39 +61,17 @@ class TableFieldsGenerator /** @var \Doctrine\DBAL\Schema\Table */ public $tableDetails; + public GeneratorConfig $config; + public function __construct($tableName, $ignoredFields, $connection = '') { $this->tableName = $tableName; $this->ignoredFields = $ignoredFields; - if (!empty($connection)) { - $this->schemaManager = DB::connection($connection)->getDoctrineSchemaManager(); - } else { - $this->schemaManager = DB::getDoctrineSchemaManager(); - } - - $platform = $this->schemaManager->getDatabasePlatform(); - $defaultMappings = [ - 'enum' => 'string', - 'json' => 'text', - 'bit' => 'boolean', - ]; - -// $this->tableDetails = $this->schemaManager->listTableDetails($this->tableName); - - $mappings = config('laravel_generator.from_table.doctrine_mappings', []); - $mappings = array_merge($mappings, $defaultMappings); - foreach ($mappings as $dbType => $doctrineType) { - $platform->registerDoctrineTypeMapping($dbType, $doctrineType); - } - // Added - $this->tableDetails = $this->schemaManager->listTableDetails($this->tableName); - - $columns = $this->schemaManager->listTableColumns($tableName); - + $columns = Schema::getColumns($tableName); $this->columns = []; foreach ($columns as $column) { - if (!in_array($column->getName(), $ignoredFields)) { + if (!in_array($column['name'], $ignoredFields)) { $this->columns[] = $column; } } @@ -107,7 +87,7 @@ public function __construct($tableName, $ignoredFields, $connection = '') public function prepareFieldsFromTable() { foreach ($this->columns as $column) { - $type = $column->getType()->getName(); + $type = $column['type_name']; switch ($type) { case 'integer': @@ -120,7 +100,7 @@ public function prepareFieldsFromTable() $field = $this->generateIntFieldInput($column, 'bigInteger'); break; case 'boolean': - $name = Str::title(str_replace('_', ' ', $column->getName())); + $name = Str::title(str_replace('_', ' ', $column['name'])); $field = $this->generateField($column, 'boolean', 'checkbox'); break; case 'datetime': @@ -160,8 +140,8 @@ public function prepareFieldsFromTable() $field->inIndex = false; $field->inView = false; } - $field->isNotNull = $column->getNotNull(); - $field->description = $column->getComment() ?? ''; // get comments from table + $field->isNotNull = !$column['nullable']; + $field->description = $column['comment'] ?? ''; // get comments from table $this->fields[] = $field; } @@ -176,9 +156,14 @@ public function prepareFieldsFromTable() */ public function getPrimaryKeyOfTable($tableName) { - $column = $this->schemaManager->listTableDetails($tableName)->getPrimaryKey(); + $column = ''; + foreach (Schema::getIndexes($tableName) as $index) { + if ($index['primary']) { + $column = $index['columns'][0]; + } + } - return $column ? $column->getColumns()[0] : ''; + return $column; } /** @@ -210,17 +195,17 @@ public static function getTimestampFieldNames() private function generateIntFieldInput($column, $dbType) { $field = new GeneratorField(); - $field->name = $column->getName(); + $field->name = $column['name']; $field->parseDBType($dbType); $field->htmlType = 'number'; - if ($column->getAutoincrement()) { + if ($column['auto_increment']) { $field->dbType .= ',true'; } else { $field->dbType .= ',false'; } - if ($column->getUnsigned()) { + if (str_contains($column['type'], 'unsigned')) { $field->dbType .= ',true'; } @@ -260,8 +245,8 @@ private function checkForPrimary(GeneratorField $field) private function generateField($column, $dbType, $htmlType) { $field = new GeneratorField(); - $field->name = $column->getName(); - $field->fieldDetails = $this->tableDetails->getColumn($field->name); + $field->name = $column['name']; + $field->fieldDetails = $column; $field->parseDBType($dbType); //, $column); TODO: handle column param $field->parseHtmlInput($htmlType); @@ -279,12 +264,13 @@ private function generateField($column, $dbType, $htmlType) private function generateNumberInput($column, $dbType) { $field = new GeneratorField(); - $field->name = $column->getName(); - $field->parseDBType($dbType.','.$column->getPrecision().','.$column->getScale()); + $field->name = $column['name']; + $length = get_field_length($column['type']); + $field->parseDBType($dbType . ',' . get_field_precision($length) . ',' . get_field_scale($length)); $field->htmlType = 'number'; if ($dbType === 'decimal') { - $field->numberDecimalPoints = $column->getScale(); + $field->numberDecimalPoints = explode(',', $length)[1]; } return $this->checkForPrimary($field); @@ -306,25 +292,27 @@ public function prepareRelations() */ public function prepareForeignKeys() { - $tables = $this->schemaManager->listTables(); + $tables = Schema::getTables(); $fields = []; foreach ($tables as $table) { - $primaryKey = $table->getPrimaryKey(); - if ($primaryKey) { - $primaryKey = $primaryKey->getColumns()[0]; + $primaryKey = ''; + foreach (Schema::getIndexes($table['name']) as $index) { + if ($index['primary']) { + $primaryKey = $index['columns'][0]; + } } $formattedForeignKeys = []; - $tableForeignKeys = $table->getForeignKeys(); + $tableForeignKeys = Schema::getForeignKeys($table['name']); foreach ($tableForeignKeys as $tableForeignKey) { $generatorForeignKey = new GeneratorForeignKey(); - $generatorForeignKey->name = $tableForeignKey->getName(); - $generatorForeignKey->localField = $tableForeignKey->getLocalColumns()[0]; - $generatorForeignKey->foreignField = $tableForeignKey->getForeignColumns()[0]; - $generatorForeignKey->foreignTable = $tableForeignKey->getForeignTableName(); - $generatorForeignKey->onUpdate = $tableForeignKey->onUpdate(); - $generatorForeignKey->onDelete = $tableForeignKey->onDelete(); + $generatorForeignKey->name = $tableForeignKey['name']; + $generatorForeignKey->localField = $tableForeignKey['columns'][0]; + $generatorForeignKey->foreignField = $tableForeignKey['foreign_columns'][0]; + $generatorForeignKey->foreignTable = $tableForeignKey['foreign_table']; + $generatorForeignKey->onUpdate = $tableForeignKey['on_update']; + $generatorForeignKey->onDelete = $tableForeignKey['on_delete']; $formattedForeignKeys[] = $generatorForeignKey; } @@ -333,7 +321,7 @@ public function prepareForeignKeys() $generatorTable->primaryKey = $primaryKey; $generatorTable->foreignKeys = $formattedForeignKeys; - $fields[$table->getName()] = $generatorTable; + $fields[$table['name']] = $generatorTable; } return $fields; From 4e8a6d65a5280b78fa5a5056af4a9f721656056c Mon Sep 17 00:00:00 2001 From: Fernando Piovezan Date: Mon, 22 Apr 2024 08:17:07 -0300 Subject: [PATCH 4/4] adjust style --- src/Generators/ModelGenerator.php | 2 +- src/Utils/TableFieldsGenerator.php | 3 +-- src/helpers.php | 3 +++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 80ca1b24..ae884066 100755 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -204,7 +204,7 @@ protected function generateRules(): array // Enforce a maximum string length if possible. - $length = get_field_length($field->fieldDetails['type']); + $length = get_field_length($field->fieldDetails['type']); if ((int) $length > 1) { $rule[] = 'max:'.$length; } diff --git a/src/Utils/TableFieldsGenerator.php b/src/Utils/TableFieldsGenerator.php index ddb2250a..252ef16d 100755 --- a/src/Utils/TableFieldsGenerator.php +++ b/src/Utils/TableFieldsGenerator.php @@ -2,7 +2,6 @@ namespace InfyOm\Generator\Utils; -use DB; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Column; use Illuminate\Support\Facades\Schema; @@ -266,7 +265,7 @@ private function generateNumberInput($column, $dbType) $field = new GeneratorField(); $field->name = $column['name']; $length = get_field_length($column['type']); - $field->parseDBType($dbType . ',' . get_field_precision($length) . ',' . get_field_scale($length)); + $field->parseDBType($dbType.','.get_field_precision($length).','.get_field_scale($length)); $field->htmlType = 'number'; if ($dbType === 'decimal') { diff --git a/src/helpers.php b/src/helpers.php index c7190093..773b5d22 100755 --- a/src/helpers.php +++ b/src/helpers.php @@ -79,6 +79,7 @@ function create_resource_route_names($name, $isScaffold = false): array function get_field_length($type): string { preg_match('/\(\s*(\d+(?:,\s*\d+)*)\s*\)/', $type, $matches); + return $matches[1] ?? 0; } } @@ -87,6 +88,7 @@ function get_field_length($type): string function get_field_precision($length): int { $precision = explode(',', $length); + return $precision[0] ?? 0; } } @@ -95,6 +97,7 @@ function get_field_precision($length): int function get_field_scale($length): int { $precision = explode(',', $length); + return $precision[1] ?? 0; } }