hypery 十一、命令行

教程:Hyperf

symfony/console

composer地址: symfony/console - Packagist

github地址:GitHub - symfony/console: Eases the creation of beautiful and testable command line interfaces

hyperf/command github地址:https://github.com/hyperf/command

一、定义

1.1 自定义

用注释定义类为命令行类,再定义领命名字、注释、参数等。

根据类Hyperf\Command\Command和其父类Symfony\Component\Console\Command\Command,Test1Command重写父类Hyperf\Command\Command::configure(),而symfony\Component\Console\Command\Command中只是定义了configure方法名。

php 复制代码
#Hyperf\Command\Command
use Symfony\Component\Console\Command\Command as SymfonyCommand;
abstract class Command extends SymfonyCommand
{
......
    protected function configure()
    {
        parent::configure();
        if (! isset($this->signature)) {
            $this->specifyParameters();
        }
    }
......
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->enableDispatcher($input);
        $callback = function () {
            ......
                call([$this, 'handle']);
            ......
            return 0;
        };

        if ($this->coroutine && ! Coroutine::inCoroutine()) {
            run($callback, $this->hookFlags);
            return $this->exitCode;
        }

        return $callback();
    }
......
}
php 复制代码
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;

/**
 * @Command
 */
#[Command]
class Test1Command extends HyperfCommand
{
    protected $name = "test:first";
    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
    }
    public function handle()
    {
        $str = "123";
        $this->line($str);
    }
}

1.2 使用命令行

php 复制代码
php bin/hyperf.php gen:command 命令名

1.3 设置配置文件

php 复制代码
#config/commands.php
return [
    "App\Command\Test2Command",
];

#App\Command\Test2Command 
namespace App\Command;
use Hyperf\Command\Command as HyperfCommand;

class Test2Command extends HyperfCommand
{
    protected $name = "test:test";

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
    }

    public function handle()
    {
        $this->line('Hello Hyperf!', 'info');
    }
}


#命令行
php bin/hyperf.php
......
test
  test:first
  test:test           Hyperf Demo Command
    

1.4 symfony/console 使用

php 复制代码
namespace Hyperf\Framework;

use Hyperf\Command\Annotation\Command;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\Annotation\AnnotationCollector;
use Hyperf\Framework\Event\BootApplication;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Console\Application;

class ApplicationFactory
{
    public function __invoke(ContainerInterface $container)
    {
        if ($container->has(EventDispatcherInterface::class)) {
            $eventDispatcher = $container->get(EventDispatcherInterface::class);
            $eventDispatcher->dispatch(new BootApplication());
        }

        $config = $container->get(ConfigInterface::class);
        //从配置文件中获取
        $commands = $config->get('commands', []);
        // Append commands that defined by annotation.
        $annotationCommands = [];
        //从设置备注的类中获取
        if (class_exists(AnnotationCollector::class) && class_exists(Command::class)) {
            $annotationCommands = AnnotationCollector::getClassesByAnnotation(Command::class);
            $annotationCommands = array_keys($annotationCommands);
        }

        $commands = array_unique(array_merge($commands, $annotationCommands));
        $application = new Application();

        if (isset($eventDispatcher) && class_exists(SymfonyEventDispatcher::class)) {
            $application->setDispatcher(new SymfonyEventDispatcher($eventDispatcher));
        }

        foreach ($commands as $command) {
            $application->add($container->get($command));
        }
        return $application;
    }
}
相关推荐
Lucky小小吴12 小时前
木马查杀篇—Opcode提取
php·opcode·木马查杀
邪恶的贝利亚16 小时前
《ffplay 读线程与解码线程分析:从初始化到 seek 操作,对比视频与音频解码的差异》
ffmpeg·php·音视频
廖圣平16 小时前
美团核销 第三方接口供应商 (含接口文档)
开发语言·数据库·php
sunsineq17 小时前
[超级简单]讲解如何用PHP实现LINE Pay API!
开发语言·php·linepay
新老农17 小时前
php数据导出pdf,然后pdf转图片,再推送钉钉群
pdf·php·钉钉
上海合宙LuatOS17 小时前
全栈工程师实战手册:LuatOS日志系统开发指南!
java·开发语言·单片机·嵌入式硬件·物联网·php·硬件工程
小诸葛的博客21 小时前
Flannel UDP 模式的优缺点
网络协议·udp·php
桃子酱紫君1 天前
华为配置篇-RSTP/MSTP实验
开发语言·华为·php
JPCstorm1 天前
客服系统重构详细计划
php
智慧地球(AI·Earth)1 天前
OpenAI for Countries:全球AI基础设施的“技术基建革命”
开发语言·人工智能·php