atphp / event
为 Symfony EventDispatcher 组件提供更多功能的包装器。
v0.1.4
2014-09-09 07:12 UTC
Requires
- php: >=5.3.8
- symfony/event-dispatcher: ~2.5.0
Requires (Dev)
- symfony/event-dispatcher: ~2.5.0
README
为 Symfony Event Dispatcher 提供更多功能的包装器。
<?php use Symfony\Component\EventDispatcher\Event; use AndTruong\Common\EventAware; class MyClass extends EventAware { public function myEventAwareMethod() { $this->dispatch('my.event.before'); $event = new Event(); $this->dispatch('my.event.after', $event); // or simpler $this->trigger('my.other.event', $this, ['param 1', 'param 2']); } } // Class usage $myobj = new MyClass(); $myobj->getDispatcher()->addListener('my.event.before', function(\AndyTruong\Common\Event $e) { $e->getTarget(); // instance of MyClass $e->getParams(); // ['param 1', 'param 2'] }); $myobj->myEventAwareMethod();
结果收集
在实际项目中,我们经常从外部代码中收集结果。这可以像这样轻松完成
<?php $myobj = new MyClass(); $myobj->getDispatcher()->addListener('my.results.collecting.event', function(\AndyTruong\Common\Event $e) { $e->addResult("Hello there!"); }); $myobj->collectResults('my.results.collecting.event'); // ["Hello there!"] // to validate input $myobj->collectResults('my.results.collecting.event', null, null, [ function($input) { if (!is_string($input)) { throw new \Exception('Input must be string!'); } } ]); // ["Hello there!"]