PHP code example of myqee / hyperf

1. Go to this page and download the library: Download myqee/hyperf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

myqee / hyperf example snippets




declare(strict_types=1);

namespace App\Command;

use Hyperf\Contract\ConfigInterface;
use MyQEE\Hyperf\ServerHyperfFactory;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * 放在 app/Command/StartTestServer.php 
 * 或者 `config/autoload/annotations.php` 中设置的 scan 会扫描目录中
 * @Command
 */
class StartTestServer extends SymfonyCommand
{
    /**
     * @var ContainerInterface
     */
    private $container;
    
    protected $description = 'Start the test server.';

    public function __construct(ContainerInterface $container) {
        parent::__construct('server:test');     // 命令名称
        $this->container = $container;
    }

    protected function configure() {
        $this->setDescription($this->description);
        $this->addOption('daemon', 'd', InputOption::VALUE_NONE, 'daemonize server process');
    }

    protected function parseOption(InputInterface $input) {
         /**
         * @var $configFactory \MyQEE\Hyperf\Config
         */
        $configFactory = $this->container->get(ConfigInterface::class);
        $config = $configFactory->get('myqee');
        
        $daemon = $input->getOption('daemon');
        if ($daemon) {
            $config['swoole']['daemonize'] = 1;
        }
        $configFactory->set('myqee', $config);
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        \Swoole\Runtime::enableCoroutine(true);

        # 处理自定义的参数(可选) ---------------
        $this->parseOption($input);


        # 启动服务 --------------------------
        /**
         * @var $serverFactory ServerHyperfFactory
         */
        $serverFactory = $this->container->get(ServerHyperfFactory::class);
        # 安装服务
        $serverFactory->setup();
        # 服务启动
        $serverFactory->start();
        # ---------------------------------


        # 若不想启动服务器,而是直接cli执行一些任务,但是又希望Config生效(比如log、php的ini等)
        # 可以这样做:
         /**
         * @var $configFactory \MyQEE\Hyperf\Config
         */
        $configFactory = $this->container->get(ConfigInterface::class);
        # 这样配置就会初始化
        $configFactory->setup();

        // do something
        // ......
    }
}