Skip to content

Commit

Permalink
Merge pull request #12 from mikejobrn/pluralize
Browse files Browse the repository at this point in the history
Definição de regras customizadas para o plural
  • Loading branch information
jeanjar authored Dec 21, 2016
2 parents 20a5d91 + 96ecb56 commit eba52d5
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions src/Console/Commands/Makes/MakeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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";
}

/**
Expand All @@ -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";
}

/**
Expand All @@ -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";
}

/**
Expand All @@ -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";
}
}
64 changes: 64 additions & 0 deletions src/Console/Commands/Makes/MakePlurals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace LAIS\Scaffold\Console\Commands\Makes;


use LAIS\Scaffold\Console\Commands\Scaffolding;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Arr;
use Doctrine\Common\Inflector\Inflector;

class MakePlurals
{
protected $scaffolding;
protected $files;

public function __construct(Scaffolding $scaffolding, Filesystem $files)
{
$this->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;
}
}
10 changes: 10 additions & 0 deletions src/Console/Commands/Scaffolding.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Console/Commands/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Model;

class {{class}} extends Model
{
protected $table = '{{tableName}}';

/**
* The attributes that are mass assignable.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Console/Commands/stubs/plurals.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'irregular' => array(
{{rules}}
)
];
34 changes: 34 additions & 0 deletions src/Providers/LaisServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace LAIS\Scaffold\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Config;
use Doctrine\Common\Inflector\Inflector;

class LaisServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$irregularInflectorRules = Config::get('plurals');
if(!is_null($irregularInflectorRules))
{
Inflector::rules('plural', $irregularInflectorRules);
}
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}

0 comments on commit eba52d5

Please sign in to comment.