You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
user_center/app/Console/Commands/ApiModelGenerator.php

186 lines
6.2 KiB

<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;
class ApiModelGenerator extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:apiModel';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new api resource.';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Model';
public function handle()
{
if (parent::handle() === false && ! $this->option('force')) {
return false;
}
if ($this->option('all')) {
$this->input->setOption('controller', true);
$this->input->setOption('resource', true);
}
if ($this->option('resource')) {
$this->createResource();
}
if ($this->option('controller')) {
$this->createController();
}
}
/**
* Build the class with the given name.
*
* @param string $name
* @return string
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function buildClass($name)
{
$db_name = DB::connection()->getDatabaseName();
$table_name = env('DB_PREFIX') . Str::snake(str_replace($this->getNamespace($name).'\\', '', $name));
$properties = $attributes = '';
$columns = DB::select("SELECT COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT,IS_NULLABLE,COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{$db_name}' AND TABLE_NAME = '{$table_name}'");
foreach ($columns as $column){
$properties .= ' * @property $'. $column->COLUMN_NAME . ' ' . $column->DATA_TYPE . PHP_EOL;
if(in_array($column->COLUMN_NAME, ['id', 'created_at', 'updated_at'])) continue;
$attributes .= " '{$column->COLUMN_NAME}' => ['name' => '{$column->COLUMN_NAME}', 'title' => '". ($column->COLUMN_COMMENT ? explode(':', $column->COLUMN_COMMENT)[0] : $column->COLUMN_NAME) ."', 'type' => '{$column->DATA_TYPE}', 'is_must' => '". intval($column->IS_NULLABLE == 'NO' && $column->COLUMN_DEFAULT === null) ."'], " . PHP_EOL;
}
$replace = [
'{{ table }}' => $table_name,
'{{ propertiesList }}' => Str::replaceLast(PHP_EOL, '', $properties),
'{{ attrList }}' => Str::replaceLast(PHP_EOL, '', $attributes)
];
return str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
);
}
protected function generatorProperty(&$stub, $name)
{
$db_name = DB::connection()->getDatabaseName();
$table_name = env('DB_PREFIX') . Str::snake(str_replace($this->getNamespace($name).'\\', '', $name));
$properties = $attributes = '';
$columns = DB::select("SELECT COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT,IS_NULLABLE,COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{$db_name}' AND TABLE_NAME = '{$table_name}'");
foreach ($columns as $column){
$properties .= ' * @property $'. $column->COLUMN_NAME . ' ' . $column->DATA_TYPE . PHP_EOL;
if(in_array($column->COLUMN_NAME, ['id', 'created_at', 'updated_at'])) continue;
$attributes .= " '{$column->COLUMN_NAME}' => ['name' => '{$column->COLUMN_NAME}', 'title' => '". ($column->COLUMN_COMMENT ? explode(':', $column->COLUMN_COMMENT)[0] : $column->COLUMN_NAME) ."', 'type' => '{$column->DATA_TYPE}', 'is_must' => '". intval($column->IS_NULLABLE == 'NO' && $column->COLUMN_DEFAULT === null) ."'], " . PHP_EOL;
}
$stub = str_replace(['{{ table }}', '{{ propertiesList }}', '{{ attrList }}'], [$table_name, Str::replaceLast(PHP_EOL, '', $properties), Str::replaceLast(PHP_EOL, '', $attributes)], $stub);
return $this;
}
/**
* Create a seeder file for the model.
*
* @return void
*/
protected function createResource()
{
$resource = Str::replaceLast('T', '', Str::studly(class_basename($this->argument('name'))));
$this->call('make:apiResource', [
'name' => "{$resource}",
]);
$this->call('make:resource', [
'name' => "{$resource}Collection",
]);
}
/**
* Create a controller for the model.
*
* @return void
*/
protected function createController()
{
$controller = Str::replaceLast('T', '', Str::studly(class_basename($this->argument('name'))));
$modelName = $this->qualifyClass($this->getNameInput());
$this->call('make:apiController', array_filter([
'name' => "App\\Http\\Controllers\\Api\\{$controller}Controller",
'--model' => $modelName,
'--api' => true,
]));
}
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return $this->resolveStubPath('/stubs/model.stub');
}
/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return is_dir(app_path('Models')) ? $rootNamespace.'\\Models' : $rootNamespace;
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['all', 'a', InputOption::VALUE_NONE, 'Generate a resource and controller for the model'],
['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'],
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the model already exists'],
['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'],
];
}
}