当前位置: 首页 > news >正文

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方法名。

#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();}
……
}
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 bin/hyperf.php gen:command 命令名

       1.3 设置配置文件

        

#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
……
testtest:firsttest:test           Hyperf Demo Command

        1.4 symfony/console 使用

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;}
}

http://www.lryc.cn/news/97338.html

相关文章:

  • QT占位符 %n+arg()、QString的格式化arg(补零/进制转换)
  • 浙江大学第六周数据结构之06-图1 列出连通集
  • DNS缓存病毒防护43.227.220
  • Spring MVC -- 返回数据(静态页面+非静态页面+JSON对象+请求转发与请求重定向)
  • k8s集群部署(使用kubeadm部署工具进行快速部署,相关对应版本为docker20.10.0+k8s1.23.0)
  • SIP视频对讲sip广播网关
  • prometheus直方图实践
  • 【C语言进阶篇】指针都学完了吧!那回调函数的应用我不允许还有人不会!
  • 专注:如何提高专注力和注意力的简要指南
  • Linux查看内存的几种方法
  • selenium定位rect元素
  • uniapp <textarea>组件的踩坑
  • README.md 文档使用 treer 生成树形项目结构
  • 朝花夕拾思维导图怎么画?看看这种绘制方法
  • Python - OpenCV、OCR识别摄像头中的文字
  • 金融中的数学:贝叶斯公式
  • ClickHouse单节点安装配置
  • AtcoderABC231场
  • opengauss数据库快速安装
  • 前端中的LocalStorage和SessionStorage
  • 论文工具——写论文好用的绘图工具(甘特图+流程图+网络模型图+泳道图)
  • Vite构建的vue3项目修改网站标题和图标
  • 平安私人银行受邀慈善服务高质量发展会议,分享慈善规划服务
  • MySQL主从复制、读写分离
  • Redis配置与优化
  • leetCode刷题记录3-面试经典150题
  • MySQL优化(面试)
  • 华为鸿蒙HarmonyOS4发布即巅峰,车机系统、多模态交互等实现突破
  • Camtasia2023电脑录屏视频自动生成字幕软件
  • List有值二次转换给其他对象报null