[SF] Symfony 在 console 中结合 Workerman
在web框架的console中,命令不再是直接指定入口文件,如以往 php test.php start,而是类似 php app/console do 的形式。
workerman 对命令的解析是 parseCommand 方法,里面主要是处理 $argv 全局变量。
那么我们只需要在自己的逻辑中对其重新赋值,满足 $argv[1] 是动作 start | stop | restart | ... 即可,那么剩余workerman参数就是 $argv[2],依次类推。
Symfony2 command:
namespace AppBundle\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Workerman\Connection\TcpConnection; use Workerman\Worker; /** * @author farwish <farwish(a)foxmail.com> * * Class DataWorkerCommand * @package AppBundle\Command */ class DataWorkerCommand extends BaseCommand { public function configure() { $this->setName('xc:data:worker') ->setDescription('启动服务') ->addArgument( 'subcommands', InputArgument::IS_ARRAY, '可选多个参数' ) ; } /** * app/console xc:data:worker start d [g] * app/console xc:data:worker stop * app/console xc:data:worker status * app/console xc:data:worker restart d [g] * * @param InputInterface $input * @param OutputInterface $output */ public function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); global $argv; /* Original data like Array ( [0] => worker.php [1] => start [2] => -d [3] => -g ) */ /* Console data like Array ( [0] => app/console [1] => xc:data:worker [2] => start [3] => d [4] => g ) */ // So redefine arguments if (isset($argv[2])) { $argv[1] = $argv[2]; if (isset($argv[3])) { $argv[2] = "-{$argv[3]}"; if (isset($argv[4])) { $argv[3] = "-{$argv[4]}"; } else { unset($argv[3]); } } else { unset($argv[2]); } } // worker $worker = new Worker("websocket://0.0.0.0:9000"); $worker->count = 4; $worker->onMessage = function ($connection, $data) { /* @var TcpConnection $connection */ $connection->send('hello'); }; Worker::runAll(); } }
Thats all.
Refer:Symfony结合Workerman
分类:
php
, php-Symfony
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架