Skip to content
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
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea
vendor
.idea/
.php-cs-fixer.cache
.phpunit.result.cache
.psalm-cache/
composer.lock
.phpunit.result.cache
vendor/
95 changes: 95 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

// vim: expandtab syntax=php

declare(strict_types=1);

$finder = Symfony\Component\Finder\Finder::create()
->exclude('tests/integration/data')
->ignoreDotFiles(true)
->ignoreVCS(true)
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php');
;

$config = new PhpCsFixer\Config();
$config
->setRiskyAllowed(true)
->setRules([
'@PSR12' => true,
'@PSR2' => true,
//'@PhpCsFixer:risky' => true,
'align_multiline_comment' => true,
'array_indentation' => true,

'array_syntax' => [
'syntax' => 'short',
],

'binary_operator_spaces' => [
'default' => 'align_single_space_minimal',
],

'blank_line_after_namespace' => true,

'blank_line_before_statement' => [
'statements' => [
//'break',
//'continue',
'declare',
'return',
//'throw',
'switch',
'try',
],
],

'clean_namespace' => true,

'concat_space' => [
'spacing' => 'one',
],

'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],

'no_break_comment' => false,
'no_empty_comment' => true,
'no_superfluous_phpdoc_tags' => false,
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,

'ordered_imports' => [
'sort_algorithm' => 'alpha',
],

'phpdoc_align' => true,
'phpdoc_scalar' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => false,
'phpdoc_var_without_name' => true,

'phpdoc_types_order' => [
'null_adjustment' => 'always_last',
'sort_algorithm' => 'none',
],

//'single_line_comment_spacing' => true,
//'single_line_comment_style' => true,

'trailing_comma_in_multiline' => [
'elements' => [
'arrays',
],
],

'unary_operator_spaces' => true,
])
->setFinder($finder);

return $config;
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
CHANGELOG
=========

2025-01-28, v2.0.3
-----------------
* Do not connect to database in command constructors

2023-11-25, v2.0.2
-----------------
* Add Laravel 10 support
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(forked from [krlove/eloquent-model-generator](https://github.com/krlove/eloquent-model-generator))

# Eloquent Model Generator

Eloquent Model Generator generates Eloquent models using database schema as a source.
Expand All @@ -8,7 +10,7 @@ Version 2.0.0 has been released. Checkout [this discussion](https://github.com/k
## Installation
Step 1. Add Eloquent Model Generator to your project:
```
composer require krlove/eloquent-model-generator --dev
composer require --dev magentron/eloquent-model-generator
```
Step 2. Register `GeneratorServiceProvider`:
```php
Expand Down
27 changes: 19 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "krlove/eloquent-model-generator",
"description": "Eloquent Model Generator",
"version": "2.0.2",
"name": "magentron/eloquent-model-generator",
"description": "Eloquent Model Generator (forked from krlove/eloquent-model-generator",
"license": "MIT",
"autoload": {
"psr-4": {
Expand All @@ -13,18 +12,30 @@
"Krlove\\Tests\\Integration\\": "tests/integration"
}
},
"replace": {
"krlove/eloquent-model-generator": "<=2.0.2"
},
"require": {
"php": "^8.0",
"illuminate/database": "^9.0 || ^10.0",
"illuminate/support": "^9.0 || ^10.0",
"illuminate/config": "^9.0 || ^10.0",
"illuminate/console": "^9.0 || ^10.0",
"illuminate/database": "^12.0",
"illuminate/support": "^12.0",
"illuminate/config": "^12.0",
"illuminate/console": "^12.0",
"doctrine/dbal": "^3.0",
"krlove/code-generator": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"friendsofphp/php-cs-fixer": "^3.70",
"larastan/larastan": "^3.1",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^12",
"symfony/var-dumper": "^7.2",
"vimeo/psalm": "~6.4.0"
},
"config": {
"optimize-autoloader": true,
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
Expand Down
49 changes: 49 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0"?>
<psalm
cacheDirectory=".psalm-cache"
errorLevel="7"
findUnusedCode="false"
findUnusedBaselineEntry="true"
findUnusedIssueHandlerSuppression="false"
phpVersion="8.3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<!-- @TODO -->
<!--
-->
<directory name="tests" />
<ignoreFiles>
<!-- @TODO -->
<!--
<directory name="tests" />
-->
<directory name="vendor" />
<directory name="tests/integration/resources" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<UndefinedFunction>
<errorLevel type="suppress">
<referencedFunction name="app" />

<file name="src/Command/GenerateCommandTrait.php" />
</errorLevel>
</UndefinedFunction>

<UndefinedFunction>
<errorLevel type="suppress">
<referencedFunction name="config" />

<file name="src/Command/GenerateCommandTrait.php" />
<file name="src/EventListener/GenerateCommandEventListener.php" />
</errorLevel>
</UndefinedFunction>
</issueHandlers>

</psalm>
18 changes: 12 additions & 6 deletions src/Command/GenerateCommandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Krlove\EloquentModelGenerator\Command;

use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Model;
use Krlove\EloquentModelGenerator\Config\Config;
use Krlove\EloquentModelGenerator\Exception\GeneratorException;
Expand All @@ -26,7 +27,7 @@ protected function saveModel(EloquentModel $model): void
$content = $model->render();

$outputFilepath = $this->resolveOutputPath() . '/' . $model->getName()->getName() . '.php';
if (!$this->option('no-backup') && file_exists($outputFilepath)) {
if (! $this->option('no-backup') && file_exists($outputFilepath)) {
rename($outputFilepath, $outputFilepath . '~');
}
file_put_contents($outputFilepath, $content);
Expand All @@ -37,17 +38,17 @@ protected function resolveOutputPath(): string
$path = $this->option('output-path');
if ($path === null) {
$path = app()->path('Models');
} elseif (!str_starts_with($path, '/')) {
} elseif (! str_starts_with($path, '/')) {
$path = app()->path($path);
}

if (!is_dir($path)) {
if (!mkdir($path, 0777, true)) {
if (! is_dir($path)) {
if (! mkdir($path, 0777, true)) {
throw new GeneratorException(sprintf('Could not create directory %s', $path));
}
}

if (!is_writeable($path)) {
if (! is_writeable($path)) {
throw new GeneratorException(sprintf('%s is not writeable', $path));
}

Expand All @@ -67,4 +68,9 @@ protected function getCommonOptions(): array
['no-backup', 'b', InputOption::VALUE_OPTIONAL, 'Backup existing model', config('eloquent_model_generator.no_backup', false)],
];
}
}

protected function resolve($abstract)
{
return Container::getInstance()->make($abstract);
}
}
12 changes: 5 additions & 7 deletions src/Command/GenerateModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ class GenerateModelCommand extends Command

protected $name = 'krlove:generate:model';

public function __construct(private Generator $generator, private DatabaseManager $databaseManager)
{
parent::__construct();
}

public function handle()
{
$generator = $this->resolve(Generator::class);
$databaseManager = $this->resolve(DatabaseManager::class);

$config = $this->createConfig();
$config->setClassName($this->argument('class-name'));
Prefix::setPrefix($this->databaseManager->connection($config->getConnection())->getTablePrefix());
Prefix::setPrefix($databaseManager->connection($config->getConnection())->getTablePrefix());

$model = $this->generator->generateModel($config);
$model = $generator->generateModel($config);
$this->saveModel($model);

$this->output->writeln(sprintf('Model %s generated', $model->getName()->getName()));
Expand Down
22 changes: 11 additions & 11 deletions src/Command/GenerateModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Krlove\EloquentModelGenerator\Command;

use Illuminate\Config\Repository as AppConfig;
use Illuminate\Console\Command;
use Illuminate\Database\DatabaseManager;
use Krlove\EloquentModelGenerator\Generator;
Expand All @@ -16,27 +15,28 @@ class GenerateModelsCommand extends Command

protected $name = 'krlove:generate:models';

public function __construct(private Generator $generator, private DatabaseManager $databaseManager)
{
parent::__construct();
}

public function handle()
{
$generator = $this->resolve(Generator::class);
$databaseManager = $this->resolve(DatabaseManager::class);

$config = $this->createConfig();
Prefix::setPrefix($this->databaseManager->connection($config->getConnection())->getTablePrefix());
Prefix::setPrefix($databaseManager->connection($config->getConnection())->getTablePrefix());

$connection = $databaseManager->connection($config->getConnection());
$schemaBuilder = $connection->getSchemaBuilder();

$schemaManager = $this->databaseManager->connection($config->getConnection())->getDoctrineSchemaManager();
$tables = $schemaManager->listTables();
$tables = $schemaBuilder->getTableListing(null, false);
$skipTables = $this->option('skip-table');

foreach ($tables as $table) {
$tableName = Prefix::remove($table->getName());
if (in_array($tableName, $skipTables)) {
continue;
}

$config->setClassName(EmgHelper::getClassNameByTableName($tableName));
$model = $this->generator->generateModel($config);
$model = $generator->generateModel($config);
$this->saveModel($model);

$this->output->writeln(sprintf('Model %s generated', $model->getName()->getName()));
Expand All @@ -52,4 +52,4 @@ protected function getOptions()
],
);
}
}
}
12 changes: 6 additions & 6 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

class Config
{
private ?string $className = null;
private ?string $tableName = null;
private ?string $namespace = null;
private ?string $className = null;
private ?string $tableName = null;
private ?string $namespace = null;
private ?string $baseClassName = null;
private ?bool $noTimestamps = null;
private ?string $dateFormat = null;
private ?string $connection = null;
private ?bool $noTimestamps = null;
private ?string $dateFormat = null;
private ?string $connection = null;

public function getClassName(): ?string
{
Expand Down
Loading