Skip to content

Commit

Permalink
Scaffolding Returns: A New Hope
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Mar 7, 2014
1 parent 51f0234 commit 6604e5f
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/Way/Generators/Commands/ControllerGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,19 @@ protected function getFileGenerationPath()
*/
protected function getTemplateData()
{
return [
'NAME' => ucwords($this->argument('controllerName'))
];
// LessonsController
$name = ucwords($this->argument('controllerName'));

// lessons
$collection = strtolower(str_replace('Controller', '', $name));

// lesson
$resource = str_singular($collection);

// Lesson
$model = ucwords($resource);

return compact('name', 'collection', 'resource', 'model');
}

/**
Expand Down
195 changes: 195 additions & 0 deletions src/Way/Generators/Commands/ScaffoldGeneratorCommand.php
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']
];
}

}
24 changes: 20 additions & 4 deletions src/Way/Generators/GeneratorsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Way\Generators\Commands\ResourceGeneratorCommand;
use Way\Generators\Commands\SeederGeneratorCommand;
use Way\Generators\Commands\PublishTemplatesCommand;
use Way\Generators\Commands\ScaffoldGeneratorCommand;

class GeneratorsServiceProvider extends ServiceProvider {

Expand All @@ -32,7 +33,7 @@ public function boot()
*/
public function register()
{
foreach(['Model', 'Controller', 'Migration', 'Seeder', 'Resource', 'Publisher'] as $command)
foreach(['Model', 'Controller', 'Migration', 'Seeder', 'Resource', 'Scaffold', 'Publisher'] as $command)
{
$this->{"register$command"}();
}
Expand Down Expand Up @@ -112,18 +113,33 @@ protected function registerResource()
}

/**
* Register command for publish templates
* register command for publish templates
*/
public function registerPublisher()
public function registerpublisher()
{
$this->app['generate.publish-templates'] = $this->app->share(function($app)
{
return new PublishTemplatesCommand;
return new publishtemplatescommand;
});

$this->commands('generate.publish-templates');
}

/**
* register scaffold command
*/
public function registerScaffold()
{
$this->app['generate.scaffold'] = $this->app->share(function($app)
{
return new ScaffoldGeneratorCommand;
});

$this->commands('generate.scaffold');
}



/**
* Get the services provided by the provider.
*
Expand Down
107 changes: 107 additions & 0 deletions src/Way/Generators/templates/scaffolding/controller.txt
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');
}

}
13 changes: 13 additions & 0 deletions src/Way/Generators/templates/scaffolding/model.txt
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 = [];

}

0 comments on commit 6604e5f

Please sign in to comment.