2016-09-29 91 views
2

我想重載方法FOS FlashListener::getSubscribedEvents(),只是徵求意見REGISTRATION_COMPLETED行:FOSUserBundle FlashListener刪除REGISTRATION_COMPLETED消息

// vendor/friendsofsymfony/user-bundle/EventListener/FlashListener.php 

public static function getSubscribedEvents() { 
    return array(
     FOSUserEvents::CHANGE_PASSWORD_COMPLETED => 'addSuccessFlash', 
     FOSUserEvents::GROUP_CREATE_COMPLETED => 'addSuccessFlash', 
     FOSUserEvents::GROUP_DELETE_COMPLETED => 'addSuccessFlash', 
     FOSUserEvents::GROUP_EDIT_COMPLETED => 'addSuccessFlash', 
     FOSUserEvents::PROFILE_EDIT_COMPLETED => 'addSuccessFlash', 
     FOSUserEvents::REGISTRATION_COMPLETED => 'addSuccessFlash', 
     FOSUserEvents::RESETTING_RESET_COMPLETED => 'addSuccessFlash', 
    ); 
} 

有一些相關信息有:

// vendor/friendsofsymfony/user-bundle/Resources/config/flash_notifications.xml 

<?xml version="1.0" encoding="UTF-8" ?> 
<container xmlns="http://symfony.com/schema/dic/services" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> 
    <services> 
     <service id="fos_user.listener.flash" class="FOS\UserBundle\EventListener\FlashListener"> 
      <tag name="kernel.event_subscriber" /> 
      <argument type="service" id="session" /> 
      <argument type="service" id="translator" /> 
     </service> 
    </services> 
</container> 

我已經做到了:

<?php 
// src/XXXX/UserBundle/EventListener/FlashListener.php 
namespace XXXX\UserBundle\EventListener; 
use FOS\UserBundle\EventListener\FlashListener AS BaseListener; 
use FOS\UserBundle\FOSUserEvents; 
class FlashListener extends BaseListener { 
    public static function getSubscribedEvents() { 
     return [ 
      FOSUserEvents::CHANGE_PASSWORD_COMPLETED => 'addSuccessFlash', 
      FOSUserEvents::GROUP_CREATE_COMPLETED => 'addSuccessFlash', 
      FOSUserEvents::GROUP_DELETE_COMPLETED => 'addSuccessFlash', 
      FOSUserEvents::GROUP_EDIT_COMPLETED => 'addSuccessFlash', 
      FOSUserEvents::PROFILE_EDIT_COMPLETED => 'addSuccessFlash', 
      // FOSUserEvents::REGISTRATION_COMPLETED => 'addSuccessFlash', 
      FOSUserEvents::RESETTING_RESET_COMPLETED => 'addSuccessFlash', 
     ]; 
    } 
} 

and that:

#src/XXXX/XXXXXBundle/Resources/config/services.yml 
services: 
    fos_user.listener.flash: 
     class: XXXX\UserBundle\EventListener\FlashListener 
     arguments: ['@session','@translator'] 
     tags: 
      - { name: kernel.event_subscriber } 

但訂閱後我仍然有消息。

有人可以幫我嗎?

回答

1

由於事件訂戶的工作方式,即使該重寫看起來是覆蓋正常服務的最佳實踐方式(如果沒有.class參數),也可能無法實現。

相反,您可能需要在套件自己的CompilerPass中將其覆蓋更深一層。

// src/Acme/DemoBundle/DependencyInjection/Compiler/OverrideServiceCompilerPass.php 
namespace Acme\DemoBundle\DependencyInjection\Compiler; 

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 

class OverrideServiceCompilerPass implements CompilerPassInterface 
{ 
    public function process(ContainerBuilder $container) 
    { 
     $definition = $container->getDefinition('original-service-id'); 
     $definition->setClass('Acme\DemoBundle\YourService'); 
    } 
} 

又見How to override FOSUserBundle's EmailConfirmationListener

0

非常感謝您的幫助!

我已經做了的Durty鉤(等待有更多的時間在這方面努力,我會稍後再試您的解決方案):

<?php // src/ACME/UserBundle/EventListener/FlashListener.php 

namespace ACME\UserBundle\EventListener; 

use FOS\UserBundle\FOSUserEvents; 
use Symfony\Component\EventDispatcher\Event; 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\HttpFoundation\Session\Session; 
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 

class FlashListener implements EventSubscriberInterface { 
    private $router; 
    private $session; 

    public function __construct(UrlGeneratorInterface $router, Session $session) { 
     $this->router = $router; 
     $this->session = $session; 
    } 
    public static function getSubscribedEvents() { 
     return [ 
      FOSUserEvents::REGISTRATION_COMPLETED => ['removeSuccessFlash', -1] 
     ]; 
    } 

    public function removeSuccessFlash(Event $event, $eventName = null) { 
     // on supprime le dernier message success : registration.flash.user_created 
     // méthode moche, essayer de surcharger la méthode 
     // FOS\UserBundle\EventListener\FlashListener::getSubscribedEvents() 
     $flashBag = $this->session->getFlashBag(); 

     if ($flashBag->peek('success')) { 
      $flashes = $flashBag->all(); 
      array_pop($flashes['success']); 
      if (!isset($flashes['success'][0])) { 
       unset($flashes['success']); 
      } 
      if ($flashes) { 
       $flashBag->setAll($flashes); 
      } 
     } 
    } 
}