Browse Source

Cleanup classes

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
180c9c17cf
  1. 51
      src/CrudMake.php
  2. 49
      src/Generators/BaseGenerator.php
  3. 3
      src/Generators/ControllerGenerator.php
  4. 3
      src/Generators/FeatureTestGenerator.php
  5. 2
      src/Generators/FormViewGenerator.php
  6. 2
      src/Generators/IndexViewGenerator.php
  7. 2
      src/Generators/LangFileGenerator.php
  8. 2
      src/Generators/MigrationGenerator.php
  9. 2
      src/Generators/ModelFactoryGenerator.php
  10. 2
      src/Generators/ModelGenerator.php
  11. 2
      src/Generators/ModelTestGenerator.php
  12. 3
      src/Generators/WebRouteGenerator.php
  13. 2
      src/ServiceProvider.php

51
src/CrudMake.php

@ -40,6 +40,7 @@ class CrudMake extends Command
/** /**
* Construct CrudMake class * Construct CrudMake class
*
* @param Filesystem $files Put generated file content to application file system * @param Filesystem $files Put generated file content to application file system
*/ */
public function __construct(Filesystem $files) public function __construct(Filesystem $files)
@ -127,6 +128,12 @@ class CrudMake extends Command
]; ];
} }
/**
* Get model path on storage
*
* @param string $modelName Input model name from command argument
* @return string Model path on storage
*/
protected function getModelPath($modelName) protected function getModelPath($modelName)
{ {
$inputName = explode('/', ucfirst($modelName)); $inputName = explode('/', ucfirst($modelName));
@ -135,6 +142,12 @@ class CrudMake extends Command
return implode('/', $inputName); return implode('/', $inputName);
} }
/**
* Get model namespace
*
* @param string $modelPath Model path
* @return string Model namespace
*/
protected function getModelNamespace($modelPath) protected function getModelNamespace($modelPath)
{ {
$modelNamespace = str_replace('/', '\\', 'App/'.ucfirst($modelPath)); $modelNamespace = str_replace('/', '\\', 'App/'.ucfirst($modelPath));
@ -150,42 +163,4 @@ class CrudMake extends Command
{ {
return $this->files->exists(app_path($this->modelNames['model_name'].'.php')); return $this->files->exists(app_path($this->modelNames['model_name'].'.php'));
} }
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}
} }

49
src/Generators/BaseGenerator.php

@ -44,37 +44,11 @@ abstract class BaseGenerator
$this->command = $command; $this->command = $command;
$this->getModelNames();
$this->modelNames = $this->command->modelNames;
$this->getStubModelNames(); $this->getStubModelNames();
} }
/** /**
* Generate class properties for model names in different usage
*
* @return array
*/
public function getModelNames($modelName = null)
{
$modelName = is_null($modelName) ? $this->command->argument('name') : $modelName;
$model_name = ucfirst(class_basename($modelName));
$plural_model_name = str_plural($model_name);
$modelPath = $this->getModelPath($modelName);
$modelNamespace = $this->getModelNamespace($modelPath);
return $this->modelNames = [
'model_namespace' => $modelNamespace,
'full_model_name' => $modelNamespace.'\\'.$model_name,
'plural_model_name' => $plural_model_name,
'model_name' => $model_name,
'table_name' => snake_case($plural_model_name),
'lang_name' => snake_case($model_name),
'collection_model_var_name' => camel_case($plural_model_name),
'single_model_var_name' => camel_case($model_name),
'model_path' => $modelPath,
];
}
/**
* Get stub's model names * Get stub's model names
* *
* @return array * @return array
@ -144,23 +118,4 @@ abstract class BaseGenerator
{ {
return str_replace($this->stubModelNames, $this->command->modelNames, $stub); return str_replace($this->stubModelNames, $this->command->modelNames, $stub);
} }
/**
* Get model path on storage
* @param string $modelName Input model name from command argument
* @return string Model path on storage
*/
protected function getModelPath($modelName)
{
$inputName = explode('/', ucfirst($modelName));
array_pop($inputName);
return implode('/', $inputName);
}
protected function getModelNamespace($modelPath)
{
$modelNamespace = str_replace('/', '\\', 'App/'.ucfirst($modelPath));
return $modelNamespace == 'App\\' ? 'App' : $modelNamespace;
}
}
}

3
src/Generators/ControllerGenerator.php

@ -34,7 +34,6 @@ class ControllerGenerator extends BaseGenerator
$controllerFileContent = $this->replaceStubString($stub); $controllerFileContent = $this->replaceStubString($stub);
if (! is_null($parentName = $this->command->option('parent'))) { if (! is_null($parentName = $this->command->option('parent'))) {
$searches = [ $searches = [
'App\Http\Controllers;', 'App\Http\Controllers;',
"use {$this->modelNames['full_model_name']};\n" "use {$this->modelNames['full_model_name']};\n"
@ -54,4 +53,4 @@ class ControllerGenerator extends BaseGenerator
return $controllerFileContent; return $controllerFileContent;
} }
}
}

3
src/Generators/FeatureTestGenerator.php

@ -17,7 +17,6 @@ class FeatureTestGenerator extends BaseGenerator
$featureTestPath = $this->makeDirectory(base_path('tests/Feature')); $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());
$this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.'); $this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.');
} }
/** /**
@ -57,4 +56,4 @@ class FeatureTestGenerator extends BaseGenerator
{ {
return $this->files->get(__DIR__.'/../stubs/test-browserkit-base-class.stub'); return $this->files->get(__DIR__.'/../stubs/test-browserkit-base-class.stub');
} }
}
}

2
src/Generators/FormViewGenerator.php

@ -27,4 +27,4 @@ class FormViewGenerator extends BaseGenerator
$stub = $this->files->get(__DIR__.'/../stubs/view-forms.stub'); $stub = $this->files->get(__DIR__.'/../stubs/view-forms.stub');
return $this->replaceStubString($stub); return $this->replaceStubString($stub);
} }
}
}

2
src/Generators/IndexViewGenerator.php

@ -27,4 +27,4 @@ class IndexViewGenerator extends BaseGenerator
$stub = $this->files->get(__DIR__.'/../stubs/view-index.stub'); $stub = $this->files->get(__DIR__.'/../stubs/view-index.stub');
return $this->replaceStubString($stub); return $this->replaceStubString($stub);
} }
}
}

2
src/Generators/LangFileGenerator.php

@ -61,4 +61,4 @@ class LangFileGenerator extends BaseGenerator
{ {
return $this->files->get(__DIR__.'/../stubs/lang-app.stub'); return $this->files->get(__DIR__.'/../stubs/lang-app.stub');
} }
}
}

2
src/Generators/MigrationGenerator.php

@ -31,4 +31,4 @@ class MigrationGenerator extends BaseGenerator
$stub = $this->files->get(__DIR__.'/../stubs/migration-create.stub'); $stub = $this->files->get(__DIR__.'/../stubs/migration-create.stub');
return $this->replaceStubString($stub); return $this->replaceStubString($stub);
} }
}
}

2
src/Generators/ModelFactoryGenerator.php

@ -30,4 +30,4 @@ class ModelFactoryGenerator extends BaseGenerator
$stub = $this->files->get(__DIR__.'/../stubs/model-factory.stub'); $stub = $this->files->get(__DIR__.'/../stubs/model-factory.stub');
return $this->replaceStubString($stub); return $this->replaceStubString($stub);
} }
}
}

2
src/Generators/ModelGenerator.php

@ -28,4 +28,4 @@ class ModelGenerator extends BaseGenerator
$stub = $this->files->get(__DIR__.'/../stubs/model.stub'); $stub = $this->files->get(__DIR__.'/../stubs/model.stub');
return $this->replaceStubString($stub); return $this->replaceStubString($stub);
} }
}
}

2
src/Generators/ModelTestGenerator.php

@ -25,4 +25,4 @@ class ModelTestGenerator extends BaseGenerator
$stub = $this->files->get(__DIR__.'/../stubs/test-unit.stub'); $stub = $this->files->get(__DIR__.'/../stubs/test-unit.stub');
return $this->replaceStubString($stub); return $this->replaceStubString($stub);
} }
}
}

3
src/Generators/WebRouteGenerator.php

@ -28,7 +28,6 @@ class WebRouteGenerator extends BaseGenerator
$webRouteFileContent = $this->replaceStubString($stub); $webRouteFileContent = $this->replaceStubString($stub);
if (! is_null($parentName = $this->command->option('parent'))) { if (! is_null($parentName = $this->command->option('parent'))) {
$pluralModelName = $this->modelNames['plural_model_name']; $pluralModelName = $this->modelNames['plural_model_name'];
$webRouteFileContent = str_replace( $webRouteFileContent = str_replace(
@ -59,4 +58,4 @@ class WebRouteGenerator extends BaseGenerator
return $routeDirPath.'/'.$filename; return $routeDirPath.'/'.$filename;
} }
}
}

2
src/ServiceProvider.php

@ -20,6 +20,6 @@ class ServiceProvider extends BaseServiceProvider
public function boot() public function boot()
{ {
//
} }
} }
Loading…
Cancel
Save