106 lines
2.4 KiB
PHP
Executable File
106 lines
2.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Common\Generators\Action;
|
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Console\GeneratorCommand;
|
|
use InvalidArgumentException;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
class GenerateAction extends GeneratorCommand
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $name = 'make:action';
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $description = 'Create a new action class';
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $type = 'Action';
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
protected function getStub()
|
|
{
|
|
return __DIR__.'/stubs/action.stub';
|
|
}
|
|
|
|
/**
|
|
* @param string $rootNamespace
|
|
* @return string
|
|
*/
|
|
protected function getDefaultNamespace($rootNamespace)
|
|
{
|
|
$base = "$rootNamespace\Actions";
|
|
$modelClass = class_basename($this->parseModel($this->option('model')));
|
|
return $this->option('model') ? "$base\\$modelClass" : $base;
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @return string
|
|
*/
|
|
protected function buildClass($name)
|
|
{
|
|
$replace = [];
|
|
|
|
if ($this->option('model')) {
|
|
$replace = $this->buildModelReplacements($replace);
|
|
}
|
|
|
|
return str_replace(
|
|
array_keys($replace), array_values($replace), parent::buildClass($name)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array $replace
|
|
* @return array
|
|
*/
|
|
protected function buildModelReplacements(array $replace)
|
|
{
|
|
$modelClass = $this->parseModel($this->option('model'));
|
|
|
|
return array_merge($replace, [
|
|
'DummyFullModelClass' => $modelClass,
|
|
'DummyModelClass' => class_basename($modelClass),
|
|
'DummyModelVariable' => lcfirst(class_basename($modelClass)),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @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.$model;
|
|
}
|
|
|
|
return $model;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getOptions()
|
|
{
|
|
return [
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the request applies to.'],
|
|
];
|
|
}
|
|
} |