Browse Source

Add web route generator class

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
ae88fa7ccc
  1. 61
      src/CrudMake.php
  2. 2
      src/Generators/IndexViewGenerator.php
  3. 62
      src/Generators/WebRouteGenerator.php

61
src/CrudMake.php

@ -11,6 +11,7 @@ use Luthfi\CrudGenerator\Generators\LangFileGenerator;
use Luthfi\CrudGenerator\Generators\MigrationGenerator;
use Luthfi\CrudGenerator\Generators\ModelFactoryGenerator;
use Luthfi\CrudGenerator\Generators\ModelGenerator;
use Luthfi\CrudGenerator\Generators\WebRouteGenerator;
class CrudMake extends Command
{
@ -81,8 +82,7 @@ class CrudMake extends Command
$this->getModelName();
if (! $this->modelExists()) {
$this->generateResourceRoute();
app(WebRouteGenerator::class, ['command' => $this])->generate();
app(ModelGenerator::class, ['command' => $this])->generate();
app(MigrationGenerator::class, ['command' => $this])->generate();
app(ControllerGenerator::class, ['command' => $this])->generate();
@ -185,19 +185,6 @@ class CrudMake extends Command
}
/**
* Generate API resource version route for CRUD Operation
*
* @return void
*/
public function generateResourceRoute()
{
$webRoutePath = $this->makeRouteFile(base_path('routes'), 'web.php');
$this->files->append($webRoutePath, $this->getWebRouteContent());
$this->info($this->modelNames['model_name'].' resource route generated on routes/web.php.');
}
/**
* Get BrowserKitBaseTest class file content
*
* @return string
@ -230,31 +217,6 @@ class CrudMake extends Command
}
/**
* Get web route content from route web stub
*
* @return string Replaced proper model names in route web file content
*/
public function getWebRouteContent()
{
$stub = $this->files->get(__DIR__.'/stubs/route-web.stub');
$webRouteFileContent = $this->replaceStubString($stub);
if (! is_null($parentName = $this->option('parent'))) {
$pluralModelName = $this->modelNames['plural_model_name'];
$webRouteFileContent = str_replace(
$pluralModelName.'Controller',
$parentName.'\\'.$pluralModelName.'Controller',
$webRouteFileContent
);
}
return $webRouteFileContent;
}
/**
* Make directory if the path is not exists
* @param string $path Absolute path of targetted directory
* @return string Absolute path
@ -269,25 +231,6 @@ class CrudMake extends Command
}
/**
* Create php route file if not exists
* @param string $routeDirPath Absolute directory path
* @param string $filename File name to be created
* @return string Absolute path of create route file
*/
protected function makeRouteFile($routeDirPath, $filename)
{
if (! $this->files->isDirectory($routeDirPath)) {
$this->files->makeDirectory($routeDirPath, 0777, true, true);
}
if (! $this->files->exists($routeDirPath.'/'.$filename)) {
$this->generateFile($routeDirPath.'/'.$filename, "<?php\n");
}
return $routeDirPath.'/'.$filename;
}
/**
* Replace all string of model names
*
* @param string $stub String of file or class stub with default content

2
src/Generators/IndexViewGenerator.php

@ -3,7 +3,7 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Index Views Generator Class
* Index View Generator Class
*/
class IndexViewGenerator extends BaseGenerator
{

62
src/Generators/WebRouteGenerator.php

@ -0,0 +1,62 @@
<?php
namespace Luthfi\CrudGenerator\Generators;
/**
* Web Route Generator Class
*/
class WebRouteGenerator extends BaseGenerator
{
/**
* {@inheritDoc}
*/
public function generate()
{
$webRoutePath = $this->makeRouteFile(base_path('routes'), 'web.php');
$this->files->append($webRoutePath, $this->getContent());
$this->command->info($this->modelNames['model_name'].' resource route generated on routes/web.php.');
}
/**
* {@inheritDoc}
*/
protected function getContent()
{
$stub = $this->files->get(__DIR__.'/../stubs/route-web.stub');
$webRouteFileContent = $this->replaceStubString($stub);
if (! is_null($parentName = $this->command->option('parent'))) {
$pluralModelName = $this->modelNames['plural_model_name'];
$webRouteFileContent = str_replace(
$pluralModelName.'Controller',
$parentName.'\\'.$pluralModelName.'Controller',
$webRouteFileContent
);
}
return $webRouteFileContent;
}
/**
* Create php route file if not exists
* @param string $routeDirPath Absolute directory path
* @param string $filename File name to be created
* @return string Absolute path of create route file
*/
protected function makeRouteFile($routeDirPath, $filename)
{
if (! $this->files->isDirectory($routeDirPath)) {
$this->files->makeDirectory($routeDirPath, 0777, true, true);
}
if (! $this->files->exists($routeDirPath.'/'.$filename)) {
$this->generateFile($routeDirPath.'/'.$filename, "<?php\n");
}
return $routeDirPath.'/'.$filename;
}
}
Loading…
Cancel
Save