1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Console/Commands/MakeClass.php

174 lines
4.8 KiB
PHP
Raw Normal View History

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