松饼 / 连接器
适用于即将推出的PSR-14标准。目前支持0.6.0版本
0.6.1
2019-05-28 11:49 UTC
Requires
- php: ~7.2
- psr/event-dispatcher: ^0.6.0, <1.0.0
- psr/event-dispatcher-message: ^0.6.0
- psr/event-dispatcher-task: ^0.6.0
Requires (Dev)
- phploc/phploc: *@stable
- phpmd/phpmd: *@stable
- phpunit/phpunit: *@stable
- sebastian/phpcpd: *@stable
Provides
This package is auto-updated.
Last update: 2024-08-28 23:06:57 UTC
README
Nexus
PSR-14事件调度器实现
(遵守PSR-12)
安装
在大多数情况下,只需通过Composer安装即可。
composer require scones/nexus "*@stable"
用法
以下示例来自测试,因此当然存在一些冗余。
消息
用法相当简单。
- 您定义一个消息类。
- 您提供一个可调用的监听器,它需要一个参数,该参数必须与它应该监听的消息类型匹配。
- 您使用要传播的消息实例调用通知器
use Psr\EventDispatcher\MessageInterface; use Nexus\StandardListenerProvider; use Nexus\SynchronousNotifier; class SomeMessage implements MessageInterface { } class SomeOtherMessage implements MessageInterface { } $provider = new StandardListenerProvider(); $notifier = new SynchronousNotifier($provider); $provider->addListener(function (SomeMessage $someMessage) { echo "some cool message reaction code goes here\n"; }); $provider->addListener(function (SomeMessage $someMessage) { echo "this message seems worth logging!\n"; }); $provider->addListener(function (SomeOtherMessage $someOtherMessage) { echo "oh, the other message was dispatched!\n"; }); $testMessage = new SomeMessage(); $notifier->notify($testMessage);
这将当然导致
some cool message reaction code goes here
this message seems worth logging!
任务
再次,用法相当简单。
- 您定义一个任务类。
- 您提供一个可调用的监听器,它需要一个参数,该参数必须与它应该监听的任务类型匹配。
- 您使用要传播的任务实例调用处理器
- 所有任务都处理完毕后,返回的结果是修改后的任务实例。
use Nexus\StandardListenerProvider; use Nexus\SynchronousProcessor; use Psr\EventDispatcher\TaskInterface; class SomeTask implements TaskInterface { private $accessCounter = 0; public function increaseCounter(): void { $this->accessCounter++; } public function getCounter(): int { return $this->accessCounter; } } class SomeOtherTask implements TaskInterface { } $provider = new StandardListenerProvider(); $processor = new SynchronousProcessor($provider); $provider->addListener(function (SomeTask $someTask) { echo "some task working!\n"; $someTask->increaseCounter(); return $someTask; }); $provider->addListener(function (SomeTask $someTask) { echo "some task working again!\n"; $someTask->increaseCounter(); $someTask->increaseCounter(); return $someTask; }); $provider->addListener(function (SomeOtherTask $someOtherTask) { echo "the other Task!"; return $someOtherTask; }); $testTask = new SomeTask(); $testTaskResult = $processor->process($testTask); echo "task result was: {$testTaskResult->getCounter()}\n";
这将当然导致
some task working!
some task working again!
task result was: 3