Browse Source

Cleanup classes

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
180c9c17cf
  1. 51
      src/CrudMake.php
  2. 47
      src/Generators/BaseGenerator.php
  3. 1
      src/Generators/ControllerGenerator.php
  4. 1
      src/Generators/FeatureTestGenerator.php
  5. 1
      src/Generators/WebRouteGenerator.php
  6. 2
      src/ServiceProvider.php

51
src/CrudMake.php

@ -40,6 +40,7 @@ class CrudMake extends Command
/**
* Construct CrudMake class
*
* @param Filesystem $files Put generated file content to application file system
*/
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)
{
$inputName = explode('/', ucfirst($modelName));
@ -135,6 +142,12 @@ class CrudMake extends Command
return implode('/', $inputName);
}
/**
* Get model namespace
*
* @param string $modelPath Model path
* @return string Model namespace
*/
protected function getModelNamespace($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'));
}
/**
* 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;
}
}

47
src/Generators/BaseGenerator.php

@ -44,37 +44,11 @@ abstract class BaseGenerator
$this->command = $command;
$this->getModelNames();
$this->modelNames = $this->command->modelNames;
$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
*
* @return array
@ -144,23 +118,4 @@ abstract class BaseGenerator
{
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;
}
}

1
src/Generators/ControllerGenerator.php

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

1
src/Generators/FeatureTestGenerator.php

@ -17,7 +17,6 @@ class FeatureTestGenerator extends BaseGenerator
$featureTestPath = $this->makeDirectory(base_path('tests/Feature'));
$this->generateFile("{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", $this->getContent());
$this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.');
}
/**

1
src/Generators/WebRouteGenerator.php

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

2
src/ServiceProvider.php

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