From 79d032508b403c2cbb6a2d6d102c266b39e28fb7 Mon Sep 17 00:00:00 2001 From: Mike Job Date: Fri, 2 Dec 2016 12:59:21 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Defini=C3=A7=C3=A3o=20de=20regras=20customi?= =?UTF-8?q?zadas=20para=20o=20plural?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Console/Commands/Makes/MakeModel.php | 22 ++++++-- src/Console/Commands/Makes/MakePlurals.php | 64 ++++++++++++++++++++++ src/Console/Commands/Scaffolding.php | 10 ++++ src/Console/Commands/stubs/model.stub | 2 + src/Console/Commands/stubs/plurals.stub | 7 +++ src/Providers/LaisServiceProvider.php | 34 ++++++++++++ 6 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 src/Console/Commands/Makes/MakePlurals.php create mode 100644 src/Console/Commands/stubs/plurals.stub create mode 100644 src/Providers/LaisServiceProvider.php diff --git a/src/Console/Commands/Makes/MakeModel.php b/src/Console/Commands/Makes/MakeModel.php index aebfbbd..32114ec 100644 --- a/src/Console/Commands/Makes/MakeModel.php +++ b/src/Console/Commands/Makes/MakeModel.php @@ -75,7 +75,7 @@ protected function createModel($relationships) { $stub = $this->files->get(dirname(__DIR__) . '/stubs/model.stub'); - $this->replaceClassName($stub)->replaceFields($stub); + $this->replaceClassName($stub)->replaceTableName($stub)->replaceFields($stub); if(!empty($relationships)) { @@ -103,6 +103,18 @@ protected function replaceClassName(&$stub) return $this; } + /** + * Replace the table name in the stub. + * + * @param string $stub + * @return $this + */ + protected function replaceTableName(&$stub) + { + $stub = str_replace('{{tableName}}', str_plural(snake_case($this->className)), $stub); + return $this; + } + protected function getFields($schema) { $fields = (new CreateSchema)->getFields($schema); @@ -187,7 +199,7 @@ protected function replaceUseModels(&$stub) protected function relationHasOne($model) { $this->useModels .= "use " . \App::getNamespace() . $model . ";\n"; - return "\tpublic function " . strtolower($model) . "()" . "{\n" . "\t\treturn \$this->hasOne(" . $model . "::class);\n" . "\t}\n"; + return "\tpublic function " . camel_case($model) . "()" . "\n\t{\n" . "\t\treturn \$this->hasOne(" . $model . "::class);\n" . "\t}\n\n"; } /** @@ -199,7 +211,7 @@ protected function relationHasOne($model) protected function relationHasMany($model) { $this->useModels .= "use " . \App::getNamespace() . $model . ";\n"; - return "\tpublic function " . str_plural(strtolower($model)) . "()" . "{\n" . "\t\treturn \$this->hasMany(" . $model . "::class);\n" . "\t}\n"; + return "\tpublic function " . camel_case(str_plural(snake_case($model))) . "()" . "\n\t{\n" . "\t\treturn \$this->hasMany(" . $model . "::class);\n" . "\t}\n\n"; } /** @@ -211,7 +223,7 @@ protected function relationHasMany($model) protected function relationBelongsToMany($model) { $this->useModels .= "use " . \App::getNamespace() . $model . ";\n"; - return "\tpublic function " . str_plural(strtolower($model)) . "()" . "{\n" . "\t\treturn \$this->belongsToMany(" . $model . "::class);\n" . "\t}\n"; + return "\tpublic function " . camel_case(str_plural(snake_case($model))) . "()" . "\n\t{\n" . "\t\treturn \$this->belongsToMany(" . $model . "::class);\n" . "\t}\n\n"; } /** @@ -223,6 +235,6 @@ protected function relationBelongsToMany($model) protected function relationBelongsTo($model) { $this->useModels .= "use " . \App::getNamespace() . $model . ";\n"; - return "\tpublic function " . strtolower($model) . "()" . "{\n" . "\t\treturn \$this->belongsTo(" . $model . "::class);\n" . "\t}\n"; + return "\tpublic function " . camel_case($model) . "()" . "\n\t{\n" . "\t\treturn \$this->belongsTo(" . $model . "::class);\n" . "\t}\n\n"; } } diff --git a/src/Console/Commands/Makes/MakePlurals.php b/src/Console/Commands/Makes/MakePlurals.php new file mode 100644 index 0000000..c426fc5 --- /dev/null +++ b/src/Console/Commands/Makes/MakePlurals.php @@ -0,0 +1,64 @@ +files = $files; + $this->scaffolding = $scaffolding; + $this->start(); + } + + protected function start() + { + if(!Config::has('plurals.irregular.' . strtolower($this->scaffolding->singular))) + { + //Execute + file_put_contents(base_path('config/plurals.php'), $this->createPlurals(), FILE_TEXT); + $this->scaffolding->info('Plural criado com sucesso'); + } + } + + protected function createPlurals() + { + $plurals = Config::get('plurals.irregular'); + $plurals = Arr::add($plurals, strtolower($this->scaffolding->singular), strtolower($this->scaffolding->plural)); + + if(snake_case($this->scaffolding->singular) != strtolower($this->scaffolding->singular)) + { + $plurals = Arr::add($plurals, snake_case($this->scaffolding->singular), snake_case($this->scaffolding->plural)); + } + + Inflector::rules('plural', array('irregular' => $plurals)); + + $stub = $this->files->get(dirname(__DIR__) . '/stubs/plurals.stub'); + $this->replacePlurals($stub, $plurals); + + return $stub; + } + + protected function replacePlurals(&$stub, $plurals) + { + $rules = ''; + foreach($plurals as $key => $value) + { + $rules .= "\t\t'" . strtolower($key) . "' => '" . strtolower($value) . "',\n"; + } + + $stub = str_replace('{{rules}}', $rules, $stub); + + return $this; + } +} \ No newline at end of file diff --git a/src/Console/Commands/Scaffolding.php b/src/Console/Commands/Scaffolding.php index d7c169b..a36d55d 100644 --- a/src/Console/Commands/Scaffolding.php +++ b/src/Console/Commands/Scaffolding.php @@ -5,6 +5,7 @@ use LAIS\Scaffold\Console\Commands\Makes\MakeMigration; use LAIS\Scaffold\Console\Commands\Makes\MakeModel; use LAIS\Scaffold\Console\Commands\Makes\MakeController; +use LAIS\Scaffold\Console\Commands\Makes\MakePlurals; use LAIS\Scaffold\Console\Commands\Makes\MakeRoute; use LAIS\Scaffold\Console\Commands\Makes\MakeView; @@ -98,6 +99,7 @@ public function handle() // Start Scaffold $this->info('Configurando ' . $this->modelName . '...'); $this->info('Configurando ' . $this->schema . '...'); + $this->makePlurals(); $this->makeMigration(); $this->makeModel(); $this->makeController(); @@ -142,6 +144,14 @@ protected function makeView() new MakeView($this, $this->files); } + /** + * Generate config for plurals + */ + public function makePlurals() + { + new MakePlurals($this, $this->files); + } + public function getModelName() { return $this->modelName; diff --git a/src/Console/Commands/stubs/model.stub b/src/Console/Commands/stubs/model.stub index 914e832..45c80fc 100644 --- a/src/Console/Commands/stubs/model.stub +++ b/src/Console/Commands/stubs/model.stub @@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Model; class {{class}} extends Model { + protected $table = '{{tableName}}'; + /** * The attributes that are mass assignable. * diff --git a/src/Console/Commands/stubs/plurals.stub b/src/Console/Commands/stubs/plurals.stub new file mode 100644 index 0000000..281f4f2 --- /dev/null +++ b/src/Console/Commands/stubs/plurals.stub @@ -0,0 +1,7 @@ + array( +{{rules}} + ) +]; \ No newline at end of file diff --git a/src/Providers/LaisServiceProvider.php b/src/Providers/LaisServiceProvider.php new file mode 100644 index 0000000..fb298ca --- /dev/null +++ b/src/Providers/LaisServiceProvider.php @@ -0,0 +1,34 @@ + Date: Thu, 8 Dec 2016 17:08:07 -0300 Subject: [PATCH 2/2] =?UTF-8?q?Altera=C3=A7=C3=A3o=20do=20arquivo=20README?= =?UTF-8?q?=20com=20instru=C3=A7=C3=B5es=20do=20como=20registrar=20o=20pro?= =?UTF-8?q?vider=20do=20LaisScaffolder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index e10ccea..b001b41 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,12 @@ protected $commands = [ ]; ``` +**Registrando o provider no Laravel** +Abra o arquivo config/app.php e adicione a seguinte linha no array da opção `providers`: +```php +\LAIS\Scaffold\Providers\LaisServiceProvider::class, +``` + ### Como usar **Comando artisan** ```sh