Created
August 30, 2013 20:15
-
-
Save alxy/6393856 to your computer and use it in GitHub Desktop.
Simple aidkit generator...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Codebryo\Aidkit\Commands; | |
use Illuminate\Console\Command; | |
use \File; | |
use Illuminate\Support\Pluralizer; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputArgument; | |
class GenerateCommand extends Command { | |
/** | |
* The console command name. | |
* | |
* @var string | |
*/ | |
protected $name = 'aidkit:generate'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Generate Aidkit ressource'; | |
protected static $templatePath; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
static::$templatePath = __DIR__.'/../../../../templates/generator'; | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return void | |
*/ | |
public function fire() | |
{ | |
// To prevent Errors create necessary Folders first | |
$this->createFolders(); | |
$this->info('Folder Structure has been created'); | |
// Next calls are logically ordered ( at least in my logic :P ) | |
$this->createController(); | |
$this->info('Controller has been created'); | |
$this->createModel(); | |
$this->info('Model has been created'); | |
$this->createViews(); | |
$this->info('Views have been created'); | |
$this->createRoute(); | |
$this->info('Route has been added'); | |
if ($this->option('createMigration')) { | |
$this->createMigration(); | |
$this->info('Migration has been created'); | |
} | |
return $this->info('Generation complete!'); | |
} | |
/** | |
* Create the basic folter Structure. | |
* | |
* @return void | |
*/ | |
protected function createFolders() | |
{ | |
File::makeDirectory(app_path().'/Aidkit/views/resources/' . Pluralizer::plural($this->argument('ressource'))); | |
} | |
/** | |
* Create a some Controllers. | |
* | |
* @return void | |
*/ | |
protected function createController() | |
{ | |
$templatePath = static::$templatePath; | |
$template = File::get($templatePath . '/controllers/Controller.txt'); | |
$ressource = $this->argument('ressource'); | |
$ressources = Pluralizer::plural($ressource); | |
$Ressources = ucwords($ressources); | |
$Ressource = Pluralizer::singular($Ressources); | |
$template = str_replace( | |
array('{{ressource}}', '{{ressources}}', '{{Ressources}}', '{{Ressource}}'), | |
array($ressource, $ressources, $Ressources, $Ressource), | |
$template); | |
File::put(app_path().'/Aidkit/controllers/Aidkit'. $Ressources .'Controller.php', $template); | |
} | |
/** | |
* Create a some Models. | |
* | |
* @return void | |
*/ | |
protected function createModel() | |
{ | |
$templatePath = static::$templatePath; | |
$template = File::get($templatePath . '/models/Model.txt'); | |
$ressource = $this->argument('ressource'); | |
$ressources = Pluralizer::plural($ressource); | |
$Ressources = ucwords($ressources); | |
$Ressource = Pluralizer::singular($Ressources); | |
$actionLogField = $this->option('actionLogField'); | |
$fields = explode(';', $this->option('fields')); | |
$rules = array(); | |
foreach ($fields as $field) { | |
$parts = explode('#', $field); | |
$rules[$parts[0]] = $parts[1]; | |
} | |
$template = str_replace( | |
array('{{ressource}}', '{{ressources}}', '{{Ressources}}', '{{Ressource}}', '{{actionLogField}}', '{{rules}}'), | |
array($ressource, $ressources, $Ressources, $Ressource, $actionLogField, var_export($rules, true)), | |
$template); | |
File::put(app_path().'/Aidkit/models/'. $Ressource .'.php', $template); | |
} | |
/** | |
* Create the provided views. | |
* | |
* @return void | |
*/ | |
protected function createViews() | |
{ | |
$templatePath = static::$templatePath; | |
$templates = array( | |
'create' => File::get($templatePath . '/views/resources/ressource/create.txt'), | |
'edit' => File::get($templatePath . '/views/resources/ressource/edit.txt'), | |
'index' => File::get($templatePath . '/views/resources/ressource/index.txt'), | |
'show' => File::get($templatePath . '/views/resources/ressource/show.txt') | |
); | |
$ressource = $this->argument('ressource'); | |
$ressources = Pluralizer::plural($ressource); | |
$Ressources = ucwords($ressources); | |
$Ressource = Pluralizer::singular($Ressources); | |
$tableHeadings = array(); | |
$tableData = array(); | |
$formData = array(); | |
$showData = array(); | |
$fields = explode(';', $this->option('fields')); | |
foreach ($fields as $field) { | |
$parts = explode('#', $field); | |
$tableHeadings[] = '<th>' . ucwords($parts[0]) . '</th>'; | |
$tableData[] = '<td>{{ $' . $ressource . '->' . $parts[0] . ' }}</td>'; | |
$formData[] = '<div class="pure-u-1-2"> | |
{{ Form::label(\'' . $parts[0] . '\',\'' . ucwords($parts[0]) . '\',array(\'class\' => \'' . ((strpos($parts[1], 'required') !== false) ? 'required' : '') . '\'))}} | |
{{ Form::' . (isset($parts[2]) ? $parts[2] : 'text') . '(\'' . $parts[0] . '\') }} | |
</div>'; | |
$showData[] = '<div class="pure-u-1"> | |
<h3>' . ucwords($parts[0]) . '</h3> | |
<p>{{ $' . $ressource . '->' . $parts[0] . ' }}</p> | |
</div>'; | |
} | |
foreach ($templates as $templateName => $template) { | |
$template = str_replace( | |
array('{{ressource}}', '{{ressources}}', '{{Ressources}}', '{{Ressource}}', | |
'{{tableHeadings}}', '{{tableData}}', '{{formData}}', '{{showData}}'), | |
array($ressource, $ressources, $Ressources, $Ressource, | |
implode(PHP_EOL, $tableHeadings), implode(PHP_EOL, $tableData), implode(PHP_EOL, $formData), implode(PHP_EOL, $showData)), | |
$template); | |
File::put(app_path().'/Aidkit/views/resources/'. $ressources .'/' . $templateName . '.blade.php', $template); | |
} | |
} | |
/** | |
* Create the routes_admin.php File for clean Route structure. | |
* | |
* @return void | |
*/ | |
protected function createRoute() | |
{ | |
$template = File::get(app_path() . '/Aidkit/routes.php'); | |
$ressource = $this->argument('ressource'); | |
$ressources = Pluralizer::plural($ressource); | |
$Ressources = ucwords($ressources); | |
$Ressource = Pluralizer::singular($Ressources); | |
$position = strpos($template, "\n", strpos($template, 'Route::resource(')); | |
$template = substr_replace($template, PHP_EOL . "\tRoute::resource('" . $ressources . "', 'Aidkit" . $Ressources . "Controller');", $position, 0); | |
File::put(app_path().'/Aidkit/routes.php', $template); | |
} | |
/** | |
* Create a basic migration File. | |
* | |
* @return void | |
*/ | |
protected function createMigration() | |
{ | |
$ressource = $this->argument('ressource'); | |
$ressources = Pluralizer::plural($ressource); | |
$this->call('migrate:make', array('name' => 'create_' . $ressources . '_table')); | |
} | |
/** | |
* Get the console command arguments. | |
* | |
* @return array | |
*/ | |
protected function getArguments() | |
{ | |
return array( | |
array('ressource', InputArgument::REQUIRED, 'Name of the ressource'), | |
); | |
} | |
/** | |
* Get the console command options. | |
* | |
* @return array | |
*/ | |
protected function getOptions() | |
{ | |
return array( | |
array('fields', 'f', InputOption::VALUE_OPTIONAL, 'Fields and validation rules for ressource, schema: "field_name#validation_rules#form_field_type;...'), | |
array('actionLogField', 'a', InputOption::VALUE_OPTIONAL, 'Field, that will be used for the action log, default username', 'username'), | |
array('createMigration', 'm', InputOption::VALUE_NONE, 'Weather to reate migration file or not.'), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Aidkit{{Ressources}}Controller extends AidkitController { | |
public function index() | |
{ | |
${{ressources}} = {{Ressource}}::paginate(15); | |
$this->layout->content = View::make('aidkit::resources.{{ressources}}.index',compact('{{ressources}}')); | |
} | |
public function create() | |
{ | |
$this->layout->content = View::make('aidkit::resources.{{ressources}}.create'); | |
} | |
public function store() | |
{ | |
$insertArray = Input::all(); | |
${{ressource}} = new {{Ressource}}($insertArray); | |
if(${{ressource}}->save()) return Redirect::route('aidkit.{{ressources}}.index'); | |
return Redirect::back()->withInput()->withErrors(${{ressource}}->errors); | |
} | |
public function show($id) | |
{ | |
${{ressource}} = {{Ressource}}::findOrFail($id); | |
$this->layout->content = View::make('aidkit::resources.{{ressources}}.show',compact('{{ressource}}')); | |
} | |
public function edit($id) | |
{ | |
${{ressource}} = {{Ressource}}::findOrFail($id); | |
$this->layout->content = View::make('aidkit::resources.{{ressources}}.edit',compact('{{ressource}}')); | |
} | |
public function update($id) | |
{ | |
${{ressource}} = {{Ressource}}::findOrFail($id); | |
$updateArray = array_filter(Input::all(), 'strlen'); | |
if (${{ressource}}->update($updateArray)) return Redirect::route('aidkit.{{ressources}}.show',$id); | |
return Redirect::back()->withInput()->withErrors(${{ressource}}->errors); | |
} | |
public function destroy($id) | |
{ | |
if(strtoupper(Input::get('delete')) == 'DELETE'){ | |
${{ressource}} = {{Ressource}}::findOrFail($id); | |
${{ressource}}->delete(); | |
return Redirect::route('aidkit.{{ressources}}.index'); | |
} | |
else { | |
return Redirect::route('aidkit.{{ressources}}.show',$id); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class {{Ressource}} extends AidkitModel { | |
/** | |
* What columns should be guarded | |
* | |
* @var string | |
*/ | |
protected $guarded = array(); | |
/** | |
* The table associated with the model. | |
* | |
* @var string | |
*/ | |
protected $table = '{{ressources}}'; | |
/** | |
* Indicates if the model should soft delete. | |
* | |
* @var bool | |
*/ | |
protected $softDelete = true; | |
/** | |
* Object name used by the Actionlog | |
* | |
* @var bool | |
*/ | |
public static $actionlogObjectName = 'Aidkit {{Ressource}}'; | |
/** | |
* Column used for composing Actionlog messages | |
* | |
* @var bool | |
*/ | |
public static $actionlogField = '{{actionLogField}}'; | |
/** | |
* A set of validationrules used when hitting the Database | |
* | |
* @var array | |
*/ | |
public static $rules = {{rules}}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment