Browse Source

Refactor file generator classes

tags/1.0.0
Nafies Luthfi 8 years ago
parent
commit
b64868b940
  1. 15
      src/Generators/BaseGenerator.php
  2. 6
      src/Generators/ControllerGenerator.php
  3. 11
      src/Generators/FeatureTestGenerator.php
  4. 11
      src/Generators/FormViewGenerator.php
  5. 11
      src/Generators/IndexViewGenerator.php
  6. 9
      src/Generators/LangFileGenerator.php
  7. 11
      src/Generators/MigrationGenerator.php
  8. 11
      src/Generators/ModelFactoryGenerator.php
  9. 14
      src/Generators/ModelGenerator.php
  10. 6
      src/Generators/ModelPolicyGenerator.php
  11. 15
      src/Generators/ModelPolicyTestGenerator.php
  12. 15
      src/Generators/ModelTestGenerator.php
  13. 7
      src/Generators/WebRouteGenerator.php

15
src/Generators/BaseGenerator.php

@ -80,12 +80,14 @@ abstract class BaseGenerator
/**
* Get class file content
*
* @param string $stubName Name of stub file
* @return void
*/
abstract protected function getContent();
abstract protected function getContent(string $stubName);
/**
* Make directory if the path is not exists
*
* @param string $path Absolute path of targetted directory
* @return string Absolute path
*/
@ -121,4 +123,15 @@ abstract class BaseGenerator
{
return str_replace($this->stubModelNames, $this->command->modelNames, $stub);
}
/**
* Get correct stub file content
*
* @param string $stubName The stub file name
* @return string The stub file content
*/
protected function getStubFileContent(string $stubName)
{
return $this->files->get(__DIR__.'/../stubs/'.$stubName.'.stub');
}
}

6
src/Generators/ControllerGenerator.php

@ -19,7 +19,7 @@ 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->getContent());
$this->generateFile($controllerPath, $this->getContent('controller.model'));
$this->command->info($this->modelNames['plural_model_name'].'Controller generated.');
}
@ -27,9 +27,9 @@ class ControllerGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function getContent()
public function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__.'/../stubs/controller.model.stub');
$stub = $this->getStubFileContent($stubName);
$controllerFileContent = $this->replaceStubString($stub);

11
src/Generators/FeatureTestGenerator.php

@ -15,16 +15,21 @@ class FeatureTestGenerator extends BaseGenerator
$this->createBrowserKitBaseTestClass();
$featureTestPath = $this->makeDirectory(base_path('tests/Feature'));
$this->generateFile("{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", $this->getContent());
$this->generateFile(
"{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php",
$this->getContent('test-feature')
);
$this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.');
}
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__.'/../stubs/test-feature.stub');
$stub = $this->getStubFileContent($stubName);
$baseTestClass = config('simple-crud.base_test_class');
$stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub);
return $this->replaceStubString($stub);

11
src/Generators/FormViewGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Form View Generator Class
*/
* Form View Generator Class
*/
class FormViewGenerator extends BaseGenerator
{
/**
@ -14,7 +14,7 @@ class FormViewGenerator extends BaseGenerator
{
$viewPath = $this->makeDirectory(resource_path('views/'.$this->modelNames['table_name']));
$this->generateFile($viewPath.'/forms.blade.php', $this->getContent());
$this->generateFile($viewPath.'/forms.blade.php', $this->getContent('view-forms'));
$this->command->info($this->modelNames['model_name'].' form view file generated.');
}
@ -22,9 +22,8 @@ class FormViewGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__.'/../stubs/view-forms.stub');
return $this->replaceStubString($stub);
return $this->replaceStubString($this->getStubFileContent($stubName));
}
}

11
src/Generators/IndexViewGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Index View Generator Class
*/
* Index View Generator Class
*/
class IndexViewGenerator extends BaseGenerator
{
/**
@ -14,7 +14,7 @@ class IndexViewGenerator extends BaseGenerator
{
$viewPath = $this->makeDirectory(resource_path('views/'.$this->modelNames['table_name']));
$this->generateFile($viewPath.'/index.blade.php', $this->getContent());
$this->generateFile($viewPath.'/index.blade.php', $this->getContent('view-index'));
$this->command->info($this->modelNames['model_name'].' index view file generated.');
}
@ -22,9 +22,8 @@ class IndexViewGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__.'/../stubs/view-index.stub');
return $this->replaceStubString($stub);
return $this->replaceStubString($this->getStubFileContent($stubName));
}
}

9
src/Generators/LangFileGenerator.php

@ -16,7 +16,8 @@ class LangFileGenerator extends BaseGenerator
$langPath = $this->makeDirectory(resource_path('lang/'.$locale));
$this->createAppLangFile($langPath);
$this->generateFile($langPath.'/'.$this->modelNames['lang_name'].'.php', $this->getContent());
$this->generateFile($langPath.'/'.$this->modelNames['lang_name'].'.php', $this->getContent('lang-'.$locale));
$this->command->info($this->modelNames['lang_name'].' lang files generated.');
}
@ -24,11 +25,9 @@ class LangFileGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$locale = config('app.locale');
$langStubPath = __DIR__.'/../stubs/lang-'.$locale.'.stub';
$langStubPath = __DIR__.'/../stubs/'.$stubName.'.stub';
if ($this->files->exists($langStubPath)) {
$stub = $this->files->get($langStubPath);

11
src/Generators/MigrationGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Migration Generator Class
*/
* Migration Generator Class
*/
class MigrationGenerator extends BaseGenerator
{
/**
@ -18,7 +18,7 @@ class MigrationGenerator extends BaseGenerator
$migrationPath = $this->makeDirectory(database_path('migrations'));
$migrationFilePath = $migrationPath.'/'.$prefix."_create_{$tableName}_table.php";
$this->generateFile($migrationFilePath, $this->getContent());
$this->generateFile($migrationFilePath, $this->getContent('migration-create'));
$this->command->info($this->modelNames['model_name'].' table migration generated.');
}
@ -26,9 +26,8 @@ class MigrationGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__.'/../stubs/migration-create.stub');
return $this->replaceStubString($stub);
return $this->replaceStubString($this->getStubFileContent($stubName));
}
}

11
src/Generators/ModelFactoryGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Model Factory Generator Class
*/
* Model Factory Generator Class
*/
class ModelFactoryGenerator extends BaseGenerator
{
/**
@ -16,7 +16,7 @@ class ModelFactoryGenerator extends BaseGenerator
$this->generateFile(
$modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php',
$this->getContent()
$this->getContent('model-factory')
);
$this->command->info($this->modelNames['model_name'].' model factory generated.');
@ -25,9 +25,8 @@ class ModelFactoryGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__.'/../stubs/model-factory.stub');
return $this->replaceStubString($stub);
return $this->replaceStubString($this->getStubFileContent($stubName));
}
}

14
src/Generators/ModelGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Model Generator Class
*/
* Model Generator Class
*/
class ModelGenerator extends BaseGenerator
{
/**
@ -15,7 +15,10 @@ class ModelGenerator extends BaseGenerator
$modelPath = $this->modelNames['model_path'];
$modelDirectory = $this->makeDirectory(app_path($modelPath));
$this->generateFile($modelDirectory.'/'.$this->modelNames['model_name'].'.php', $this->getContent());
$this->generateFile(
$modelDirectory.'/'.$this->modelNames['model_name'].'.php',
$this->getContent('model')
);
$this->command->info($this->modelNames['model_name'].' model generated.');
}
@ -23,9 +26,8 @@ class ModelGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__.'/../stubs/model.stub');
return $this->replaceStubString($stub);
return $this->replaceStubString($this->getStubFileContent($stubName));
}
}

6
src/Generators/ModelPolicyGenerator.php

@ -20,7 +20,7 @@ class ModelPolicyGenerator extends BaseGenerator
$this->generateFile(
$modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php',
$this->getContent()
$this->getContent('model-policy')
);
$this->command->info($this->modelNames['model_name'].' model policy generated.');
@ -31,9 +31,9 @@ class ModelPolicyGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__.'/../stubs/model-policy.stub');
$stub = $this->getStubFileContent($stubName);
$policyFileContent = $this->replaceStubString($stub);

15
src/Generators/ModelPolicyTestGenerator.php

@ -13,18 +13,23 @@ class ModelPolicyTestGenerator extends BaseGenerator
public function generate()
{
$modelPolicyTestPath = $this->makeDirectory(base_path('tests/Unit/Policies'));
$this->generateFile("{$modelPolicyTestPath}/{$this->modelNames['model_name']}PolicyTest.php", $this->getContent());
$this->command->info($this->modelNames['model_name'] . 'PolicyTest (model policy) generated.');
$this->generateFile(
"{$modelPolicyTestPath}/{$this->modelNames['model_name']}PolicyTest.php",
$this->getContent('test-policy')
);
$this->command->info($this->modelNames['model_name'].'PolicyTest (model policy) generated.');
}
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__ . '/../stubs/test-policy.stub');
$stub = $this->getStubFileContent($stubName);
$baseTestClass = config('simple-crud.base_test_class');
$stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub);
$stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub);
return $this->replaceStubString($stub);
}
}

15
src/Generators/ModelTestGenerator.php

@ -13,18 +13,23 @@ class ModelTestGenerator extends BaseGenerator
public function generate()
{
$unitTestPath = $this->makeDirectory(base_path('tests/Unit/Models'));
$this->generateFile("{$unitTestPath}/{$this->modelNames['model_name']}Test.php", $this->getContent());
$this->command->info($this->modelNames['model_name'] . 'Test (model) generated.');
$this->generateFile(
"{$unitTestPath}/{$this->modelNames['model_name']}Test.php",
$this->getContent('test-unit')
);
$this->command->info($this->modelNames['model_name'].'Test (model) generated.');
}
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__ . '/../stubs/test-unit.stub');
$stub = $this->getStubFileContent($stubName);
$baseTestClass = config('simple-crud.base_test_class');
$stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub);
$stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub);
return $this->replaceStubString($stub);
}
}

7
src/Generators/WebRouteGenerator.php

@ -13,7 +13,8 @@ class WebRouteGenerator extends BaseGenerator
public function generate()
{
$webRoutePath = $this->makeRouteFile(base_path('routes'), 'web.php');
$this->files->append($webRoutePath, $this->getContent());
$this->files->append($webRoutePath, $this->getContent('route-web'));
$this->command->info($this->modelNames['model_name'].' resource route generated on routes/web.php.');
}
@ -21,9 +22,9 @@ class WebRouteGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
protected function getContent()
protected function getContent(string $stubName)
{
$stub = $this->files->get(__DIR__.'/../stubs/route-web.stub');
$stub = $this->getStubFileContent($stubName);
$webRouteFileContent = $this->replaceStubString($stub);

Loading…
Cancel
Save