Last active
August 19, 2017 07:35
-
-
Save tajulasri/0cdd99cf8c0d239063633193009b7e38 to your computer and use it in GitHub Desktop.
Make transformer command laravel
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 App\Console\Commands; | |
use Illuminate\Console\GeneratorCommand; | |
use Illuminate\Support\Str; | |
use InvalidArgumentException; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputOption; | |
class MakeTransformerCommand extends GeneratorCommand | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $name = 'make:transformer'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Generate transformer'; | |
/** | |
* type to generate | |
* @var string | |
*/ | |
protected $type = 'Transformer'; | |
/** | |
* namespace models | |
* @var string | |
*/ | |
protected $modelNamespace = 'Entity\\'; | |
/** | |
* Get the stub file for the generator. | |
* | |
* @return string | |
*/ | |
protected function getStub() | |
{ | |
return __DIR__ . '/stubs/transformer.plain.stub'; | |
} | |
/** | |
* Get the default namespace for the class. | |
* | |
* @param string $rootNamespace | |
* @return string | |
*/ | |
protected function getDefaultNamespace($rootNamespace) | |
{ | |
return $rootNamespace . '\Http\Transformers'; | |
} | |
/** | |
* Build the class with the given name. | |
* | |
* Remove the base controller import if we are already in base namespace. | |
* | |
* @param string $name | |
* @return string | |
*/ | |
protected function buildClass($name) | |
{ | |
$transformerNamespace = $this->getNamespace($name); | |
$replace = []; | |
if ($this->argument('model')) { | |
$modelClass = $this->parseModel($this->argument('model')); | |
if (!class_exists($modelClass)) { | |
if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) { | |
$this->call('make:model', ['name' => $modelClass]); | |
} | |
//throw new FileNotFoundException(sprintf('Model are not found %s', $modelClass)); | |
} | |
$replace = [ | |
'DummyFullModelClass' => $modelClass, | |
'DummyModelClass' => class_basename($modelClass), | |
'DummyModelVariable' => lcfirst(class_basename($modelClass)), | |
]; | |
} | |
$replace["use {$transformerNamespace}\Transformer;\n"] = ''; | |
return str_replace( | |
array_keys($replace), array_values($replace), parent::buildClass($name) | |
); | |
} | |
/** | |
* Get the fully-qualified model class name. | |
* | |
* @param string $model | |
* @return string | |
*/ | |
protected function parseModel($model) | |
{ | |
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { | |
throw new InvalidArgumentException('Model name contains invalid characters.'); | |
} | |
$model = trim(str_replace('/', '\\', $model), '\\'); | |
if (!Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) { | |
$model = $rootNamespace . $this->modelNamespace . $model; | |
} | |
return $model; | |
} | |
/** | |
* Get the console command options. | |
* | |
* @return array | |
*/ | |
protected function getArguments() | |
{ | |
return [ | |
['name', InputArgument::REQUIRED, 'Generate a transfomer for the given model.'], | |
['model', InputOption::VALUE_REQUIRED, 'Use this models inside transformation.'], | |
]; | |
} | |
} |
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 DummyNamespace; | |
use DummyFullModelClass; | |
use League\Fractal\TransformerAbstract; | |
class DummyClass extends TransformerAbstract | |
{ | |
public function transform(DummyModelClass $DummyModelVariable) | |
{ | |
return [ | |
'id' => $DummyModelVariable->id, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment