2016-09-29 5 views
2

私はコメントをREGISTRATION_COMPLETEDライン、方法FOS FlashListener::getSubscribedEvents()をオーバーロードしたいと思います: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', 
     ]; 
    } 
} 

およびその値:

#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); 
      } 
     } 
    } 
} 
関連する問題