forked from dahabit/Laravel-4-Generators
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51f0234
commit 6604e5f
Showing
5 changed files
with
348 additions
and
7 deletions.
There are no files selected for viewing
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
195 changes: 195 additions & 0 deletions
195
src/Way/Generators/Commands/ScaffoldGeneratorCommand.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
<?php namespace Way\Generators\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
class ScaffoldGeneratorCommand extends Command { | ||
|
||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'generate:scaffold'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Scaffold a new resource (with boilerplate)'; | ||
|
||
/** | ||
* Generate a resource | ||
* | ||
* @return mixed | ||
*/ | ||
public function fire() | ||
{ | ||
$resource = $this->argument('resource'); | ||
|
||
$this->callModel($resource); | ||
$this->callController($resource); | ||
$this->callMigration($resource); | ||
$this->callSeeder($resource); | ||
$this->callMigrate(); | ||
|
||
// All done! | ||
$this->info(sprintf( | ||
"All done! Don't forget to add '%s` to %s." . PHP_EOL, | ||
"Route::resource('{$this->getTableName($resource)}', '{$this->getControllerName($resource)}');", | ||
"app/routes.php" | ||
)); | ||
|
||
} | ||
|
||
/** | ||
* Get the name for the model | ||
* | ||
* @param $resource | ||
* @return string | ||
*/ | ||
protected function getModelName($resource) | ||
{ | ||
return ucwords(str_singular(camel_case($resource))); | ||
} | ||
|
||
/** | ||
* Get the name for the controller | ||
* | ||
* @param $resource | ||
* @return string | ||
*/ | ||
protected function getControllerName($resource) | ||
{ | ||
return ucwords(str_plural(camel_case($resource))) . 'Controller'; | ||
} | ||
|
||
/** | ||
* Get the DB table name | ||
* | ||
* @param $resource | ||
* @return string | ||
*/ | ||
protected function getTableName($resource) | ||
{ | ||
return str_plural($resource); | ||
} | ||
|
||
/** | ||
* Get the name for the migration | ||
* | ||
* @param $resource | ||
* @return string | ||
*/ | ||
protected function getMigrationName($resource) | ||
{ | ||
return "create_" . str_plural($resource) . "_table"; | ||
} | ||
|
||
/** | ||
* Call model generator if user confirms | ||
* | ||
* @param $resource | ||
*/ | ||
protected function callModel($resource) | ||
{ | ||
$modelName = $this->getModelName($resource); | ||
|
||
if ($this->confirm("Do you want me to create a $modelName model? [yes|no]")) | ||
{ | ||
$this->call('generate:model', [ | ||
'modelName' => $modelName, | ||
'--templatePath' => __DIR__.'/../templates/scaffolding/model.txt' | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Call controller generator if user confirms | ||
* | ||
* @param $resource | ||
*/ | ||
protected function callController($resource) | ||
{ | ||
$controllerName = $this->getControllerName($resource); | ||
|
||
if ($this->confirm("Do you want me to create a $controllerName controller? [yes|no]")) | ||
{ | ||
$this->call('generate:controller', [ | ||
'controllerName' => $controllerName, | ||
'--templatePath' => __DIR__.'/../templates/scaffolding/controller.txt' | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Call migration generator if user confirms | ||
* | ||
* @param $resource | ||
*/ | ||
protected function callMigration($resource) | ||
{ | ||
$migrationName = $this->getMigrationName($resource); | ||
|
||
if ($this->confirm("Do you want me to create a '$migrationName' migration and schema for this resource? [yes|no]")) | ||
{ | ||
$this->call('generate:migration', [ | ||
'migrationName' => $migrationName, | ||
'--fields' => $this->option('fields') | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Call seeder generator if user confirms | ||
* | ||
* @param $resource | ||
*/ | ||
protected function callSeeder($resource) | ||
{ | ||
$tableName = str_plural($this->getModelName($resource)); | ||
|
||
if ($this->confirm("Would you like a '$tableName' table seeder?")) | ||
{ | ||
$this->call('generate:seed', compact('tableName')); | ||
} | ||
} | ||
|
||
/** | ||
* Migrate database if user confirms | ||
*/ | ||
protected function callMigrate() | ||
{ | ||
if ($this->confirm('Do you want to go ahead and migrate the database? [yes|no]')) { | ||
$this->call('migrate'); | ||
$this->info('Done!'); | ||
} | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getArguments() | ||
{ | ||
return [ | ||
['resource', InputArgument::REQUIRED, 'Singular resource name'] | ||
]; | ||
} | ||
|
||
/** | ||
* Get the console command options. | ||
* | ||
* @return array | ||
*/ | ||
protected function getOptions() | ||
{ | ||
return [ | ||
['fields', null, InputOption::VALUE_OPTIONAL, 'Fields for the migration'] | ||
]; | ||
} | ||
|
||
} |
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
107 changes: 107 additions & 0 deletions
107
src/Way/Generators/templates/scaffolding/controller.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
class $NAME$ extends \BaseController { | ||
|
||
/** | ||
* Display a listing of $COLLECTION$ | ||
* | ||
* @return Response | ||
*/ | ||
public function index() | ||
{ | ||
$$COLLECTION$ = $MODEL$::all(); | ||
|
||
return View::make('$COLLECTION$.index', compact('$COLLECTION$')); | ||
} | ||
|
||
/** | ||
* Show the form for creating a new $RESOURCE$ | ||
* | ||
* @return Response | ||
*/ | ||
public function create() | ||
{ | ||
return View::make('$COLLECTION$.create'); | ||
} | ||
|
||
/** | ||
* Store a newly created $RESOURCE$ in storage. | ||
* | ||
* @return Response | ||
*/ | ||
public function store() | ||
{ | ||
$validator = Validator::make($data = Input::all(), $MODEL$::$rules); | ||
|
||
if ($validator->fails()) | ||
{ | ||
return Redirect::back()->withErrors($validator)->withInput(); | ||
} | ||
|
||
$MODEL$::create($data); | ||
|
||
return Redirect::route('$COLLECTION$.index'); | ||
} | ||
|
||
/** | ||
* Display the specified $RESOURCE$. | ||
* | ||
* @param int $id | ||
* @return Response | ||
*/ | ||
public function show($id) | ||
{ | ||
$$RESOURCE$ = $MODEL$::findOrFail($id); | ||
|
||
return View::make('$COLLECTION$.show', compact('$RESOURCE$')); | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified $RESOURCE$. | ||
* | ||
* @param int $id | ||
* @return Response | ||
*/ | ||
public function edit($id) | ||
{ | ||
$$RESOURCE$ = $MODEL$::find($id); | ||
|
||
return View::make('$COLLECTION$.edit', compact('$RESOURCE$')); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @param int $id | ||
* @return Response | ||
*/ | ||
public function update($id) | ||
{ | ||
$$RESOURCE$ = $MODEL$::findOrFail($id); | ||
|
||
$validator = Validator::make($data = Input::all(), $MODEL$::$rules); | ||
|
||
if ($validator->fails()) | ||
{ | ||
return Redirect::back()->withErrors($validator)->withInput(); | ||
} | ||
|
||
$$RESOURCE$->update($data); | ||
|
||
return Redirect::route('$COLLECTION$.index'); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param int $id | ||
* @return Response | ||
*/ | ||
public function destroy($id) | ||
{ | ||
$MODEL$::destroy($id); | ||
|
||
return Redirect::route('$COLLECTION$.index'); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
class $NAME$ extends \Eloquent { | ||
|
||
// Add your validation rules here | ||
public static $rules = [ | ||
// 'title' => 'required' | ||
]; | ||
|
||
// Don't forget to fill this array | ||
protected $fillable = []; | ||
|
||
} |