Symfony2:如何钩住从不同捆绑包的控制台命令启动的事件

Symfony2:如何钩住从不同捆绑包的控制台命令启动的事件,symfony,command,dispatcher,event-listener,symfony-2.4,Symfony,Command,Dispatcher,Event Listener,Symfony 2.4,我有两个不同的包 在第一个示例中,我实现了一个控制台命令。在命令执行期间,我需要启动一个可以被第二个bundle监听的“事件”,因为我需要执行自定义逻辑 第一个bundle必须与第二个bundle解耦(第一个bundle不能有第二个bundle的任何引用) 提前感谢。在第一个bundle命令中发送事件 <?php namespace FirstBundle\Command; class SomeCommand extends ContainerAwareCommand { pr

我有两个不同的包

在第一个示例中,我实现了一个控制台命令。在命令执行期间,我需要启动一个可以被第二个bundle监听的“事件”,因为我需要执行自定义逻辑

第一个bundle必须与第二个bundle解耦(第一个bundle不能有第二个bundle的任何引用)


提前感谢。

在第一个bundle命令中发送事件

<?php

namespace FirstBundle\Command;

class SomeCommand extends ContainerAwareCommand
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // ...
        $this->getContainer()->get('event_dispatcher')->dispatch(
            'my.event'
            new GenericEvent($someData)
        );
        // ...
    }
}

在第一个bundle命令中,调度一个事件

<?php

namespace FirstBundle\Command;

class SomeCommand extends ContainerAwareCommand
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // ...
        $this->getContainer()->get('event_dispatcher')->dispatch(
            'my.event'
            new GenericEvent($someData)
        );
        // ...
    }
}

在第一个bundle命令中,调度一个事件

<?php

namespace FirstBundle\Command;

class SomeCommand extends ContainerAwareCommand
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // ...
        $this->getContainer()->get('event_dispatcher')->dispatch(
            'my.event'
            new GenericEvent($someData)
        );
        // ...
    }
}

在第一个bundle命令中,调度一个事件

<?php

namespace FirstBundle\Command;

class SomeCommand extends ContainerAwareCommand
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // ...
        $this->getContainer()->get('event_dispatcher')->dispatch(
            'my.event'
            new GenericEvent($someData)
        );
        // ...
    }
}

在第一个包中,像往常一样在命令中分派事件。在分派事件时,还提供一个唯一的名称以及分派的事件

在第二个包中,创建一个监听器(class和yml/xml-config),它将侦听具有该唯一名称的事件

这样,第一个包将不知道第二个包。如果第二个包不存在,则没有侦听器将捕获该事件

现在唯一的问题是事件本身。我建议你看一下GenericEvent

另外,尽量不要传入在第一个bundle中定义的任何对象,否则会使第二个bundle依赖于第一个bundle。我会传递简单的数据,比如int、string、array


希望这有帮助。

在第一个包中,像往常一样在命令中分派事件。在分派事件时,还提供一个唯一的名称以及分派的事件

在第二个包中,创建一个监听器(class和yml/xml-config),它将侦听具有该唯一名称的事件

这样,第一个包将不知道第二个包。如果第二个包不存在,则没有侦听器将捕获该事件

现在唯一的问题是事件本身。我建议你看一下GenericEvent

另外,尽量不要传入在第一个bundle中定义的任何对象,否则会使第二个bundle依赖于第一个bundle。我会传递简单的数据,比如int、string、array


希望这有帮助。

在第一个包中,像往常一样在命令中分派事件。在分派事件时,还提供一个唯一的名称以及分派的事件

在第二个包中,创建一个监听器(class和yml/xml-config),它将侦听具有该唯一名称的事件

这样,第一个包将不知道第二个包。如果第二个包不存在,则没有侦听器将捕获该事件

现在唯一的问题是事件本身。我建议你看一下GenericEvent

另外,尽量不要传入在第一个bundle中定义的任何对象,否则会使第二个bundle依赖于第一个bundle。我会传递简单的数据,比如int、string、array


希望这有帮助。

在第一个包中,像往常一样在命令中分派事件。在分派事件时,还提供一个唯一的名称以及分派的事件

在第二个包中,创建一个监听器(class和yml/xml-config),它将侦听具有该唯一名称的事件

这样,第一个包将不知道第二个包。如果第二个包不存在,则没有侦听器将捕获该事件

现在唯一的问题是事件本身。我建议你看一下GenericEvent

另外,尽量不要传入在第一个bundle中定义的任何对象,否则会使第二个bundle依赖于第一个bundle。我会传递简单的数据,比如int、string、array

希望这有帮助