-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 59b760a
Showing
6 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
composer.lock | ||
vendor | ||
composer.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} | ||
} |