Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Alla MAC committed Apr 25, 2017
1 parent a211ad5 commit 18cec68
Show file tree
Hide file tree
Showing 18 changed files with 1,784 additions and 0 deletions.
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "roland/crud",
"description": "This is a CRUD generator for Laravel 5.4",
"license": "MIT",
"authors": [
{
"name": "Roland Alla",
"email": "alla.roland@gmail.com"
}
],
"minimum-stability": "stable",
"require": {}
}
122 changes: 122 additions & 0 deletions src/Commands/CrudCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Roland\Crud\Commands;

use File;
use Illuminate\Console\Command;

class CrudCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'crud:generate
{name : The name of the Crud.}
{--fields= : Fields name for the form & model.}
{--route=yes : Include Crud route to routes.php? yes|no.}
{--pk=id : The name of the primary key.}
{--view-path= : The name of the view path.}
{--namespace= : Namespace of the controller.}
{--route-group= : Prefix of the route group.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate Crud including controller, model, views & migrations.';

/** @var string */
protected $routeName = '';

/** @var string */
protected $controller = '';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$name = $this->argument('name');
$modelName = str_singular($name);
$migrationName = str_plural(strtolower($name));
$tableName = $migrationName;
$viewName = strtolower($name);

$routeGroup = $this->option('route-group');
$this->routeName = ($routeGroup) ? $routeGroup . '/' . strtolower($name) : strtolower($name);

$controllerNamespace = ($this->option('namespace')) ? $this->option('namespace') . '\\' : '';

$fields = $this->option('fields');
$primaryKey = $this->option('pk');
$viewPath = $this->option('view-path');

$fieldsArray = explode(',', $fields);
$requiredFields = '';
$requiredFieldsStr = '';

foreach ($fieldsArray as $item) {
$fillableArray[] = preg_replace("/(.*?):(.*)/", "$1", trim($item));

$itemArray = explode(':', $item);
$currentField = trim($itemArray[0]);
$requiredFieldsStr .= (isset($itemArray[2])
&& (trim($itemArray[2]) == 'req'
|| trim($itemArray[2]) == 'required'))
? "'$currentField' => 'required', " : '';
}

$commaSeparetedString = implode("', '", $fillableArray);
$fillable = "['" . $commaSeparetedString . "']";

$requiredFields = ($requiredFieldsStr != '') ? "[" . $requiredFieldsStr . "]" : '';

$this->call('crud:controller', ['name' => $controllerNamespace . $name . 'Controller', '--crud-name' => $name, '--model-name' => $modelName, '--view-path' => $viewPath, '--required-fields' => $requiredFields, '--route-group' => $routeGroup]);
$this->call('crud:model', ['name' => $modelName, '--fillable' => $fillable, '--table' => $tableName]);
$this->call('crud:migration', ['name' => $migrationName, '--schema' => $fields, '--pk' => $primaryKey]);
$this->call('crud:view', ['name' => $viewName, '--fields' => $fields, '--view-path' => $viewPath, '--route-group' => $routeGroup]);
// For optimizing the class loader
$this->callSilent('optimize');

// Updating the Http/routes.php file old laravels app_path('Http/routes.php')
$routeFile = config('crudgenerator.route'); ; //change it to config file
if (file_exists($routeFile) && (strtolower($this->option('route')) === 'yes')) {
$this->controller = ($controllerNamespace != '') ? $controllerNamespace . '\\' . $name . 'Controller' : $name . 'Controller';

if (\App::VERSION() >= '5.2') {
$isAdded = File::append($routeFile,
"\nRoute::group(['middleware' => ['web']], function () {"
. "\n\t" . implode("\n\t", $this->addRoutes())
. "\n});"
);
} else {
$isAdded = File::append($routeFile, "\n".implode("\n", $this->addRoutes()));
}

if ($isAdded) {
$this->info('Crud/Resource route added to ' . $routeFile);
} else {
$this->info('Unable to add the route to ' . $routeFile);
}
}
}

protected function addRoutes() {
return ["Route::resource('" . $this->routeName . "', '" . $this->controller . "');"];
}
}
194 changes: 194 additions & 0 deletions src/Commands/CrudControllerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php

namespace Roland\Crud\Commands;

use Illuminate\Console\GeneratorCommand;

class CrudControllerCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'crud:controller
{name : The name of the controler.}
{--crud-name= : The name of the Crud.}
{--model-name= : The name of the Model.}
{--view-path= : The name of the view path.}
{--required-fields= : Required fields for validations.}
{--route-group= : Prefix of the route group.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new resource controller.';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Controller';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return config('crudgenerator.custom_template')
? config('crudgenerator.path') . '/controller.stub'
: __DIR__ . '/stubs/controller.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
*
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Http\Controllers';
}

/**
* Build the model class with the given name.
*
* @param string $name
*
* @return string
*/
protected function buildClass($name)
{
$stub = $this->files->get($this->getStub());

$viewPath = $this->option('view-path') ? $this->option('view-path') . '.' : '';
$crudName = strtolower($this->option('crud-name'));
$crudNameSingular = str_singular($crudName);
$modelName = $this->option('model-name');
$routeGroup = ($this->option('route-group')) ? $this->option('route-group') . '/' : '';

$validationRules = '';
if ($this->option('required-fields') != '') {
$validationRules = "\$this->validate(\$request, " . $this->option('required-fields') . ");\n";
}

return $this->replaceNamespace($stub, $name)
->replaceViewPath($stub, $viewPath)
->replaceCrudName($stub, $crudName)
->replaceCrudNameSingular($stub, $crudNameSingular)
->replaceModelName($stub, $modelName)
->replaceRouteGroup($stub, $routeGroup)
->replaceValidationRules($stub, $validationRules)
->replaceClass($stub, $name);
}

/**
* Replace the viewPath for the given stub.
*
* @param string $stub
* @param string $viewPath
*
* @return $this
*/
protected function replaceViewPath(&$stub, $viewPath)
{
$stub = str_replace(
'{{viewPath}}', $viewPath, $stub
);

return $this;
}

/**
* Replace the crudName for the given stub.
*
* @param string $stub
* @param string $crudName
*
* @return $this
*/
protected function replaceCrudName(&$stub, $crudName)
{
$stub = str_replace(
'{{crudName}}', $crudName, $stub
);

return $this;
}

/**
* Replace the crudNameSingular for the given stub.
*
* @param string $stub
* @param string $crudNameSingular
*
* @return $this
*/
protected function replaceCrudNameSingular(&$stub, $crudNameSingular)
{
$stub = str_replace(
'{{crudNameSingular}}', $crudNameSingular, $stub
);

return $this;
}

/**
* Replace the modelName for the given stub.
*
* @param string $stub
* @param string $modelName
*
* @return $this
*/
protected function replaceModelName(&$stub, $modelName)
{
$stub = str_replace(
'{{modelName}}', $modelName, $stub
);

return $this;
}

/**
* Replace the routeGroup for the given stub.
*
* @param string $stub
* @param string $routeGroup
*
* @return $this
*/
protected function replaceRouteGroup(&$stub, $routeGroup)
{
$stub = str_replace(
'{{routeGroup}}', $routeGroup, $stub
);

return $this;
}

/**
* Replace the validationRules for the given stub.
*
* @param string $stub
* @param string $validationRules
*
* @return $this
*/
protected function replaceValidationRules(&$stub, $validationRules)
{
$stub = str_replace(
'{{validationRules}}', $validationRules, $stub
);

return $this;
}

}
Loading

0 comments on commit 18cec68

Please sign in to comment.