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

12. 命令行

Hyperf 的命令行默认由 hyperf/command 组件提供,而该组件本身也是基于 symfony/console 的抽象。

一、安装

  • 通常来说该组件会默认存在,但如果您希望用于非 Hyperf 项目,也可通过下面的命令依赖 hyperf/command 组件。

    composer require hyperf/command
    

二、生成命令

  • 如果你有安装 hyperf/devtool 组件的话,可以通过 gen:command 命令来生成一个自定义命令。

  • 生成文件:app\Command\FooCommand.php

    php bin/hyperf.php gen:command FooCommand
    

三、类文件内容

<?php
namespace App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;

// 通过注解 Command 定义
#[Command]
class FooCommand extends HyperfCommand
{
    public function __construct(protected ContainerInterface $container)
    {
        // 参加为执行命令
        parent::__construct('foo:test');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
        // 定义命令 中的 name参数
        $this->addArgument('name', InputArgument::OPTIONAL, '这是name参数', 'guanyuyan');
    }

    public function handle()
    {
        // 取命令行中 传入的 name参数
        $name = $this->input->getArgument('name');
        $this->line(sprintf('Hello %s!', $name), 'info');
    }
}

四、使用

php bin/hyperf.php foo:test name:abc

在这里插入图片描述


http://www.kler.cn/news/361190.html

相关文章:

  • Lab3.1:Priority Sorted Doubly Linked List
  • Android 13 修改系统源码强制夸克浏览器支持横竖屏显示
  • Elasticsearch封装公共索引增删改查
  • C语言(十六)函数综合(二)递归 --- 辩论赛经验谈
  • 【厦门大学附属第一医院(互联网医院)-注册安全分析报告-无验证方式导致安全隐患】
  • API接口的未来展望:构建更加智能、安全、高效的数字世界
  • 【ARM】ARM架构参考手册_Part B 内存和系统架构(2)
  • Docker基础部署
  • 监控易监测对象及指标之:JBoss 7.1.x中间件监控
  • 第21~22周Java主流框架入门-Spring 3.SpringJDBC事务管理
  • 【linux开发-Qt】-Qt多线程开发
  • Java Swing的优秀开源项目学习推荐(UI、数据结构与设计模式)
  • Unity学习记录-API
  • 防蓝光护眼灯什么牌子好?五款防蓝光效果好的护眼台灯
  • npm配置阿里镜像库教程
  • SEO基础:什么是SERP?【百度SEO专家】
  • 【开发语言】c++的发展前景
  • 外包干了2年,技术原地踏步。。。。。
  • GIT常用操作及多人提交代码的工作流程
  • ORB-SLAM2 ---- Tracking::StereoInitialization()