Skip to content

Commit

Permalink
[HttpFoundation] Remove constants from FlashBagInterface
Browse files Browse the repository at this point in the history
As requested by fabpot.
Corrected a few mistakes in the documentation.
  • Loading branch information
Drak committed Feb 11, 2012
1 parent dad60ef commit 0d2745f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 76 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* [BC BREAK] Moved all session related classes and interfaces into own namespace, as
`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()`.
This makes the implementation ESI compatible.
* Added `AutoExpireFlashBag` to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring
Expand Down
24 changes: 6 additions & 18 deletions UPGRADE-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,21 +259,14 @@ UPGRADE FROM 2.0 to 2.1
</div>
<?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">
<?php echo $flash; ?>
</div>
<?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):

{% if app.session.hasFlash('notice') %}
Expand All @@ -284,25 +277,20 @@ UPGRADE FROM 2.0 to 2.1

After (Twig):

{% if app.session.getFlashes.has('notice') %}
{% if app.session.getFlashes().has('notice') %}
<div class="flash-notice">
{{ app.session.getFlashes.pop('notice') }}
{{ app.session.getFlashes().pop('notice') }}
</div>
{% endif %}

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 }}">
{{ flashMessage }}
</div>
{% endforeach %}

.. note::

You can optionally use constants in Twig templates using `constant()` e.g.
`constant('Symfony\Component\HttpFoundation\Session\Flash\FlashBag::NOTICE')`.

* Session object

The methods, `setFlash()`, `setFlashes()`, `getFlash()`, `hasFlash()`, and `removeFlash()`
Expand All @@ -318,7 +306,7 @@ UPGRADE FROM 2.0 to 2.1
`StorageInterface`.

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]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function setUp()

$session = new Session(new MockArrayStorage());
$session->set('foobar', 'bar');
$session->getFlashes()->set(FlashBag::NOTICE, 'bar');
$session->getFlashes()->set('notice', 'bar');

$this->request->setSession($session);
}
Expand All @@ -42,15 +42,15 @@ public function testFlash()
{
$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()
{
$helper = new SessionHelper($this->request);
$this->assertEquals(array(FlashBag::NOTICE => 'bar'), $helper->getFlashes());
$this->assertEquals(array('notice' => 'bar'), $helper->getFlashes());
}

public function testGet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
*/
interface FlashBagInterface extends SessionBagInterface
{
const INFO = 'info';
const NOTICE = 'notice';
const WARNING = 'warning';
const ERROR = 'error';

/**
* Registers a message for a given type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function setUp()
{
parent::setUp();
$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);
}

Expand All @@ -48,62 +48,62 @@ public function tearDown()
public function testInitialize()
{
$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);
$this->assertEquals('A previous flash message', $bag->peek(FlashBag::NOTICE));
$this->assertEquals('A previous flash message', $bag->peek('notice'));
$array = array('new' => array(
FlashBag::NOTICE => 'Something else',
FlashBag::ERROR => 'a',
'notice' => 'Something else',
'error' => 'a',
));
$bag->initialize($array);
$this->assertEquals('Something else', $bag->peek(FlashBag::NOTICE));
$this->assertEquals('a', $bag->peek(FlashBag::ERROR));
$this->assertEquals('Something else', $bag->peek('notice'));
$this->assertEquals('a', $bag->peek('error'));
}

public function testPeek()
{
$this->assertNull($this->bag->peek('non_existing'));
$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(FlashBag::NOTICE));
$this->assertEquals('A previous flash message', $this->bag->peek('notice'));
$this->assertEquals('A previous flash message', $this->bag->peek('notice'));
}

public function testSet()
{
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->assertNotEquals('Foo', $this->bag->peek(FlashBag::NOTICE));
$this->bag->set('notice', 'Foo');
$this->assertNotEquals('Foo', $this->bag->peek('notice'));
}

public function testHas()
{
$this->assertFalse($this->bag->has('nothing'));
$this->assertTrue($this->bag->has(FlashBag::NOTICE));
$this->assertTrue($this->bag->has('notice'));
}

public function testKeys()
{
$this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys());
$this->assertEquals(array('notice'), $this->bag->keys());
}

public function testPeekAll()
{
$array = array(
'new' => array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
'notice' => 'Foo',
'error' => 'Bar',
),
);

$this->bag->initialize($array);
$this->assertEquals(array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
'notice' => 'Foo',
'error' => 'Bar',
), $this->bag->peekAll()
);

$this->assertEquals(array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
'notice' => 'Foo',
'error' => 'Bar',
), $this->bag->peekAll()
);
}
Expand All @@ -112,16 +112,16 @@ public function testPop()
{
$this->assertNull($this->bag->pop('non_existing'));
$this->assertEquals('default', $this->bag->pop('non_existing', 'default'));
$this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE));
$this->assertNull($this->bag->pop(FlashBag::NOTICE));
$this->assertEquals('A previous flash message', $this->bag->pop('notice'));
$this->assertNull($this->bag->pop('notice'));
}

public function testPopAll()
{
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar');
$this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar');
$this->assertEquals(array(
FlashBag::NOTICE => 'A previous flash message',
'notice' => 'A previous flash message',
), $this->bag->popAll()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function setUp()
{
parent::setUp();
$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);
}

Expand All @@ -59,62 +59,62 @@ public function testPeek()
{
$this->assertNull($this->bag->peek('non_existing'));
$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(FlashBag::NOTICE));
$this->assertEquals('A previous flash message', $this->bag->peek('notice'));
$this->assertEquals('A previous flash message', $this->bag->peek('notice'));
}

public function testPop()
{
$this->assertNull($this->bag->pop('non_existing'));
$this->assertEquals('default', $this->bag->pop('not_existing', 'default'));
$this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE));
$this->assertNull($this->bag->pop(FlashBag::NOTICE));
$this->assertEquals('A previous flash message', $this->bag->pop('notice'));
$this->assertNull($this->bag->pop('notice'));
}

public function testPopAll()
{
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar');
$this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar');
$this->assertEquals(array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar'), $this->bag->popAll()
'notice' => 'Foo',
'error' => 'Bar'), $this->bag->popAll()
);

$this->assertEquals(array(), $this->bag->popAll());
}

public function testSet()
{
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::NOTICE, 'Bar');
$this->assertEquals('Bar', $this->bag->peek(FlashBag::NOTICE));
$this->bag->set('notice', 'Foo');
$this->bag->set('notice', 'Bar');
$this->assertEquals('Bar', $this->bag->peek('notice'));
}

public function testHas()
{
$this->assertFalse($this->bag->has('nothing'));
$this->assertTrue($this->bag->has(FlashBag::NOTICE));
$this->assertTrue($this->bag->has('notice'));
}

public function testKeys()
{
$this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys());
$this->assertEquals(array('notice'), $this->bag->keys());
}

public function testPeekAll()
{
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar');
$this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar');
$this->assertEquals(array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
'notice' => 'Foo',
'error' => 'Bar',
), $this->bag->peekAll()
);
$this->assertTrue($this->bag->has(FlashBag::NOTICE));
$this->assertTrue($this->bag->has(FlashBag::ERROR));
$this->assertTrue($this->bag->has('notice'));
$this->assertTrue($this->bag->has('error'));
$this->assertEquals(array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
'notice' => 'Foo',
'error' => 'Bar',
), $this->bag->peekAll()
);
}
Expand Down

0 comments on commit 0d2745f

Please sign in to comment.