[HttpFoundation] Remove constants from FlashBagInterface

As requested by fabpot.
Corrected a few mistakes in the documentation.
This commit is contained in:
Drak 2012-02-10 15:38:21 +05:45
parent dad60efccc
commit 0d2745f750
6 changed files with 56 additions and 76 deletions

View File

@ -231,9 +231,6 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* made mimetype to extension conversion configurable * made mimetype to extension conversion configurable
* [BC BREAK] Moved all session related classes and interfaces into own namespace, as * [BC BREAK] Moved all session related classes and interfaces into own namespace, as
`Symfony\Component\HttpFoudation\Session` and renamed classes accordingly. `Symfony\Component\HttpFoudation\Session` and renamed classes accordingly.
* Flashes are now stored as a bucket of messages per `$type` so there can be multiple messages per type.
There are four interface constants for type, `FlashBagInterface::INFO`, `FlashBagInterface::NOTICE`,
`FlashBagInterface::WARNING` and `FlashBagInterface::ERROR`.
* Added `FlashBag` (default). Flashes expire when retrieved by `pop()` or `popAll()`. * Added `FlashBag` (default). Flashes expire when retrieved by `pop()` or `popAll()`.
This makes the implementation ESI compatible. This makes the implementation ESI compatible.
* Added `AutoExpireFlashBag` to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring * Added `AutoExpireFlashBag` to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring

View File

@ -259,21 +259,14 @@ UPGRADE FROM 2.0 to 2.1
</div> </div>
<?php endif; ?> <?php endif; ?>
If you wanted to process all flash types you could also make use of the `getFlashes()->all()` API: If you wanted to process all flash types you could also make use of the `getFlashes()->popAll()` API:
<?php foreach ($view['session']->getFlashes()->all() as $type => $flash): ?> <?php foreach ($view['session']->getFlashes()->popAll() as $type => $flash): ?>
<div class="flash-$type"> <div class="flash-$type">
<?php echo $flash; ?> <?php echo $flash; ?>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
.. note::
The Flash Message API provides constants which you can optionally use. For example
`Symfony\Component\HttpFoundation\Session\Flash\FlashBag::NOTICE`, which can also be abbreviated to
`FlashBag::NOTICE` providing you declare `<?php use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; ?>`
at the beginning of the PHP template.
Before (Twig): Before (Twig):
{% if app.session.hasFlash('notice') %} {% if app.session.hasFlash('notice') %}
@ -284,25 +277,20 @@ UPGRADE FROM 2.0 to 2.1
After (Twig): After (Twig):
{% if app.session.getFlashes.has('notice') %} {% if app.session.getFlashes().has('notice') %}
<div class="flash-notice"> <div class="flash-notice">
{{ app.session.getFlashes.pop('notice') }} {{ app.session.getFlashes().pop('notice') }}
</div> </div>
{% endif %} {% endif %}
Again you can process all flash messages in one go with Again you can process all flash messages in one go with
{% for type, flashMessage in app.session.getFlashes.popAll() %} {% for type, flashMessage in app.session.getFlashes().popAll() %}
<div class="flash-{{ type }}"> <div class="flash-{{ type }}">
{{ flashMessage }} {{ flashMessage }}
</div> </div>
{% endforeach %} {% endforeach %}
.. note::
You can optionally use constants in Twig templates using `constant()` e.g.
`constant('Symfony\Component\HttpFoundation\Session\Flash\FlashBag::NOTICE')`.
* Session object * Session object
The methods, `setFlash()`, `setFlashes()`, `getFlash()`, `hasFlash()`, and `removeFlash()` The methods, `setFlash()`, `setFlashes()`, `getFlash()`, `hasFlash()`, and `removeFlash()`
@ -318,7 +306,7 @@ UPGRADE FROM 2.0 to 2.1
`StorageInterface`. `StorageInterface`.
Any session storage driver that wants to use custom save handlers should Any session storage driver that wants to use custom save handlers should
implement `Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface` implement `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface`
### [FrameworkBundle] ### [FrameworkBundle]

View File

@ -28,7 +28,7 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase
$session = new Session(new MockArrayStorage()); $session = new Session(new MockArrayStorage());
$session->set('foobar', 'bar'); $session->set('foobar', 'bar');
$session->getFlashes()->set(FlashBag::NOTICE, 'bar'); $session->getFlashes()->set('notice', 'bar');
$this->request->setSession($session); $this->request->setSession($session);
} }
@ -42,15 +42,15 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase
{ {
$helper = new SessionHelper($this->request); $helper = new SessionHelper($this->request);
$this->assertTrue($helper->hasFlash(FlashBag::NOTICE)); $this->assertTrue($helper->hasFlash('notice'));
$this->assertEquals('bar', $helper->getFlash(FlashBag::NOTICE)); $this->assertEquals('bar', $helper->getFlash('notice'));
} }
public function testGetFlashes() public function testGetFlashes()
{ {
$helper = new SessionHelper($this->request); $helper = new SessionHelper($this->request);
$this->assertEquals(array(FlashBag::NOTICE => 'bar'), $helper->getFlashes()); $this->assertEquals(array('notice' => 'bar'), $helper->getFlashes());
} }
public function testGet() public function testGet()

View File

@ -20,11 +20,6 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
*/ */
interface FlashBagInterface extends SessionBagInterface interface FlashBagInterface extends SessionBagInterface
{ {
const INFO = 'info';
const NOTICE = 'notice';
const WARNING = 'warning';
const ERROR = 'error';
/** /**
* Registers a message for a given type. * Registers a message for a given type.
* *

View File

@ -35,7 +35,7 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase
{ {
parent::setUp(); parent::setUp();
$this->bag = new FlashBag(); $this->bag = new FlashBag();
$this->array = array('new' => array(FlashBag::NOTICE => 'A previous flash message')); $this->array = array('new' => array('notice' => 'A previous flash message'));
$this->bag->initialize($this->array); $this->bag->initialize($this->array);
} }
@ -48,62 +48,62 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase
public function testInitialize() public function testInitialize()
{ {
$bag = new FlashBag(); $bag = new FlashBag();
$array = array('new' => array(FlashBag::NOTICE => 'A previous flash message')); $array = array('new' => array('notice' => 'A previous flash message'));
$bag->initialize($array); $bag->initialize($array);
$this->assertEquals('A previous flash message', $bag->peek(FlashBag::NOTICE)); $this->assertEquals('A previous flash message', $bag->peek('notice'));
$array = array('new' => array( $array = array('new' => array(
FlashBag::NOTICE => 'Something else', 'notice' => 'Something else',
FlashBag::ERROR => 'a', 'error' => 'a',
)); ));
$bag->initialize($array); $bag->initialize($array);
$this->assertEquals('Something else', $bag->peek(FlashBag::NOTICE)); $this->assertEquals('Something else', $bag->peek('notice'));
$this->assertEquals('a', $bag->peek(FlashBag::ERROR)); $this->assertEquals('a', $bag->peek('error'));
} }
public function testPeek() public function testPeek()
{ {
$this->assertNull($this->bag->peek('non_existing')); $this->assertNull($this->bag->peek('non_existing'));
$this->assertEquals('default', $this->bag->peek('non_existing', 'default')); $this->assertEquals('default', $this->bag->peek('non_existing', 'default'));
$this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); $this->assertEquals('A previous flash message', $this->bag->peek('notice'));
$this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); $this->assertEquals('A previous flash message', $this->bag->peek('notice'));
} }
public function testSet() public function testSet()
{ {
$this->bag->set(FlashBag::NOTICE, 'Foo'); $this->bag->set('notice', 'Foo');
$this->assertNotEquals('Foo', $this->bag->peek(FlashBag::NOTICE)); $this->assertNotEquals('Foo', $this->bag->peek('notice'));
} }
public function testHas() public function testHas()
{ {
$this->assertFalse($this->bag->has('nothing')); $this->assertFalse($this->bag->has('nothing'));
$this->assertTrue($this->bag->has(FlashBag::NOTICE)); $this->assertTrue($this->bag->has('notice'));
} }
public function testKeys() public function testKeys()
{ {
$this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); $this->assertEquals(array('notice'), $this->bag->keys());
} }
public function testPeekAll() public function testPeekAll()
{ {
$array = array( $array = array(
'new' => array( 'new' => array(
FlashBag::NOTICE => 'Foo', 'notice' => 'Foo',
FlashBag::ERROR => 'Bar', 'error' => 'Bar',
), ),
); );
$this->bag->initialize($array); $this->bag->initialize($array);
$this->assertEquals(array( $this->assertEquals(array(
FlashBag::NOTICE => 'Foo', 'notice' => 'Foo',
FlashBag::ERROR => 'Bar', 'error' => 'Bar',
), $this->bag->peekAll() ), $this->bag->peekAll()
); );
$this->assertEquals(array( $this->assertEquals(array(
FlashBag::NOTICE => 'Foo', 'notice' => 'Foo',
FlashBag::ERROR => 'Bar', 'error' => 'Bar',
), $this->bag->peekAll() ), $this->bag->peekAll()
); );
} }
@ -112,16 +112,16 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase
{ {
$this->assertNull($this->bag->pop('non_existing')); $this->assertNull($this->bag->pop('non_existing'));
$this->assertEquals('default', $this->bag->pop('non_existing', 'default')); $this->assertEquals('default', $this->bag->pop('non_existing', 'default'));
$this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); $this->assertEquals('A previous flash message', $this->bag->pop('notice'));
$this->assertNull($this->bag->pop(FlashBag::NOTICE)); $this->assertNull($this->bag->pop('notice'));
} }
public function testPopAll() public function testPopAll()
{ {
$this->bag->set(FlashBag::NOTICE, 'Foo'); $this->bag->set('notice', 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar'); $this->bag->set('error', 'Bar');
$this->assertEquals(array( $this->assertEquals(array(
FlashBag::NOTICE => 'A previous flash message', 'notice' => 'A previous flash message',
), $this->bag->popAll() ), $this->bag->popAll()
); );

View File

@ -35,7 +35,7 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase
{ {
parent::setUp(); parent::setUp();
$this->bag = new FlashBag(); $this->bag = new FlashBag();
$this->array = array(FlashBag::NOTICE => 'A previous flash message'); $this->array = array('notice' => 'A previous flash message');
$this->bag->initialize($this->array); $this->bag->initialize($this->array);
} }
@ -59,25 +59,25 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase
{ {
$this->assertNull($this->bag->peek('non_existing')); $this->assertNull($this->bag->peek('non_existing'));
$this->assertEquals('default', $this->bag->peek('not_existing', 'default')); $this->assertEquals('default', $this->bag->peek('not_existing', 'default'));
$this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); $this->assertEquals('A previous flash message', $this->bag->peek('notice'));
$this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); $this->assertEquals('A previous flash message', $this->bag->peek('notice'));
} }
public function testPop() public function testPop()
{ {
$this->assertNull($this->bag->pop('non_existing')); $this->assertNull($this->bag->pop('non_existing'));
$this->assertEquals('default', $this->bag->pop('not_existing', 'default')); $this->assertEquals('default', $this->bag->pop('not_existing', 'default'));
$this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); $this->assertEquals('A previous flash message', $this->bag->pop('notice'));
$this->assertNull($this->bag->pop(FlashBag::NOTICE)); $this->assertNull($this->bag->pop('notice'));
} }
public function testPopAll() public function testPopAll()
{ {
$this->bag->set(FlashBag::NOTICE, 'Foo'); $this->bag->set('notice', 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar'); $this->bag->set('error', 'Bar');
$this->assertEquals(array( $this->assertEquals(array(
FlashBag::NOTICE => 'Foo', 'notice' => 'Foo',
FlashBag::ERROR => 'Bar'), $this->bag->popAll() 'error' => 'Bar'), $this->bag->popAll()
); );
$this->assertEquals(array(), $this->bag->popAll()); $this->assertEquals(array(), $this->bag->popAll());
@ -85,36 +85,36 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase
public function testSet() public function testSet()
{ {
$this->bag->set(FlashBag::NOTICE, 'Foo'); $this->bag->set('notice', 'Foo');
$this->bag->set(FlashBag::NOTICE, 'Bar'); $this->bag->set('notice', 'Bar');
$this->assertEquals('Bar', $this->bag->peek(FlashBag::NOTICE)); $this->assertEquals('Bar', $this->bag->peek('notice'));
} }
public function testHas() public function testHas()
{ {
$this->assertFalse($this->bag->has('nothing')); $this->assertFalse($this->bag->has('nothing'));
$this->assertTrue($this->bag->has(FlashBag::NOTICE)); $this->assertTrue($this->bag->has('notice'));
} }
public function testKeys() public function testKeys()
{ {
$this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); $this->assertEquals(array('notice'), $this->bag->keys());
} }
public function testPeekAll() public function testPeekAll()
{ {
$this->bag->set(FlashBag::NOTICE, 'Foo'); $this->bag->set('notice', 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar'); $this->bag->set('error', 'Bar');
$this->assertEquals(array( $this->assertEquals(array(
FlashBag::NOTICE => 'Foo', 'notice' => 'Foo',
FlashBag::ERROR => 'Bar', 'error' => 'Bar',
), $this->bag->peekAll() ), $this->bag->peekAll()
); );
$this->assertTrue($this->bag->has(FlashBag::NOTICE)); $this->assertTrue($this->bag->has('notice'));
$this->assertTrue($this->bag->has(FlashBag::ERROR)); $this->assertTrue($this->bag->has('error'));
$this->assertEquals(array( $this->assertEquals(array(
FlashBag::NOTICE => 'Foo', 'notice' => 'Foo',
FlashBag::ERROR => 'Bar', 'error' => 'Bar',
), $this->bag->peekAll() ), $this->bag->peekAll()
); );
} }