invoiceninja/app/Console/Commands/MakeClass.php

141 lines
3.9 KiB
PHP
Raw Normal View History

2016-12-08 17:09:45 +02:00
<?php
namespace App\Console\Commands;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Nwidart\Modules\Commands\GeneratorCommand;
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait;
2016-12-08 20:00:13 +02:00
class MakeClass extends GeneratorCommand
2016-12-08 17:09:45 +02:00
{
use ModuleCommandTrait;
protected $argumentName = 'name';
/**
* The name and signature of the console command.
*
* @var string
*/
2016-12-08 20:00:13 +02:00
protected $name = 'ninja:make-class';
2016-12-08 17:09:45 +02:00
/**
* The console command description.
*
* @var string
*/
2016-12-08 20:00:13 +02:00
protected $description = 'Create class stub';
2016-12-08 17:09:45 +02:00
protected function getArguments()
{
return [
2016-12-08 22:39:15 +02:00
['name', InputArgument::REQUIRED, 'The name of the module.'],
2016-12-08 20:00:13 +02:00
['module', InputArgument::REQUIRED, 'The name of module will be used.'],
['class', InputArgument::REQUIRED, 'The name of the class.'],
2016-12-08 22:39:15 +02:00
['prefix', InputArgument::OPTIONAL, 'The prefix of the class.'],
2016-12-08 17:09:45 +02:00
];
}
2016-12-08 23:13:40 +02:00
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('fields', null, InputOption::VALUE_OPTIONAL, 'The model attributes.', null),
2016-12-09 00:06:31 +02:00
array('filename', null, InputOption::VALUE_OPTIONAL, 'The class filename.', null),
2016-12-08 23:13:40 +02:00
);
}
2016-12-08 17:09:45 +02:00
public function getTemplateContents()
{
$module = $this->laravel['modules']->findOrFail($this->getModuleName());
2016-12-08 22:39:15 +02:00
$path = str_replace('/', '\\', config('modules.paths.generator.' . $this->argument('class')));
2016-12-08 17:09:45 +02:00
2016-12-08 22:39:15 +02:00
return (new Stub('/' . $this->argument('prefix') . $this->argument('class') . '.stub', [
'NAMESPACE' => $this->getClassNamespace($module) . "\\" . $path,
2016-12-08 17:09:45 +02:00
'LOWER_NAME' => $module->getLowerName(),
'CLASS' => $this->getClass(),
'STUDLY_NAME' => Str::studly($module->getLowerName()),
2016-12-09 00:18:10 +02:00
'DATATABLE_COLUMNS' => $this->getColumns(),
2016-12-09 00:06:31 +02:00
'FORM_FIELDS' => $this->getFormFields(),
2016-12-09 00:18:10 +02:00
'DATABASE_FIELDS' => $this->getDatabaseFields($module),
2016-12-08 17:09:45 +02:00
]))->render();
}
public function getDestinationFilePath()
{
$path = $this->laravel['modules']->getModulePath($this->getModuleName());
2016-12-08 20:00:13 +02:00
$seederPath = $this->laravel['modules']->config('paths.generator.' . $this->argument('class'));
2016-12-08 17:09:45 +02:00
return $path . $seederPath . '/' . $this->getFileName() . '.php';
}
/**
* @return string
*/
protected function getFileName()
{
2016-12-09 00:06:31 +02:00
if ($this->option('filename')) {
return $this->option('filename');
}
2016-12-08 22:39:15 +02:00
return studly_case($this->argument('prefix')) . studly_case($this->argument('name')) . Str::studly($this->argument('class'));
2016-12-08 17:09:45 +02:00
}
2016-12-08 23:13:40 +02:00
protected function getColumns()
{
$fields = $this->option('fields');
$fields = explode(',', $fields);
$str = '';
foreach ($fields as $field) {
$field = explode(':', $field)[0];
$str .= '[
\''. $field . '\',
function ($model) {
return $model->' . $field . ';
}
],';
}
return $str;
}
2016-12-09 00:06:31 +02:00
protected function getFormFields()
{
$fields = $this->option('fields');
$fields = explode(',', $fields);
$str = '';
foreach ($fields as $field) {
$field = explode(':', $field)[0];
$str .= '{!! Former::text(\''. $field . '\') !!}
';
}
return $str;
}
2016-12-09 00:18:10 +02:00
protected function getDatabaseFields($module)
{
$fields = $this->option('fields');
$fields = explode(',', $fields);
$str = '';
foreach ($fields as $field) {
$field = explode(':', $field)[0];
$str .= "'" . $module->getLowerName() . ".{$field}', ";
}
return $str;
}
2016-12-08 17:09:45 +02:00
}