我有一个artisan命令,它获取一些选项,其中一个选项是--type=,如下所示:
protected $signature = 'make:procedure {name} {--type=}';--type=包含了这种差异,我想在存根中检查这个选项,因为每种类型都有一个不同的名称空间,应该在存根中使用。
例如,这是我的存根:
<?php
namespace DummyNamespace;
class DummyClass
{
//
}我该怎么做呢(当然这只是一个例子,我只是想解释一下我的问题):
<?php
namespace DummyNamespace;
if ($type === 'one') {
echo 'use App\Some\Namespace\One'
}
class DummyClass
{
//
}如果有人能给我建议,我将不胜感激!
发布于 2021-08-16 17:06:14
您的自定义命令应该从GeneratorCommand派生,然后您可以使用抽象方法getStub()
您的存根文件
namespace DummyNamespace;
/**
* Class DummyClass.
*/
class DummyClass
{
}在您的命令文件中,您只需使用以下代码
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return app_path('file/path/test.stub');
}仅供解释
在GeneratorCommand类中
/**
* Get the stub file for the generator.
*
* @return string
*/
abstract protected function getStub();
/**
* Build the class with the given name.
*
* @param string $name
* @return string
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function buildClass($name)
{
$stub = $this->files->get($this->getStub());
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
}发布于 2021-09-30 14:03:06
从存根生成文件的一种非常简单的方法。
在存根文件中
namespace {{namespace}};
/**
* Class {{name}}.
*/
class {{name}}
{
}你的命令中的某处
protected function getStub()
{
return file_get_contents(resource_path('stubs/dummy.stub'));
}protected function generate($namespace, $name)
{
$template = str_replace(
['{{namespace}}', '{{name}}'],
[$namespace, $name],
$this->getStub()
);
file_put_contents(app_path("Dummies/$name.php"), $template);
}https://stackoverflow.com/questions/64800241
复制相似问题