EasySwoole 框架接入 HyperfCommand

倉(cāng)庫(kù)地址

試用 EasySwoole 自定義命令

創(chuàng)建文件


<?php

declare(strict_types=1);

namespace App\Command;

use EasySwoole\EasySwoole\Command\CommandInterface;

class DemoCommand implements CommandInterface

{

    public function commandName(): string

    {

        return 'demo:command';

    }

    public function exec(array $args): ?string

    {

        var_dump('Hello World');

        return 'success';

    }

    public function help(array $args): ?string

    {

        return 'help';

    }

}

運(yùn)行


$ php easyswoole                       

  ______                          _____                              _

|  ____|                        / ____|                            | |

| |__      __ _  ___  _  _  | (___  __      __  ___    ___  | |  ___

|  __|    / _` | / __| | | | |  \___ \  \ \ /\ / /  / _ \  / _ \  | |  / _ \

| |____  | (_| | \__ \ | |_| |  ____) |  \ V  V /  | (_) | | (_) | | | |  __/

|______|  \__,_| |___/  \__, | |_____/    \_/\_/    \___/  \___/  |_|  \___|

                          __/ |

                        |___/

Welcome To EASYSWOOLE Command Console!

Usage: php easyswoole [command] [arg]

Get help : php easyswoole help [command]

Current Register Command:

demo:command

help

install

start

stop

reload

phpunit

$ php easyswoole demo:command

string(11) "Hello World"

success

不得不說,還是相當(dāng)簡(jiǎn)潔的。

改造

接下來(lái),讓我們開始改造一部分代碼辕坝,給 EasySwoole 插上 Hyperf 的 Command。

EasySwoole 運(yùn)行模式十分簡(jiǎn)單荐健,所有的命令都保存在 CommandContainer 中酱畅,所以我們大可以修改入口文件,把其中的命令全部查出來(lái)江场,動(dòng)態(tài)翻譯成 HyperfCommand纺酸,然后直接運(yùn)行 HyperfCommand 就可以了。

為了不與 easyswoole 命令行沖突址否,我們新建一個(gè) hyperf 好了餐蔬。

首先我們創(chuàng)建一個(gè)組件


$ composer create hyperf/component-creater hyperf

Installing hyperf/component-creater (v1.1.1)

  - Installing hyperf/component-creater (v1.1.1): Downloading (100%)       

Created project in hyperf

> Installer\Script::install

Setting up optional packages

What is your component name (hyperf/demo): hyperf-cloud/easyswoole-command

What is your component license (MIT) :

What is your component description : HyperfCommand for EasySwoole

What is your namespace (HyperfCloud\EasyswooleCommand):

Removing installer development dependencies

  Do you want to use hyperf/framework component ?

  [1] yes

  [n] None of the above

  Make your selection or type a composer package name and version (n):

  Do you want to use hyperf/di component ?

  [1] yes

  [n] None of the above

  Make your selection or type a composer package name and version (n):

...

并給組件增加 "hyperf/command": "1.1.*" 依賴碎紊。

下面修改根目錄 composer.json


{

    "require": {

        "easyswoole/easyswoole": "3.x",

        "hyperf-cloud/easyswoole-command": "dev-master"

    },

    "require-dev": {

        "swoft/swoole-ide-helper": "^4.2",

        "friendsofphp/php-cs-fixer": "^2.14",

        "mockery/mockery": "^1.0",

        "phpstan/phpstan": "^0.11.2"

    },

    "autoload": {

        "psr-4": {

            "App\\": "App/"

        }

    },

    "minimum-stability": "dev",

    "prefer-stable": true,

    "config": {

        "sort-packages": true

    },

    "scripts": {

        "test": "co-phpunit -c phpunit.xml --colors=always",

        "cs-fix": "php-cs-fixer fix $1",

        "analyse": "phpstan analyse --memory-limit 300M -l 0 -c phpstan.neon ./App"

    },

    "repositories": {

        "hyperf": {

            "type": "path",

            "url": "./hyperf"

        },

        "packagist": {

            "type": "composer",

            "url": "https://mirrors.aliyun.com/composer"

        }

    }

}

接管CommandInterface

讓我們創(chuàng)建一個(gè) EasySwooleCommand 來(lái)接管所有的 CommandInterface


<?php

declare(strict_types=1);

namespace HyperfCloud\EasyswooleCommand;

use EasySwoole\EasySwoole\Command\CommandInterface;

use EasySwoole\EasySwoole\Core;

use Hyperf\Command\Command;

use Symfony\Component\Console\Input\InputOption;

class EasySwooleCommand extends Command

{

    /**

    * @var CommandInterface

    */

    protected $command;

    /**

    * @var bool

    */

    protected $coroutine = false;

    public function __construct(CommandInterface $command)

    {

        parent::__construct($command->commandName());

        $this->command = $command;

    }

    public function configure()

    {

        $this->addOption('args', 'a', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'EasySwoole 入?yún)?, []);

    }

    public function handle()

    {

        $args = $this->input->getOption('args');

        if (in_array('produce', $args)) {

            Core::getInstance()->setIsDev(false);

        }

        Core::getInstance()->initialize();

        $result = $this->command->exec($args);

        $this->output->success($result);

    }

}

增加 Application 初始化所有 CommandContainer 中的 Command仗考。


<?php

declare(strict_types=1);

namespace HyperfCloud\EasyswooleCommand;

use EasySwoole\Component\Singleton;

use EasySwoole\EasySwoole\Command\CommandContainer;

use Hyperf\Command\Command;

use Hyperf\Contract\ApplicationInterface;

use Symfony\Component\Console\Application as SymfonyApplication;

class Application implements ApplicationInterface

{

    use Singleton;

    protected $commands;

    public function __construct()

    {

        $container = CommandContainer::getInstance();

        $list = $container->getCommandList();

        foreach ($list as $name) {

            $this->commands[] = new EasySwooleCommand($container->get($name));

        }

    }

    public function add(Command $command)

    {

        $this->commands[] = $command;

    }

    public function run()

    {

        $application = new SymfonyApplication();

        foreach ($this->commands as $command) {

            $application->add($command);

        }

        return $application->run();

    }

}

最后改造入口函數(shù)


<?php

declare(strict_types=1);

use EasySwoole\EasySwoole\Command\CommandRunner;

use HyperfCloud\EasyswooleCommand\Application;

defined('IN_PHAR') or define('IN_PHAR', boolval(\Phar::running(false)));

defined('RUNNING_ROOT') or define('RUNNING_ROOT', realpath(getcwd()));

defined('EASYSWOOLE_ROOT') or define('EASYSWOOLE_ROOT', IN_PHAR ? \Phar::running() : realpath(getcwd()));

$file = EASYSWOOLE_ROOT . '/vendor/autoload.php';

if (file_exists($file)) {

    require $file;

} else {

    die("include composer autoload.php fail\n");

}

// 初始化 CommandContainer

CommandRunner::getInstance();

if (file_exists(EASYSWOOLE_ROOT . '/bootstrap.php')) {

    require_once EASYSWOOLE_ROOT . '/bootstrap.php';

}

Application::getInstance()->run();

執(zhí)行命令 demo:command


$ php hyperf.php demo:command

string(11) "Hello World"

[OK] success                                                                                                         

啟動(dòng) Server


$ php hyperf.php start -a produce

創(chuàng)建 HyperfCommand

接下來(lái)必搞,我們創(chuàng)建一個(gè) HyperfCommand 看看效果梅割。


<?php

declare(strict_types=1);

namespace App\Command;

use Hyperf\Command\Command;

class Demo2Command extends Command

{

    public function __construct()

    {

        parent::__construct('demo:command2');

    }

    public function handle()

    {

        var_dump('Hello Hyperf Command.');

    }

}

修改 bootstrap.php


<?php

declare(strict_types=1);

use EasySwoole\EasySwoole\Command\CommandContainer;

use App\Command\{DemoCommand, Demo2Command};

use HyperfCloud\EasyswooleCommand\Application;

CommandContainer::getInstance()->set(new DemoCommand());

Application::getInstance()->add(new Demo2Command());

執(zhí)行結(jié)果


$ php hyperf.php demo:command2

string(21) "Hello Hyperf Command."

寫在最后

Hyperf 是基于 Swoole 4.4+ 實(shí)現(xiàn)的高性能惦银、高靈活性的 PHP 協(xié)程框架,內(nèi)置協(xié)程服務(wù)器及大量常用的組件,性能較傳統(tǒng)基于 PHP-FPM 的框架有質(zhì)的提升,提供超高性能的同時(shí),也保持著極其靈活的可擴(kuò)展性,標(biāo)準(zhǔn)組件均基于 PSR 標(biāo)準(zhǔn) 實(shí)現(xiàn)田盈,基于強(qiáng)大的依賴注入設(shè)計(jì)瓷式,保證了絕大部分組件或類都是 可替換 與 可復(fù)用 的。

框架組件庫(kù)除了常見的協(xié)程版的 MySQL 客戶端踱卵、Redis 客戶端廊驼,還為您準(zhǔn)備了協(xié)程版的 Eloquent ORM、WebSocket 服務(wù)端及客戶端惋砂、JSON RPC 服務(wù)端及客戶端妒挎、GRPC 服務(wù)端及客戶端、Zipkin/Jaeger (OpenTracing) 客戶端西饵、Guzzle HTTP 客戶端酝掩、Elasticsearch 客戶端、Consul 客戶端眷柔、ETCD 客戶端期虾、AMQP 組件、Apollo 配置中心驯嘱、阿里云 ACM 應(yīng)用配置管理镶苞、ETCD 配置中心、基于令牌桶算法的限流器鞠评、通用連接池茂蚓、熔斷器、Swagger 文檔生成剃幌、Swoole Tracker聋涨、Blade 和 Smarty 視圖引擎、Snowflake 全局ID生成器 等組件负乡,省去了自己實(shí)現(xiàn)對(duì)應(yīng)協(xié)程版本的麻煩牛郑。

Hyperf 還提供了 基于 PSR-11 的依賴注入容器、注解敬鬓、AOP 面向切面編程淹朋、基于 PSR-15 的中間件笙各、自定義進(jìn)程、基于 PSR-14 的事件管理器础芍、Redis/RabbitMQ 消息隊(duì)列杈抢、自動(dòng)模型緩存、基于 PSR-16 的緩存仑性、Crontab 秒級(jí)定時(shí)任務(wù)惶楼、Translation 國(guó)際化、Validation 驗(yàn)證器 等非常便捷的功能诊杆,滿足豐富的技術(shù)場(chǎng)景和業(yè)務(wù)場(chǎng)景歼捐,開箱即用。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末晨汹,一起剝皮案震驚了整個(gè)濱河市豹储,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌淘这,老刑警劉巖剥扣,帶你破解...
    沈念sama閱讀 216,651評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異铝穷,居然都是意外死亡钠怯,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門曙聂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)晦炊,“玉大人,你說我怎么就攤上這事宁脊《瞎” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵朦佩,是天一觀的道長(zhǎng)并思。 經(jīng)常有香客問我庐氮,道長(zhǎng)语稠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評(píng)論 1 292
  • 正文 為了忘掉前任弄砍,我火速辦了婚禮仙畦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘音婶。我一直安慰自己慨畸,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評(píng)論 6 388
  • 文/花漫 我一把揭開白布衣式。 她就那樣靜靜地躺著寸士,像睡著了一般檐什。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上弱卡,一...
    開封第一講書人閱讀 51,198評(píng)論 1 299
  • 那天乃正,我揣著相機(jī)與錄音,去河邊找鬼婶博。 笑死瓮具,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的凡人。 我是一名探鬼主播名党,決...
    沈念sama閱讀 40,084評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼挠轴!你這毒婦竟也來(lái)了传睹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤忠荞,失蹤者是張志新(化名)和其女友劉穎蒋歌,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體委煤,經(jīng)...
    沈念sama閱讀 45,341評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡堂油,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了碧绞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片府框。...
    茶點(diǎn)故事閱讀 39,731評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖讥邻,靈堂內(nèi)的尸體忽然破棺而出迫靖,到底是詐尸還是另有隱情,我是刑警寧澤兴使,帶...
    沈念sama閱讀 35,430評(píng)論 5 343
  • 正文 年R本政府宣布系宜,位于F島的核電站,受9級(jí)特大地震影響发魄,放射性物質(zhì)發(fā)生泄漏盹牧。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評(píng)論 3 326
  • 文/蒙蒙 一励幼、第九天 我趴在偏房一處隱蔽的房頂上張望汰寓。 院中可真熱鬧,春花似錦苹粟、人聲如沸有滑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)毛好。三九已至望艺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肌访,已是汗流浹背荣茫。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留场靴,地道東北人啡莉。 一個(gè)月前我還...
    沈念sama閱讀 47,743評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像旨剥,于是被迫代替她去往敵國(guó)和親咧欣。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容