spekkionu / domain-dispatcher
域名命令分发器
1.0.0
2016-10-19 16:33 UTC
Requires
- php: ~5.6|~7.0
- league/container: ^2.2
Requires (Dev)
- phpunit/phpunit: ^5.5
- squizlabs/php_codesniffer: ^2.7
This package is auto-updated.
Last update: 2024-09-12 12:59:01 UTC
README
与 league/container 集成的命令分发器。
安装
通过 Composer
$ composer require spekkionu/domain-dispatcher
使用方法
您必须首先在 league/container 实例中注册服务提供者。
$container->addServiceProvider('Spekkionu\DomainDispatcher\DispatcherServiceProvider');
然后,您可以从容器中拉取分发器以使用它。
$container = new \League\Container\Container(); $container->delegate( new \League\Container\ReflectionContainer ); $dispatcher = $container->get('Spekkionu\DomainDispatcher\Dispatcher'); $command = new MyCommand($var1, $var2); $dispatcher->dispatch($command);
编写命令
class MyCommand { /** * @var User */ private $user; /** * You can add any arguments you need to the constructor */ public function __construct(User $user) { $this->user = $user; } /** * The command must have a handle method. * Any dependencies for the handle method will be automatically resolved by the container * Whatever you return here will be returned by the dispatch call */ public function handle(EmailSender $mailer, Logger $logger) { // Your code goes here $result = $mailer->sendWelcomeEmail($user); $logger->log('Welcome email sent to user'); return $result; } }
$user = new User(); $user->name = 'Bob'; $user->email = 'email@example.com'; $command = new MyCommand($user); $result = $dispatcher->dispatch($command);
测试
$ composer test
许可协议
MIT 许可协议(MIT)。有关更多信息,请参阅许可文件。