hyperf: 使用 bin/hyperf.php 命令行

一,查看帮助

$ php bin/hyperf.php help

二,查看可用的命令:

$ php bin/hyperf.php

三,创建一个命令

$ php bin/hyperf.php gen:command DemoCommand

代码如下:

<?php

declare(strict_types=1);

namespace App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;

#[Command]
class DemoCommand extends HyperfCommand
{
    public function __construct(protected ContainerInterface $container)
    {
        parent::__construct('demo:command');
    }

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

    public function handle()
    {
        echo "DomoCommand的handle\n";
        $this->line('Hello Hyperf!', 'info');
    }
}

执行命令:

 php bin/hyperf.php demo:command 
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
DomoCommand的handle
Hello Hyperf!
[DEBUG] Event Hyperf\Command\Event\AfterExecute handled by App\Listener\ResumeExitCoordinatorListener listener.

四,去掉命令行的调试信息

config/config.php

编辑如下:


return [
    'app_name' => env('APP_NAME', 'skeleton'),
    'app_env' => env('APP_ENV', 'dev'),
    'scan_cacheable' => env('SCAN_CACHEABLE', false),
    StdoutLoggerInterface::class => [
        'log_level' => [
            LogLevel::ALERT,
            LogLevel::CRITICAL,
            //LogLevel::DEBUG,
            LogLevel::EMERGENCY,
            LogLevel::ERROR,
            LogLevel::INFO,
            LogLevel::NOTICE,
            LogLevel::WARNING,
        ],
    ],
];

说明:给   //LogLevel::DEBUG 这行加了注释

效果:

$ php bin/hyperf.php demo:command 
DomoCommand的handle
Hello Hyperf!

 

posted @ 2025-01-25 11:26  刘宏缔的架构森林  阅读(202)  评论(0)    收藏  举报