Browse Source

Add controller generator class

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
98be84bb93
  1. 55
      src/CrudMake.php
  2. 105
      src/Generators/BaseGenerator.php
  3. 59
      src/Generators/ControllerGenerator.php

55
src/CrudMake.php

@ -4,6 +4,7 @@ namespace Luthfi\CrudGenerator;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Luthfi\CrudGenerator\Generators\ControllerGenerator;
class CrudMake extends Command
{
@ -78,7 +79,7 @@ class CrudMake extends Command
$this->generateModel();
$this->generateMigration();
$this->generateController();
app(ControllerGenerator::class, ['modelNames' => $this->modelNames, 'command' => $this])->generate();
$this->generateViews();
$this->generateLangFile();
$this->generateModelFactory();
@ -156,25 +157,6 @@ class CrudMake extends Command
}
/**
* Generate controller for model CRUD operation
*
* @return void
*/
public function generateController()
{
$parentControllerDirectory = '';
if (! is_null($this->option('parent'))) {
$parentControllerDirectory = '/'.$this->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->info($this->modelNames['plural_model_name'].'Controller generated.');
}
/**
* Generate migration file for the model
*
* @return void
@ -313,39 +295,6 @@ class CrudMake extends Command
}
/**
* 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->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;
}
/**
* Get model content from model stub
*
* @return string Replaced proper model names in model file content

105
src/Generators/BaseGenerator.php

@ -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);
}
}

59
src/Generators/ControllerGenerator.php

@ -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;
}
}
Loading…
Cancel
Save