mpoiriert / nucleus-bundle
可重用库
Requires
- php: >=5.3
- mpoiriert/invoker: *
- symfony/framework-bundle: ~2.4
Requires (Dev)
- phpunit/phpunit: 3.7.*
- symfony/symfony: ~2.4
Suggests
- mpoiriert/binder-bundle: Hide session usage via annotation on service attribute
- mpoiriert/business-rule-engine-bundle: Service that implement the specification pattern
- mpoiriert/nucleus-console-bundle: System to convert any service method in a command line task easily
- mpoiriert/nucleus-migration-bundle: Structure to manage migration script base on task execution
This package is auto-updated.
Last update: 2024-09-09 13:53:24 UTC
README
这是一个基础包,被其他Nucleus bundle使用。
它自身有一个通用的编译器传递,可以帮助修改容器以进行注解。
它还实现了Nucleus\Invoker\IInvoker作为服务。
它还重新定义了Nucleus\Bundle\CoreBundle\EventDispatcher\InvokerEventDispatcher的事件分发服务。
NucleusCompilerPass
如果你需要基于注解进行某些操作,可以使用NucleusCompilerPass来简化你的生活。
你必须定义一个注解,如doctrine注解项目中所述。
一旦完成,你必须创建一个类,该类将实现Nucleus\Bundle\CoreBundle\DependencyInjection\IAnnotationContainerGenerator,并将接收一个Nucleus\Bundle\CoreBundle\GenerationContext。
然后你需要在nucleus_core配置中添加你的注解和生成器之间的映射。
nucleus_core:
annotation_container_generators:
test_annotation:
annotationClass: Nucleus\Bundle\CoreBundle\Tests\DependencyInjection\Annotation
generatorClass: Nucleus\Bundle\CoreBundle\Tests\DependencyInjection\AnnotationTagGenerator
从那里你可以做任何你想做的事情,就像你在编译器传递中一样。
你还在Nucleus\Bundle\CoreBundle\DependencyInjection\Definition中有一些静态方法,可以帮助你。
如果你想看看示例,可以检查测试类Nucleus\Bundle\CoreBundle\Tests\DependencyInjection\AnnotationTagGenerator,以及mpoiriert/binder-bundle。
Nucleus\Bundle\CoreBundle\EventDispatcher\InvokerEventDispatcher
你应该通过使用InvokerEventDispatcher::notify()方法来使用新的事件分发器,该方法接收$subject、$eventName和$parameters作为参数。Invoker会将参数映射到事件的事件监听器。创建的"Event"对象是返回值。你不应该通过这种方式创建类型化的Event,因为服务不应该自己操作事件(除非你想停止事件的传播)。它像请求和控制器一样工作,但是是事件。
你也必须考虑你将如何进行单元测试,不再需要事件模拟,直接调用即可。你也可以重用服务的方法直接调用,而不必重新定义另一个方法或事件本身的介质。
<?php
$eventDispatcher = $client->getContainer()->get('event_dispatcher');
/* @var $eventDispatcher \Nucleus\Bundle\CoreBundle\EventDispatcher\InvokerEventDispatcher */
$eventDispatcher->addListenerService('WebTestCase.test',array('test_listener_service','listen'));
$user = new User();
$event = $eventDispatcher->notify($user,'User.test',array('param1'=>1));
class ListenerService
{
public function listen(User $user,IEvent $event, $eventName, $param1)
{
//...
}
}
Invoker负责映射$subject、$event、$eventName和传递给事件的任何参数。
你仍然可以创建类型化的事件,但你需要使用原始的EventDispatcherInterface::dispatch()方法。如果你想使用参数映射功能,请确保从Nucleus\Bundle\CoreBundle\EventDispatcher扩展。你可以重写Event::getParameters()方法并从事件返回处理后的参数。