Browse Source

Add model generator class

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
02a16ee48f
  1. 29
      src/CrudMake.php
  2. 7
      src/Generators/BaseGenerator.php
  3. 8
      src/Generators/ControllerGenerator.php
  4. 31
      src/Generators/ModelGenerator.php

29
src/CrudMake.php

@ -5,6 +5,7 @@ namespace Luthfi\CrudGenerator;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Luthfi\CrudGenerator\Generators\ControllerGenerator;
use Luthfi\CrudGenerator\Generators\ModelGenerator;
class CrudMake extends Command
{
@ -77,7 +78,7 @@ class CrudMake extends Command
if (! $this->modelExists()) {
$this->generateResourceRoute();
$this->generateModel();
app(ModelGenerator::class, ['command' => $this])->generate();
$this->generateMigration();
app(ControllerGenerator::class, ['command' => $this])->generate();
$this->generateViews();
@ -142,21 +143,6 @@ class CrudMake extends Command
}
/**
* Generate the model file
*
* @return void
*/
public function generateModel()
{
$modelPath = $this->modelNames['model_path'];
$modelDirectory = $this->makeDirectory(app_path($modelPath));
$this->generateFile($modelDirectory.'/'.$this->modelNames['model_name'].'.php', $this->getModelContent());
$this->info($this->modelNames['model_name'].' model generated.');
}
/**
* Generate migration file for the model
*
* @return void
@ -295,17 +281,6 @@ class CrudMake extends Command
}
/**
* Get model content from model stub
*
* @return string Replaced proper model names in model file content
*/
public function getModelContent()
{
$stub = $this->files->get(__DIR__.'/stubs/model.stub');
return $this->replaceStubString($stub);
}
/**
* Get migration file content from migration stub
*
* @return string Replaced proper model names in migration file content

7
src/Generators/BaseGenerator.php

@ -101,6 +101,13 @@ abstract class BaseGenerator
abstract public function generate();
/**
* Get class file content
*
* @return void
*/
abstract protected function getContent();
/**
* Make directory if the path is not exists
* @param string $path Absolute path of targetted directory
* @return string Absolute path

8
src/Generators/ControllerGenerator.php

@ -19,17 +19,15 @@ class ControllerGenerator extends BaseGenerator
$controllerPath = $this->makeDirectory(app_path('Http/Controllers'.$parentControllerDirectory));
$controllerPath = $controllerPath.'/'.$this->modelNames['plural_model_name'].'Controller.php';
$this->generateFile($controllerPath, $this->getControllerContent());
$this->generateFile($controllerPath, $this->getContent());
$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
* {@inheritDoc}
*/
public function getControllerContent()
public function getContent()
{
$stub = $this->files->get(__DIR__.'/../stubs/controller.model.stub');

31
src/Generators/ModelGenerator.php

@ -0,0 +1,31 @@
<?php
namespace Luthfi\CrudGenerator\Generators;
/**
* Controller Generator Class
*/
class ModelGenerator extends BaseGenerator
{
/**
* {@inheritDoc}
*/
public function generate()
{
$modelPath = $this->modelNames['model_path'];
$modelDirectory = $this->makeDirectory(app_path($modelPath));
$this->generateFile($modelDirectory.'/'.$this->modelNames['model_name'].'.php', $this->getContent());
$this->command->info($this->modelNames['model_name'].' model generated.');
}
/**
* {@inheritDoc}
*/
protected function getContent()
{
$stub = $this->files->get(__DIR__.'/../stubs/model.stub');
return $this->replaceStubString($stub);
}
}
Loading…
Cancel
Save