Ich glaube, dass es keine Möglichkeit gibt, dies zu überschreiben (vorerst), aber ich denke, dass Sie Ihren benutzerdefinierten Befehl erstellen können, der Laravel-Logik verwenden wird. Dies wurde für Laravel erstellt 5.
Zuerst haben Generator Befehl erstellen app/Console/Commands/Generator.php
:
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputArgument;
class Generator extends Command
{
/**
* Command name
*
* @var string
*/
protected $name = 'generate';
/**
* Command description
*
* @var string
*/
protected $description = 'Custom object generator';
/**
* An array with all available generator classes
*
* @var array
*/
protected $types = ['request', 'model', 'middleware'];
/**
* Execute command
*
* @return mixed
*/
public function handle()
{
$type = $this->argument('type');
if (!in_array($type, $this->types)) {
return $this->error('Type must be one of: '.implode(', ', $this->types));
}
// Create new instance
$generatorClass = 'App\Console\Commands\Generators\\'.ucfirst($type);
$generator = new $generatorClass(new Filesystem());
// Each generator has "fire" method
$this->comment($generator->setClassName($this->argument('name'))->fire());
}
/**
* @return array
*/
public function getArguments()
{
return [
['type', InputArgument::REQUIRED, 'Type of class to generate: '.implode(', ', $this->types)],
['name', InputArgument::REQUIRED, 'Name of class to generate'],
];
}
}
Dann haben Sie eine abstrakte Klasse für alle Ihre Generatoren Klassen app/Console/Commands/Generators/Generator.php
zu erstellen:
<?php namespace App\Console\Commands\Generators;
use Illuminate\Console\GeneratorCommand;
abstract class Generator extends GeneratorCommand
{
// Directory name with whole application (by default app)
const APP_PATH = 'app';
/*
* Name and description of command wont be used
* Generators Commands are not loaded via Kernel
* Name and description property has been put just to avoid Exception thrown by Symfony Command class
*/
protected $name = 'fake';
protected $description = 'fake';
/**
* Class name to generate
*
* @var string
*/
protected $className;
/**
* Returns class name to generate
*
* @return string
*/
protected function getNameInput()
{
return $this->className;
}
/**
* Returns path under which class should be generated
*
* @param string $name
* @return string
*/
protected function getPath($name)
{
$name = str_replace($this->getAppNamespace(), '', $name);
return self::APP_PATH.'/'.str_replace('\\', '/', $name).'.php';
}
/**
* Sets class name to generate
*
* @param string $name
* @return $this
*/
public function setClassName($name)
{
$this->className = $name;
return $this;
}
/**
* Execute command
*
* @return string
*/
public function fire()
{
$name = $this->parseName($this->getNameInput());
if ($this->files->exists($path = $this->getPath($name)))
{
return $this->type.' already exists!';
}
$this->makeDirectory($path);
$this->files->put($path, $this->buildClass($name));
return $this->type.' '.$this->className.' created successfully.';
}
}
Am Ende können Sie Ihre erste Generator-Klasse erstellen! app/Console/Commands/Generators/Request.php
<?php namespace App\Console\Commands\Generators;
class Request extends Generator
{
/**
* Class type to generate
*
* @var string
*/
protected $type = 'Request';
/**
* Returns default namespace for objects being generated
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Http\Requests';
}
/**
* Returns path to custom stub
*
* @return string
*/
public function getStub()
{
return base_path('resources').'/stubs/request.stub';
}
}
Vergessen Sie nicht, Ihren erzeugen Befehl Kernel hinzufügen app/Console/Kernel.php
:
<?php namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel {
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
...
'App\Console\Commands\Generator',
...
];
Ihre Stubs Setzen unter resources/stubs
Verzeichnis. Lassen Sie uns zunächst einen für Request Generator resources/stubs/request.stub
erstellen:
<?php namespace {{namespace}};
class {{class}} extends Request
{
/**
* @return bool
*/
public function authorize()
{
// CUSTOM LOGIC
return false;
}
/**
* @return array
*/
public function rules()
{
$rules = [];
// CUSTOM LOGIC
return $rules;
}
}
Dann rufen Sie mit php artisan generate request MyRequest
.
Sie können Ihr benutzerdefiniertes Modell, Middleware, Controller usw. erstellenGeneratoren, es ist sehr einfach - Sie müssen neue Generator-Klasse unter app/Commands/Console/Generators
erstellen - werfen Sie einen Blick auf Request.php
Generator, um zu sehen, wie es funktioniert!
Wenn jemand interessiert ist, machte ich ein Paket, das dieses Problem löst L5 + https://github.com/Kyslik/artisan-stubs – Kyslik