Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

L11 compability without doctrine #1101

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 4 additions & 2 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
93 changes: 40 additions & 53 deletions src/Utils/TableFieldsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace InfyOm\Generator\Utils;

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;

Expand Down Expand Up @@ -59,39 +60,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;
}
}
Expand All @@ -107,7 +86,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':
Expand All @@ -120,7 +99,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':
Expand Down Expand Up @@ -160,8 +139,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;
}
Expand All @@ -176,9 +155,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;
}

/**
Expand Down Expand Up @@ -210,17 +194,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';
}

Expand Down Expand Up @@ -260,8 +244,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);

Expand All @@ -279,12 +263,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);
Expand All @@ -306,25 +291,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;
}
Expand All @@ -333,7 +320,7 @@ public function prepareForeignKeys()
$generatorTable->primaryKey = $primaryKey;
$generatorTable->foreignKeys = $formattedForeignKeys;

$fields[$table->getName()] = $generatorTable;
$fields[$table['name']] = $generatorTable;
}

return $fields;
Expand Down
27 changes: 27 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,30 @@ 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;
}
}