有人详解下命令行stub吗?

目前写的

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Service extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:service {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $name = $this->argument('name');
        $service_name = $name . 'Service';
        $dir = 'app/Services';
        if (!(is_dir($dir) && file_exists($dir))) {
            mkdir($dir, 0777, true);
        }
        $str = <<<STR
<?php

namespace App\Services;

class {$service_name}  {

}
STR;
        try{
            if (!file_exists($service_name)) {
                $fp = fopen($dir . '/' . $service_name . '.php', 'w');
                fwrite($fp, $str);
            }
        }catch(\Exception $e) {
            $this->info('创建失败');return;
        }
        $this->info("create {$service_name} successfully");
    }
}
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
陈先生
最佳答案

框架使用的是hyperf 内容不难

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Command;

use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Di\Annotation\Inject;
use League\Flysystem\Filesystem;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;

/**
 * @Command
 */
class GenServiceCommand extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    protected $signature = 'gen:service {--name : name} {--model= : model}';

    /**
     * @Inject
     */
    protected Filesystem $fileSystem;

    protected string $namespace = 'App\\Service';

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('gen:service');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
        $this->addArgument('name', InputArgument::REQUIRED, '服务名称');
        $this->addArgument('model', InputArgument::OPTIONAL, '使用的model对象');
    }

    public function handle()
    {
        $this->line('生成Service ', 'info');
        [$model, $namespace, $class, $modelName] = $this->getData();
        $this->line('生成Service ' . $class . ' 使用model为' . $modelName, 'info');
        $content = $this->getStub();
        $list = compact('model', 'namespace', 'class', 'modelName');
        foreach ($list as $key => $item) {
            $this->replaceClass($content, $key, $item);
        }
        $directory = BASE_PATH . '/' . str_replace('\\', '/', lcfirst($namespace)) . '/';
        if (is_dir($directory) === false) {
            mkdir($directory, 0777);
        }
        file_put_contents($directory . $class . '.php', $content, FILE_TEXT);
    }

    public function getData()
    {
        $arguments = ($this->input->getArguments());
        $name = ucfirst($arguments['name']);
        $model = $arguments['model'];
        $name = str_replace('/', '\\', $name);
        $result = (explode('\\', $name));
        $class = array_pop($result);
        $namespace = $this->namespace . '\\' . implode('\\', $result);
        $namespace = substr($this->namespace . '\\' . implode('\\', $result), 0, mb_strlen($namespace) - 1);
        $modelName = $model;
        $model = '\\App\\Model\\' . ucfirst($model);
        return [$model, $namespace, $class, $modelName];
    }

    public function getStub()
    {
        $file = BASE_PATH . '/app/Command/stub/service.stub';
        return file_get_contents($file);
    }

    public function replaceClass(&$stub, $name, $value)
    {
        $stub = str_replace('%' . strtoupper($name) . '%', $value, $stub);
    }
}

2年前 评论
讨论数量: 9
陈先生

具体是哪里需要详细解释呢?

2年前 评论

@陈先生 创建使用stub,然后执行命令的时候,如果目录层级多,是怎么解决的,比如 Services/Admin/xxxService.php

2年前 评论
陈先生 2年前
王大牛 (作者) (楼主) 2年前
陈先生 2年前
王大牛 (作者) (楼主) 2年前
陈先生 2年前
陈先生

框架使用的是hyperf 内容不难

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Command;

use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Di\Annotation\Inject;
use League\Flysystem\Filesystem;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;

/**
 * @Command
 */
class GenServiceCommand extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    protected $signature = 'gen:service {--name : name} {--model= : model}';

    /**
     * @Inject
     */
    protected Filesystem $fileSystem;

    protected string $namespace = 'App\\Service';

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('gen:service');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
        $this->addArgument('name', InputArgument::REQUIRED, '服务名称');
        $this->addArgument('model', InputArgument::OPTIONAL, '使用的model对象');
    }

    public function handle()
    {
        $this->line('生成Service ', 'info');
        [$model, $namespace, $class, $modelName] = $this->getData();
        $this->line('生成Service ' . $class . ' 使用model为' . $modelName, 'info');
        $content = $this->getStub();
        $list = compact('model', 'namespace', 'class', 'modelName');
        foreach ($list as $key => $item) {
            $this->replaceClass($content, $key, $item);
        }
        $directory = BASE_PATH . '/' . str_replace('\\', '/', lcfirst($namespace)) . '/';
        if (is_dir($directory) === false) {
            mkdir($directory, 0777);
        }
        file_put_contents($directory . $class . '.php', $content, FILE_TEXT);
    }

    public function getData()
    {
        $arguments = ($this->input->getArguments());
        $name = ucfirst($arguments['name']);
        $model = $arguments['model'];
        $name = str_replace('/', '\\', $name);
        $result = (explode('\\', $name));
        $class = array_pop($result);
        $namespace = $this->namespace . '\\' . implode('\\', $result);
        $namespace = substr($this->namespace . '\\' . implode('\\', $result), 0, mb_strlen($namespace) - 1);
        $modelName = $model;
        $model = '\\App\\Model\\' . ucfirst($model);
        return [$model, $namespace, $class, $modelName];
    }

    public function getStub()
    {
        $file = BASE_PATH . '/app/Command/stub/service.stub';
        return file_get_contents($file);
    }

    public function replaceClass(&$stub, $name, $value)
    {
        $stub = str_replace('%' . strtoupper($name) . '%', $value, $stub);
    }
}

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!