3 changed files with 166 additions and 53 deletions
@ -0,0 +1,105 @@ |
|||
<?php |
|||
|
|||
namespace Luthfi\CrudGenerator\Generators; |
|||
|
|||
use Illuminate\Filesystem\Filesystem; |
|||
use Luthfi\CrudGenerator\CrudMake; |
|||
|
|||
/** |
|||
* Base Generator Class |
|||
*/ |
|||
abstract class BaseGenerator |
|||
{ |
|||
/** |
|||
* The injected Filesystem class |
|||
* |
|||
* @var Filesystem |
|||
*/ |
|||
protected $files; |
|||
|
|||
/** |
|||
* Array of defined model names |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $modelNames; |
|||
|
|||
/** |
|||
* Array of stub's model names |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $stubModelNames; |
|||
|
|||
/** |
|||
* The CrudMake class |
|||
* |
|||
* @var CrudMake |
|||
*/ |
|||
protected $command; |
|||
|
|||
public function __construct(Filesystem $files, array $modelNames, CrudMake $command) |
|||
{ |
|||
$this->files = $files; |
|||
|
|||
$this->command = $command; |
|||
|
|||
$this->modelNames = $modelNames; |
|||
|
|||
$this->stubModelNames = [ |
|||
'model_namespace' => 'mstrNmspc', |
|||
'full_model_name' => 'fullMstr', |
|||
'plural_model_name' => 'Masters', |
|||
'model_name' => 'Master', |
|||
'table_name' => 'masters', |
|||
'lang_name' => 'master', |
|||
'collection_model_var_name' => 'mstrCollections', |
|||
'single_model_var_name' => 'singleMstr', |
|||
]; |
|||
} |
|||
|
|||
/** |
|||
* Generate class file content |
|||
* |
|||
* @return void |
|||
*/ |
|||
abstract public function generate(); |
|||
|
|||
/** |
|||
* Make directory if the path is not exists |
|||
* @param string $path Absolute path of targetted directory |
|||
* @return string Absolute path |
|||
*/ |
|||
protected function makeDirectory($path) |
|||
{ |
|||
if (! $this->files->isDirectory($path)) { |
|||
$this->files->makeDirectory($path, 0777, true, true); |
|||
} |
|||
|
|||
return $path; |
|||
} |
|||
|
|||
/** |
|||
* Generate file on filesystem |
|||
* @param string $path Absoute path of file |
|||
* @param string $content Generated file content |
|||
* @return string Absolute path of file |
|||
*/ |
|||
protected function generateFile($path, $content) |
|||
{ |
|||
$this->files->put($path, $content); |
|||
|
|||
return $path; |
|||
} |
|||
|
|||
/** |
|||
* Replace all string of model names |
|||
* |
|||
* @param string $stub String of file or class stub with default content |
|||
* @return string Replaced content |
|||
*/ |
|||
protected function replaceStubString($stub) |
|||
{ |
|||
return str_replace($this->stubModelNames, $this->modelNames, $stub); |
|||
} |
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
<?php |
|||
|
|||
namespace Luthfi\CrudGenerator\Generators; |
|||
|
|||
/** |
|||
* Controller Generator Class |
|||
*/ |
|||
class ControllerGenerator extends BaseGenerator |
|||
{ |
|||
/** |
|||
* {@inheritDoc} |
|||
*/ |
|||
public function generate() |
|||
{ |
|||
$parentControllerDirectory = ''; |
|||
if (! is_null($this->command->option('parent'))) { |
|||
$parentControllerDirectory = '/'.$this->command->option('parent'); |
|||
} |
|||
$controllerPath = $this->makeDirectory(app_path('Http/Controllers'.$parentControllerDirectory)); |
|||
|
|||
$controllerPath = $controllerPath.'/'.$this->modelNames['plural_model_name'].'Controller.php'; |
|||
$this->generateFile($controllerPath, $this->getControllerContent()); |
|||
|
|||
$this->command->info($this->modelNames['plural_model_name'].'Controller generated.'); |
|||
} |
|||
|
|||
/** |
|||
* Get controller content from controller stub |
|||
* |
|||
* @return string Replaced proper model names in controller file content |
|||
*/ |
|||
public function getControllerContent() |
|||
{ |
|||
$stub = $this->files->get(__DIR__.'/../stubs/controller.model.stub'); |
|||
|
|||
$controllerFileContent = $this->replaceStubString($stub); |
|||
|
|||
if (! is_null($parentName = $this->command->option('parent'))) { |
|||
|
|||
$searches = [ |
|||
'App\Http\Controllers;', |
|||
"use {$this->modelNames['full_model_name']};\n" |
|||
]; |
|||
|
|||
$replacements = [ |
|||
"App\Http\Controllers\\{$parentName};", |
|||
"use {$this->modelNames['full_model_name']};\nuse App\Http\Controllers\Controller;\n" |
|||
]; |
|||
|
|||
$controllerFileContent = str_replace( |
|||
$searches, |
|||
$replacements, |
|||
$controllerFileContent |
|||
); |
|||
} |
|||
|
|||
return $controllerFileContent; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue