Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adetxt committed Apr 15, 2022
0 parents commit 59b760a
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
composer.lock
vendor
composer.phar
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Laravel Fillable Generator

This package provides a laravel artisan command to generate model fillable automaticly.

## Installation

This package can be installed through Composer.
```
composer require adetxt/laravel-fillable-generator --dev
```

## Usage

You can start generate your model fillable by this artisan command:
```
php artisan generate-model-fillable
```

### With Specific Model
```
php artisan generate-model-fillable User Post Category
```

### Options
```
-P, --path Model path (default: app\Models)
-E, --excludecol[=EXCLUDECOL] Exclude column [default: "id,created_at,updated_at,deleted_at"]
```

28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "adetxt/laravel-fillable-generator",
"description": "Generate model fillable automaticly",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Adetxt\\LaravelFillableGenerator\\": "src/"
}
},
"authors": [
{
"name": "Adeardo Dora Harnanda",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"nette/php-generator": "^3.6"
},
"extra": {
"laravel": {
"providers": [
"Adetxt\\LaravelFillableGenerator\\ServiceProvider"
]
}
}
}
119 changes: 119 additions & 0 deletions src/Console/Commands/GenerateModelFillable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Adetxt\LaravelFillableGenerator\Console\Commands;

use Adetxt\LaravelFillableGenerator\Printer\DefaultPrinter;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Nette\PhpGenerator\PhpFile;
use SplFileInfo;

class GenerateModelFillable extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generate-model-fillable
{names?*}
{--P|path : Model path (default: app\Models)}
{--E|excludecol=id,created_at,updated_at,deleted_at : Exclude column}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate model fillable from database table';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$names = $this->argument('names');
$error = 0;
$success = 0;

$excludecol = explode(',', $this->option('excludecol'));

$path = $this->option('path');
$path = empty($path) ? app_path('Models') : $path;

$this->line('Starting to sync model fillable...');

if (empty($names)) {
$this->info('No table name given. All tables will be processed.');
if (!$this->confirm('Do you wish to continue?')) {
return 1;
}
}

$names = empty($names) ? File::files($path) : $names;

if ($names[0] instanceof SplFileInfo) {
$names = collect($names)->map(function (SplFileInfo $i) {
return str_replace('.php', '', $i->getFilename());
})->toArray();
}

foreach ($names as $name) {
try {
$this->syncModelFillable($name, $path, $excludecol);

$this->info($name . ' generated');
$success++;
} catch (\Throwable $th) {
$this->error($name . ' failed to sync because ' . $th->getMessage());
$error++;
}
}

$this->comment(sprintf('Model fillable has been generated with : %d success, %d error', $success, $error));

return 0;
}

public function syncModelFillable($model_name, $path, $excludecol)
{
$fileName = $path . '/' . $model_name . '.php';

$class = PhpFile::fromCode(file_get_contents($fileName));

$ns = collect($class->getNamespaces())->first()->getName();
$model = $ns . '\\' . $model_name;

$columns = DB::getSchemaBuilder()->getColumnListing((new $model)->getTable());
$fillable = collect($columns)->filter(fn ($i) => !in_array($i, $excludecol))->values()->toArray();

$hasFillableProperties = $class->getClasses()[$model]->getProperties()['fillable'] ?? false;

if (!$hasFillableProperties) {
$class->getClasses()[$model]->addProperty('fillable', $fillable)->setProtected();
} else {
$class->getClasses()[$model]
->getProperties()['fillable']
->setValue($fillable);
}

$printer = new DefaultPrinter;

file_put_contents($fileName, $printer->printFile($class));

return true;
}
}
12 changes: 12 additions & 0 deletions src/Printer/DefaultPrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Adetxt\LaravelFillableGenerator\Printer;

use Nette\PhpGenerator\Printer;

class DefaultPrinter extends Printer
{
protected $indentation = " ";
protected $linesBetweenProperties = 1;
protected $linesBetweenMethods = 1;
}
33 changes: 33 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Adetxt\LaravelFillableGenerator;

use Adetxt\LaravelFillableGenerator\Console\Commands\GenerateModelFillable;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;

class ServiceProvider extends LaravelServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
GenerateModelFillable::class,
]);
}
}
}

0 comments on commit 59b760a

Please sign in to comment.