-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from mikejobrn/pluralize
Definição de regras customizadas para o plural
- Loading branch information
Showing
7 changed files
with
140 additions
and
5 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
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
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,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; | ||
} | ||
} |
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
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
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,7 @@ | ||
<?php | ||
|
||
return [ | ||
'irregular' => array( | ||
{{rules}} | ||
) | ||
]; |
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,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() | ||
{ | ||
// | ||
} | ||
} |