secret
Last active

Your Assignment: what could be improved in this class? Does it violate the SRP? Can we add additional generators easily? Provide your ideas below. Solution will be recorded/posted Friday, May 3rd.

  • Download Gist
ModelGeneratorCommand.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
<?php namespace Commands;
 
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem as File;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
 
class ModelGeneratorCommand extends Command {
 
/**
* The console command name.
*
* @var string
*/
protected $name = 'generate:model';
 
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate an Eloquent model with boilerplate.';
 
/**
* The Filesystem instance
*
* @var Illuminate\Filesystem\Filesystem
*/
protected $file;
 
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(File $file)
{
parent::__construct();
 
$this->file = $file;
}
 
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
// figure out what model to create
$modelName = ucwords($this->argument('name'));
$path = $this->option('path').'/'.$modelName.'.php';
$template = $this->getTemplate($modelName);
 
if ($this->createModel($path, $template))
{
return $this->info("Created: $path.");
}
 
$this->info("Could not create $path.");
}
 
protected function getPath()
{
return app_path().'/models';
}
 
protected function getTemplate($modelName)
{
$template = $this->file->get(__DIR__.'/templates/model.txt');
return str_replace('{{modelName}}', $modelName, $template);
}
 
protected function createModel($path, $data)
{
if ( ! $this->file->exists($path))
return $this->file->put($path, $data);
 
return false;
}
 
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('name', InputArgument::REQUIRED, 'The name of the model to generate.'),
);
}
 
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('path', 'p', InputOption::VALUE_OPTIONAL, 'Path to where model should be created.', $this->getPath()),
);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.