ZF3共享事件管理器忽略优先级

时间:2018-03-12 10:45:32

标签: zend-framework2 zend-framework3

我有以下类和配置来监听配置文件的创建。一切正常,但现在我需要第二次听它,这应该在第一次之后执行。我以为我可以在这里使用优先级,但似乎这个优先级没有效果。

有什么想法吗?

配置

return [
    'listeners' => [
        CreateListener::class
    ]
]

ProfileService

class ProfileService implements EventManagerAwareInterface {

    use EventManagerAwareTrait;

    public function createProfile(Profile $profile) {
        $this->getEventManager()->trigger(__FUNCTION__, $this, ['profile' => $profile]);
        $this->profileRepository->save($profile);
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, ['profile' => $profile]);
    }

}

CreateListener

class CreateListener extends AbstractListenerAggregate {

    public function attach(EventManagerInterface $eventManager, $priority = 100) {
        // Priority of 100 seems to be ignored...

        $this->listeners[] = $eventManager->getSharedManager()->attach(
            ProfileService::class, 
            'createProfile.post',
            [$this, 'onPostCreateProfile'],
            $priority
        );
    }

    public function onPostCreateProfile(EventInterface $event) {
        // Do something
    }

}

1 个答案:

答案 0 :(得分:0)

根据我在评论中链接的the answer,您可以尝试以下操作吗?

(用你自己的命名取代命名)

namespace Demo\Listener;

use Demo\Event\DemoEvent;
use Zend\EventManager\Event;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;

class DemoListener implements ListenerAggregateInterface
{
    use EventManagerAwareTrait;

    // You had "$priority = 100", this will cause an error in PHP >7.1 as it does not match interface
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $sharedManager = $events->getSharedManager();

        $sharedManager->attach(
            ProfileService::class, 
            'createProfile',               // <<-- first function to execute
            [$this, 'createProfile'], 
            100
        );

        $sharedManager->attach(
            ProfileService::class, 
            'createProfilePost',           // <<-- second function to execute via daisy-chain
            [$this, 'onPostCreateProfile'], 
            100
        );
    }

    public function createProfile(Event $event)
    {
        // Gets passed along parameters from the ->trigger() function elsewhere
        $params = $event->getParams(); 

        $specificClass = $params[SpecificClass::class]; // get your event managed object

        //
        // Do your magic
        //

        // Don't forget to detach this function (Handler) so it doesn't accidentally run again
        $event->getEventManager()->getSharedManager()->clearListeners(get_class($specificClass), $event->getName());

        // Trigger events specific for the Entity/class (this "daisy-chains" events, allowing for follow-up functionality)
        $specificClass->getEventManager()->trigger(
            'createProfile.post',
            $specificClass ,
            [get_class($specificClass) => $specificClass ] // Params getting passed along
        );
    }

    public function createProfilePost(Event $event)
    {
        // This function is daisy-chained by the above. Copy contents, do magic
    }
}