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.mzph.cn/news/9164.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ubuntu18.04未安装Qt报qt.qpa.plugin could not load the Qt platform plugin xcb问题的解决方法

在Ubuntu 18.04开发机上安装了Qt 5.14.2,当将其可执行程序拷贝到另一台未安装Qt的Ubuntu 18.04上报错:拷贝可执行程序前,使用ldd将此执行程序依赖的动态库也一起拷贝过去,包括Qt5.14.2/5.14.2/gcc_64/plugins目录系的platforms目录…

【UE5 多人联机教程】05-生成玩家

步骤 1. 新建一个游戏模式基础 命名为“GM_Lobby” 2. 新建一个玩家控制器,命名为“PC_Lobby” 3. 新建一个游戏状态基础 命名为“GS_Lobby” 重新设置游戏模式重载、玩家控制器类、游戏状态类 4. 新建一个控件蓝图,命名为“UMG_Lobby” 打开“UMG_Lobb…

达闼面试(部分)(未完全解析)

grpc怎么解决负载均衡问题? Answer by newBing : gRPC提供了多种负载均衡策略,包括轮询、随机、最少连接数等。gRPC客户端可以使用这些策略来选择要连接的服务器。 k8s环境下部署grpc的几种方案 : 在k8s环境中,可以选择headless service,或者…

通过wait和notify来协调线程执行顺序

为什么要协调线程执行顺序 wait和notify是多线程编程中的重要工具,多线程调度是随机的,很多时候希望多个线程能够按照我们规定的顺序来执行,完成线程之间的配合工作 注意事项 1.要想让notify能够顺利唤醒wait,就需要确保wait和no…

PostgreSQL-视图-03-查询对象依赖关系视图-dba_dependencies

PostgreSQL查询对象依赖关系视图 -- PostgreSQL查询对象依赖关系视图drop view if exists tzq.dba_dependencies; create view tzq.dba_dependencies as with source_obj as (select sp.oid,sp.proname,unnest(string_to_array(regexp_replace(regexp_replace(lower(sp.prosrc…

Spring Alibaba Sentinel实现集群限流demo

1.背景 1.什么是单机限流? 小伙伴们或许遇到过下图这样的限流配置 又或者是这样的Nacos动态配置限流规则: 以上这些是什么限流?没错,就是单机限流,那么单机限流有什么弊端呢? 假设我们集群部署3台机器&a…

Vue过度与动画

Test.vue:元素外面包一层transition&#xff0c;展示的时候就默认调用style里面的v-enter-action和v-leave-action执行进入和退出效果&#xff0c;appear上来默认展示动画效果 <template><div><button click"isShow !isShow">显示/隐藏</butto…

如何高效地查询IP归属地

高效识别IP归属地是网络安全领域中的一项重要工作。准确地识别IP的归属地不仅可以帮助网络管理员追踪和定位潜在的网络攻击者&#xff0c;还可以用于网络流量分析、地理定位服务等方面。 以下将介绍几种高效识别IP归属地的方法。 使用IP归属地数据库 IP归属地数据库是一种存储…

FileNotFoundException:xxx(系统找不到指定的路径)

目录 前言 背景 解决方法 错误示例 前言 这次是有个两年前的项目吧&#xff0c;不知道为什么无法启动了。中间迭代了多个版本&#xff0c;现在另一个同事接手了&#xff0c;领导让看一下。因为时间间隔过长&#xff0c;问题处理比较费劲。其中有的是配置问题&#xff0c;比…

MySQL - 常用的命令

当涉及到具体的数据库操作时&#xff0c;我会给出实际的示例&#xff0c;以更清楚地说明每个命令的用法。 创建数据库&#xff1a; CREATE DATABASE students;列出数据库&#xff1a; SHOW DATABASES;使用数据库&#xff1a; USE students;创建表&#xff1a; CREATE TABL…

java篇 类的进阶0x06:可见性修饰符(访问修饰符)

文章目录 可见性修饰符&#xff08;访问修饰符&#xff09;成员变量都应该是 private构造方法可以是 private 的public 修饰的东西尽量不要改动非 public 的类&#xff0c;类名可以不和文件名相同protected 继承专属的访问控制 可见性修饰符&#xff08;访问修饰符&#xff09;…

学习笔记21 list

一、概述 有两种不同的方法来实现List接口。ArrayList类使用基于连续内存分配的实现&#xff0c;而LinkedList实现基于linked allocation。 list接口提供了一些方法&#xff1a; 二、The ArrayList and LinkedList Classes 1.构造方法 这两个类有相似的构造方法&#xff1a…

AVKit 播放

文章目录 - (void)testAVKit{NSString *fileName = @"ElephantSeals.mov";NSURL *fileURL = [[NSBundle mainBundle] URLForResource:fileNamewithExtension:nil]

Redis学习路线(2)—— Redis的数据结构

一、Redis的数据结构 Redis是一个Key-Value的数据库&#xff0c;key一般是String类型&#xff0c;不过Value的类型却有很多&#xff1a; String&#xff1a; Hello WorldHash&#xff1a; {name: "jack", age: 21}List&#xff1a; [A -> B -> C -> C]Set…

Unity进阶--物品,背包,角色管理器

文章目录 物品管理器背包管理器角色管理器 物品管理器 物品数据 Item.json&#xff08;json部分)&#xff08;Resources/Data/Item&#xff09; [{ "id": 1, "name": "新手剑", "des": "这是一把宝剑", "price": …

React的hooks---useLayoutEffect

useLayoutEffect 与 useEffect 类似&#xff0c;与 useEffect 在浏览器 layout 和 painting 完成后异步执行 effect 不同的是&#xff0c;它会在浏览器布局 layout 之后&#xff0c;painting 之前同步执行 effect useLayoutEffect 的执行时机对比如下&#xff1a; import Rea…

Spring Security OAuth2.0 - 学习笔记

一、OAuth基本概念 1、什么是OAuth2.0 OAuth2.0是一个开放标准&#xff0c;允许用户授权第三方应用程序访问他们存储在另外的服务提供者上的信息&#xff0c;而不需要将用户和密码提供给第三方应用或分享数据的所有内容。 2、四种认证方式 1&#xff09;授权码模式 2&#x…

CHI中的网络层

System address map □ 系统中每个Requester(包括RN和HN)必须有一个System Address Map(SAM)来决定一个request的target ID&#xff1b; □ SAM的范围可能只是简单的为所有发送的requests提供一个固定的node ID □ SAM具体的结构和格式是由具体实现决定的 □ SAM必须可以对全地…

kotlin高阶函数

kotlin高阶函数 函数式API:一个函数的入参数为Lambda表达式的函数就是函数式api 例子: public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {return filterTo(ArrayList<T>(), predicate) }上面这段函数: 首先这个函…

SciencePub学术 | 物联网类重点SCIEEI征稿中

SciencePub学术 刊源推荐: 物联网类重点SCIE&EI征稿中&#xff01;信息如下&#xff0c;录满为止&#xff1a; 一、期刊概况&#xff1a; 物联网类重点SCIE&EI 【期刊简介】IF&#xff1a;7.5-8.0&#xff0c;JCR1区&#xff0c;中科院1/2区TOP&#xff1b; 【出版社…