Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
some refactoring with the key and the config
  • Loading branch information
Miliooo committed Oct 18, 2013
commit ef1ee688d3733832013fa1736557a8f7ed3aabc8
3 changes: 1 addition & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function getConfigTreeBuilder()
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')->defaultFalse()->end()
->scalarNode('flash_key')->defaultNull()->cannotBeEmpty()->end()
->scalarNode('service')->defaultValue('fos_message.flash_listener.default')->cannotBeEmpty()->end()
->scalarNode('flash_key')->defaultValue('success')->cannotBeEmpty()->end()
->end()
->end()
->arrayNode('search')
Expand Down
14 changes: 5 additions & 9 deletions DependencyInjection/FOSMessageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,12 @@ public function load(array $configs, ContainerBuilder $container)

$container->getDefinition('fos_message.recipients_data_transformer')
->replaceArgument(0, new Reference($config['user_transformer']));
if($config['flash_messages']['enabled'])

if ($config['flash_messages']['enabled'])
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this curly brace should be on the previous line

$loader->load('flash.xml');
$container->setAlias('fos_message.flash_listener', $config['flash_messages']['service']);
if($config['flash_messages']['flash_key'] !== null)
{
$container->getDefinition('fos_message.flash_listener.default')
->replaceArgument(2, $config['flash_messages']['flash_key']);
}
$loader->load('flash.xml');
$container->getDefinition('fos_message.flash_listener')
->replaceArgument(2, $config['flash_messages']['flash_key']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should make it a parameter instead of replacing it in the service definition directly IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it's an easy fix, now I don't check if that setting has been changed but just always set that option. A container doesn't get rebuild that many times so is there harm in this?

Edit: Think I understand what you mean now...I'll look into it

}
}
}
17 changes: 10 additions & 7 deletions EventListener/FlashListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,26 @@ class FlashListener implements EventSubscriberInterface
* @var Session
*/
protected $session;

/**
* The flash key
*
* @var string
*/
protected $key;

/**
* Constructor.
*
* @param Session $session The current session
* @param TranslatorInterface $translator A translator instance
* @param string $key An optional flashBag key which overwrites the default key
* @param string $key The flash key
*/
public function __construct(Session $session, TranslatorInterface $translator, $key = '')
public function __construct(Session $session, TranslatorInterface $translator, $key)
{
$this->session = $session;
$this->translator = $translator;
if (empty($key)) {
$this->key = 'success';
} else {
$this->key = $key;
}
$this->key = $key;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion Resources/config/flash.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
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">

<parameters>
<parameter key="fos_message.flash_listener.class">FOS\MessageBundle\EventListener\FlashListener</parameter>
</parameters>

<services>

<service id="fos_message.flash_listener.default" class="FOS\MessageBundle\EventListener\FlashListener">
<service id="fos_message.flash_listener" class="%fos_message.flash_listener.class%">
<argument type="service" id="session" />
<argument type="service" id="translator" />
<argument></argument>
Expand Down
29 changes: 3 additions & 26 deletions Tests/EventListener/FlashListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FlashListenerTest extends \PHPUnit_Framework_TestCase
private $session;
private $translator;
private $event;
private $key;

public function setUp()
{
Expand All @@ -23,7 +24,8 @@ public function setUp()
//if we use the interface getflashbag returns an error...
$this->session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock();
$this->translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
$this->listener = new FlashListener($this->session, $this->translator);
$this->key = 'success';
$this->listener = new FlashListener($this->session, $this->translator, $this->key);
}

/**
Expand Down Expand Up @@ -57,29 +59,4 @@ public function testAddFlashOnValidEventWithDefaultKey()

$this->listener->addSuccessFlash($this->event);
}

public function testAddFlashOnValidEventWithCustomKey()
{
$customKey = 'message_info';
$flashbagMock = $this->getMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');

$this->listener = new FlashListener($this->session, $this->translator, $customKey);

$this->event->expects($this->once())->method('getName')->will($this->returnValue(FOSMessageEvents::POST_SEND));

$this->translator->expects($this->once())
->method('trans')
->with('flash_post_send_success', array(), 'FOSMessageBundle')
->will($this->returnValue('translatedString'));

$this->session->expects($this->once())
->method('getFlashBag')
->will($this->returnValue($flashbagMock));

$flashbagMock->expects($this->once())
->method('add')
->with($customKey, 'translatedString');

$this->listener->addSuccessFlash($this->event);
}
}