va5ja/rdn-console
Composer 安装命令:
composer require va5ja/rdn-console
包简介
Zend Framework 2 Symfony Console module
README 文档
README
The RdnConsole module bridges the Symfony Console component with Zend Framework 2.
How to install
Use composer to require the va5ja/rdn-console package:
$ composer require va5ja/rdn-console:1.*
Activate the module by including it in your application.config.php file:
<?php return array( 'modules' => array( 'RdnConsole', // ... ), );
How to use
You can use the vendor/bin/console utility to run your commands. This utility might be in a different directory depending on your composer's bin-dir configuration.
The module will also take over all zf2 console routes. So you can simply run php public/index.php to run your commands as well.
Make sure you return your application variable $app in your public/index.php file:
// ... return Zend\Mvc\Application::init($config);
How to create commands
Create your commands inside your module, register them with the console command service locator, and finally inject the command into the console application.
Let's create a hello world command inside our App module:
1. Create command class
Create the class App\Console\Command\HelloWorld by extending RdnConsole\Command\AbstractCommand:
<?php namespace App\Console\Command; use RdnConsole\Command\AbstractCommand; use RdnConsole\Command\ConfigurableInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class HelloWorld extends AbstractCommand implements ConfigurableInterface { public function configure() { $this->adapter ->setName('app:hello-world') ->setDescription('Test hello world command') ; } public function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Hello world!'); } }
2. Register command with service locator
Place the following in your module.config.php file (or in your module's getConfig() method):
<?php return array( 'rdn_console_commands' => array( 'invokables' => array( 'App:HelloWorld' => 'App\Console\Command\HelloWorld', ), ), );
3. Inject command into console application
Now, place the following in your module.config.php file:
<?php return array( 'rdn_console' => array( 'commands' => array( 'App:HelloWorld', ), ), );
4. Run the command!
Now you can simply run vendor/bin/console app:hello-world to run this command.
Sample command
A sample RdnConsole:CacheClear command is provided with the module. You can enable it by including the following in your module.config.php:
<?php return array( 'rdn_console' => array( 'commands' => array( 'RdnConsole:CacheClear', ), ), );
Command Adapter
You'll notice the command uses an adapter to configure itself:
public function configure() { $this->adapter ->setName('app:hello-world') ->setDescription('Test hello world command') ; }
This is slightly different from the default Symfony workflow. You must always use this adapter to talk with symfony: to get the command name, get helpers, etc.
We use the adapter in order to separate the configuration from the execution code. The benefits of this are explained in the next section.
Factories
The best part about this module is that it allows us to separate a command's configuration from it's actual execution code.
What does this mean? Well, let's say you have a lot of commands, each with multiple dependencies. If we were to create our commands the default way, Symfony would load each command and all its dependencies every time.
But this is inefficient and slows down the application. Instead, this module will only load the initial configuration required to display the available list of commands.
Then when you are ready to actually execute the command, the module will use the factory pattern to create the command, load all its dependencies, and finally run the execution code.
How to create commands using factories
1. Create command class
Create your command class like you would normally, but don't implement the ConfigurableInterface. We also include any external dependencies:
<?php namespace App\Console\Command; use RdnConsole\Command\AbstractCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class HelloWorld extends AbstractCommand { protected $dependency; public function __construct($dependency) { $this->dependency = $dependency; } public function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Hello world!'); } }
2. Create command factory
Next, we create the factory for this class by extending the RdnConsole\Factory\Command\AbstractCommandFactory class. This factory class will contain the command's configuration. You must also implement the create() method to create the actual command object:
<?php namespace App\Factory\Console\Command; use App\Console\Command; use RdnConsole\Factory\Command\AbstractCommandFactory; class HelloWorld extends AbstractCommandFactory { public function configure() { $this->adapter ->setName('app:hello-world') ->setDescription('Test hello world command') ; } protected function create() { $dependency = $this->service('External dependency'); return new Command\HelloWorld($dependency); } }
The AbstractCommandFactory class extends the AbstractFactory class from the rdn-factory module.
3. Register command factory with service locator
Finally, we register this factory with the service locator replacing our previous invokable class:
<?php return array( 'rdn_console_commands' => array( 'factories' => array( 'App:HelloWorld' => 'App\Factory\Console\Command\HelloWorld', ), ), );
va5ja/rdn-console 适用场景与选型建议
va5ja/rdn-console 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「zend」 「zf2」 「console」 「SF2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 va5ja/rdn-console 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 va5ja/rdn-console 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 va5ja/rdn-console 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LosLog provides some log utility
EdpModuleLayouts is very simple Laminas module for making module-specific layouts insanely easy.
The bundle for easy using json-rpc api on your project
Polyfill for mb_ereg(), mb_eregi(), mb_ereg_match(), and mb_ereg_replace*() functions; primary use case is for mbstring on Windows 7.4+
Zend Framework module to leverage the symfony dependency injection container
RCM User HTML views/pages
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-07