Skip to content

Commit

Permalink
[HttpFoundation] Allow flash messages to have multiple messages per t…
Browse files Browse the repository at this point in the history
…ype.
  • Loading branch information
Drak committed Mar 15, 2012
1 parent 85d4068 commit 84c2e3c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 51 deletions.
Expand Up @@ -62,7 +62,7 @@ public function showFlashAction()
$session = $request->getSession(); $session = $request->getSession();


if ($session->getFlashBag()->has('notice')) { if ($session->getFlashBag()->has('notice')) {
$output = $session->getFlashBag()->get('notice'); list($output) = $session->getFlashBag()->get('notice');
} else { } else {
$output = 'No flash was set.'; $output = 'No flash was set.';
} }
Expand Down
Expand Up @@ -42,13 +42,13 @@ public function testFlash()


$this->assertTrue($helper->hasFlash('notice')); $this->assertTrue($helper->hasFlash('notice'));


$this->assertEquals('bar', $helper->getFlash('notice')); $this->assertEquals(array('bar'), $helper->getFlash('notice'));
} }


public function testGetFlashes() public function testGetFlashes()
{ {
$helper = new SessionHelper($this->request); $helper = new SessionHelper($this->request);
$this->assertEquals(array('notice' => 'bar'), $helper->getFlashes()); $this->assertEquals(array('notice' => array('bar')), $helper->getFlashes());
} }


public function testGet() public function testGet()
Expand Down
Expand Up @@ -75,7 +75,15 @@ public function initialize(array &$flashes)
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function peek($type, $default = null) public function add($type, $message)
{
$this->flashes['new'][$type][] = $message;
}

/**
* {@inheritdoc}
*/
public function peek($type, array $default = array())
{ {
return $this->has($type) ? $this->flashes['display'][$type] : $default; return $this->has($type) ? $this->flashes['display'][$type] : $default;
} }
Expand All @@ -91,7 +99,7 @@ public function peekAll()
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function get($type, $default = null) public function get($type, array $default = array())
{ {
$return = $default; $return = $default;


Expand Down Expand Up @@ -129,17 +137,18 @@ public function setAll(array $messages)
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function set($type, $message) public function set($type, $messages)
{ {
$this->flashes['new'][$type] = $message; $messages = (array)$messages;
$this->flashes['new'][$type] = $messages;
} }


/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function has($type) public function has($type)
{ {
return array_key_exists($type, $this->flashes['display']); return array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type];

This comment has been minimized.

Copy link
@docteurklein

docteurklein Apr 16, 2013

Contributor

isset() would work here.

} }


/** /**
Expand Down
19 changes: 14 additions & 5 deletions src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php
Expand Up @@ -68,7 +68,15 @@ public function initialize(array &$flashes)
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function peek($type, $default = null) public function add($type, $message)
{
$this->flashes[$type][] = $message;
}

/**
* {@inheritdoc}
*/
public function peek($type, array $default =array())
{ {
return $this->has($type) ? $this->flashes[$type] : $default; return $this->has($type) ? $this->flashes[$type] : $default;
} }
Expand All @@ -84,7 +92,7 @@ public function peekAll()
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function get($type, $default = null) public function get($type, array $default = array())
{ {
if (!$this->has($type)) { if (!$this->has($type)) {
return $default; return $default;
Expand All @@ -111,9 +119,10 @@ public function all()
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function set($type, $message) public function set($type, $messages)
{ {
$this->flashes[$type] = $message; $messages = (array)$messages;
$this->flashes[$type] = $messages;
} }


/** /**
Expand All @@ -129,7 +138,7 @@ public function setAll(array $messages)
*/ */
public function has($type) public function has($type)
{ {
return array_key_exists($type, $this->flashes); return array_key_exists($type, $this->flashes) && $this->flashes[$type];
} }


/** /**
Expand Down
Expand Up @@ -32,11 +32,11 @@ function set($type, $message);
* Gets flash message for a given type. * Gets flash message for a given type.
* *
* @param string $type Message category type. * @param string $type Message category type.
* @param string $default Default value if $type doee not exist. * @param array $default Default value if $type doee not exist.
* *
* @return string * @return string
*/ */
function peek($type, $default = null); function peek($type, array $default = array());


/** /**
* Gets all flash messages. * Gets all flash messages.
Expand All @@ -49,11 +49,11 @@ function peekAll();
* Gets and clears flash from the stack. * Gets and clears flash from the stack.
* *
* @param string $type * @param string $type
* @param string $default Default value if $type doee not exist. * @param array $default Default value if $type doee not exist.
* *
* @return string * @return string
*/ */
function get($type, $default = null); function get($type, array $default = array());


/** /**
* Gets and clears flashes from the stack. * Gets and clears flashes from the stack.
Expand Down
Expand Up @@ -34,7 +34,7 @@ public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->bag = new FlashBag(); $this->bag = new FlashBag();
$this->array = array('new' => array('notice' => 'A previous flash message')); $this->array = array('new' => array('notice' => array('A previous flash message')));
$this->bag->initialize($this->array); $this->bag->initialize($this->array);
} }


Expand All @@ -47,16 +47,16 @@ public function tearDown()
public function testInitialize() public function testInitialize()
{ {
$bag = new FlashBag(); $bag = new FlashBag();
$array = array('new' => array('notice' => 'A previous flash message')); $array = array('new' => array('notice' => array('A previous flash message')));
$bag->initialize($array); $bag->initialize($array);
$this->assertEquals('A previous flash message', $bag->peek('notice')); $this->assertEquals(array('A previous flash message'), $bag->peek('notice'));
$array = array('new' => array( $array = array('new' => array(
'notice' => 'Something else', 'notice' => array('Something else'),
'error' => 'a', 'error' => array('a'),
)); ));
$bag->initialize($array); $bag->initialize($array);
$this->assertEquals('Something else', $bag->peek('notice')); $this->assertEquals(array('Something else'), $bag->peek('notice'));
$this->assertEquals('a', $bag->peek('error')); $this->assertEquals(array('a'), $bag->peek('error'));
} }


public function testGetStorageKey() public function testGetStorageKey()
Expand All @@ -75,16 +75,16 @@ public function testGetSetName()


public function testPeek() public function testPeek()
{ {
$this->assertNull($this->bag->peek('non_existing')); $this->assertEquals(array(), $this->bag->peek('non_existing'));
$this->assertEquals('default', $this->bag->peek('non_existing', 'default')); $this->assertEquals(array('default'), $this->bag->peek('non_existing', array('default')));
$this->assertEquals('A previous flash message', $this->bag->peek('notice')); $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
$this->assertEquals('A previous flash message', $this->bag->peek('notice')); $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
} }


public function testSet() public function testSet()
{ {
$this->bag->set('notice', 'Foo'); $this->bag->set('notice', 'Foo');
$this->assertNotEquals('Foo', $this->bag->peek('notice')); $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
} }


public function testHas() public function testHas()
Expand Down Expand Up @@ -123,10 +123,10 @@ public function testPeekAll()


public function testGet() public function testGet()
{ {
$this->assertNull($this->bag->get('non_existing')); $this->assertEquals(array(), $this->bag->get('non_existing'));
$this->assertEquals('default', $this->bag->get('non_existing', 'default')); $this->assertEquals(array('default'), $this->bag->get('non_existing', array('default')));
$this->assertEquals('A previous flash message', $this->bag->get('notice')); $this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
$this->assertNull($this->bag->get('notice')); $this->assertEquals(array(), $this->bag->get('notice'));
} }


public function testSetAll() public function testSetAll()
Expand All @@ -141,7 +141,7 @@ public function testAll()
$this->bag->set('notice', 'Foo'); $this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar'); $this->bag->set('error', 'Bar');
$this->assertEquals(array( $this->assertEquals(array(
'notice' => 'A previous flash message', 'notice' => array('A previous flash message'),
), $this->bag->all() ), $this->bag->all()
); );


Expand Down
Expand Up @@ -35,7 +35,7 @@ public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->bag = new FlashBag(); $this->bag = new FlashBag();
$this->array = array('notice' => 'A previous flash message'); $this->array = array('notice' => array('A previous flash message'));
$this->bag->initialize($this->array); $this->bag->initialize($this->array);
} }


Expand Down Expand Up @@ -71,27 +71,27 @@ public function testGetSetName()


public function testPeek() public function testPeek()
{ {
$this->assertNull($this->bag->peek('non_existing')); $this->assertEquals(array(), $this->bag->peek('non_existing'));
$this->assertEquals('default', $this->bag->peek('not_existing', 'default')); $this->assertEquals(array('default'), $this->bag->peek('not_existing', array('default')));
$this->assertEquals('A previous flash message', $this->bag->peek('notice')); $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
$this->assertEquals('A previous flash message', $this->bag->peek('notice')); $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
} }


public function testGet() public function testGet()
{ {
$this->assertNull($this->bag->get('non_existing')); $this->assertEquals(array(), $this->bag->get('non_existing'));
$this->assertEquals('default', $this->bag->get('not_existing', 'default')); $this->assertEquals(array('default'), $this->bag->get('not_existing', array('default')));
$this->assertEquals('A previous flash message', $this->bag->get('notice')); $this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
$this->assertNull($this->bag->get('notice')); $this->assertEquals(array(), $this->bag->get('notice'));
} }


public function testAll() public function testAll()
{ {
$this->bag->set('notice', 'Foo'); $this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar'); $this->bag->set('error', 'Bar');
$this->assertEquals(array( $this->assertEquals(array(
'notice' => 'Foo', 'notice' => array('Foo'),
'error' => 'Bar'), $this->bag->all() 'error' => array('Bar')), $this->bag->all()
); );


$this->assertEquals(array(), $this->bag->all()); $this->assertEquals(array(), $this->bag->all());
Expand All @@ -101,7 +101,7 @@ public function testSet()
{ {
$this->bag->set('notice', 'Foo'); $this->bag->set('notice', 'Foo');
$this->bag->set('notice', 'Bar'); $this->bag->set('notice', 'Bar');
$this->assertEquals('Bar', $this->bag->peek('notice')); $this->assertEquals(array('Bar'), $this->bag->peek('notice'));
} }


public function testHas() public function testHas()
Expand All @@ -120,15 +120,15 @@ public function testPeekAll()
$this->bag->set('notice', 'Foo'); $this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar'); $this->bag->set('error', 'Bar');
$this->assertEquals(array( $this->assertEquals(array(
'notice' => 'Foo', 'notice' => array('Foo'),
'error' => 'Bar', 'error' => array('Bar'),
), $this->bag->peekAll() ), $this->bag->peekAll()
); );
$this->assertTrue($this->bag->has('notice')); $this->assertTrue($this->bag->has('notice'));
$this->assertTrue($this->bag->has('error')); $this->assertTrue($this->bag->has('error'));
$this->assertEquals(array( $this->assertEquals(array(
'notice' => 'Foo', 'notice' => array('Foo'),
'error' => 'Bar', 'error' => array('Bar'),
), $this->bag->peekAll() ), $this->bag->peekAll()
); );
} }
Expand Down
Expand Up @@ -81,7 +81,7 @@ public function testSave()
$storage->start(); $storage->start();
$this->assertEquals('108', $storage->getBag('attributes')->get('new')); $this->assertEquals('108', $storage->getBag('attributes')->get('new'));
$this->assertTrue($storage->getBag('flashes')->has('newkey')); $this->assertTrue($storage->getBag('flashes')->has('newkey'));
$this->assertEquals('test', $storage->getBag('flashes')->peek('newkey')); $this->assertEquals(array('test'), $storage->getBag('flashes')->peek('newkey'));
} }


public function testMultipleInstances() public function testMultipleInstances()
Expand Down

0 comments on commit 84c2e3c

Please sign in to comment.