Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][HttpFoundation] Move flash messages out of Session class and change to bucket system. #2592

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
phpunit.xml
autoload.php
/vendor/
/nbproject/private/
4 changes: 3 additions & 1 deletion CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* removed the ContentTypeMimeTypeGuesser class as it is deprecated and never used on PHP 5.3
* added ResponseHeaderBag::makeDisposition() (implements RFC 6266)
* made mimetype to extension conversion configurable
* [BC BREAK] Flashes are now stored as a bucket of messages per $type. Moved flash messages
out of the session class. Must use $session->getFlashBag() to get FlashBag instance.

### HttpKernel

Expand Down Expand Up @@ -117,4 +119,4 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* added a Size validator
* added a SizeLength validator
* improved the ImageValidator with min width, max width, min height, and max height constraints
* added support for MIME with wildcard in FileValidator
* added support for MIME with wildcard in FileValidator
156 changes: 156 additions & 0 deletions src/Symfony/Component/HttpFoundation/FlashBag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation;

/**
* FlashBag flash message container.
*/
class FlashBag implements FlashBagInterface
{
const STATUS = 'status';
const ERROR = 'error';

/**
* Flash messages.
*
* @var array
*/
private $flashes = array();

/**
* Old flash messages to be purged.
*
* @var array
*/
private $oldFlashes = array();

/**
* @var boolean
*/
private $initialized = false;

/**
* Initializes the FlashBag.
*
* @param array $flashes
*/
public function initialize(array $flashes)
{
if ($this->initialized) {
return;
}

$this->flashes = $flashes;
$this->oldFlashes = $flashes;
$this->initialized = true;
}

/**
* Adds a flash to the stack for a given type.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is confusing as we aren't actually storing messages in a stack and they can be retrieved by index.

*
* @param string $message Message.
* @param string $type Message category
*/
public function add($message, $type = self::STATUS)
{
$this->flashes[$type][] = $message;
}

/**
* Gets flashes for a given type.
*
* @return array
*/
public function get($type)
{
if (!$this->has($type)) {
throw new \InvalidArgumentException(sprintf('Specified $type %s does not exist', $type));
}

return $this->flashes[$type];
}

/**
* Sets an array of flash messages for a given type.
*
* @param string $type
* @param array $array
*/
public function set($type, array $array)
{
$this->flashes[$type] = $array;
}

/**
* Has messages for a given type?
*
* @return boolean
*/
public function has($type)
{
return array_key_exists($type, $this->flashes);
}

/**
* Returns a list of all defined types.
*
* @return array
*/
public function getTypes()
{
return array_keys($this->flashes);
}

/**
* Gets all flashes.
*
* @return array
*/
public function all()
{
return $this->flashes;
}

/**
* Clears flash messages for a given type.
*/
public function clear($type)
{
if (isset($this->flashes[$type])) {
unset($this->flashes[$type]);
}

if (isset($this->oldFlashes[$type])) {
unset($this->oldFlashes[$type]);
}
}

/**
* Clears all flash messages.
*/
public function clearAll()
{
$this->flashes = array();
$this->oldFlashes = array();
}

/**
* Removes flash messages set in a previous request.
*/
public function purgeOldFlashes()
{
foreach ($this->oldFlashes as $type => $flashes) {
$this->flashes[$type] = array_diff($this->flashes[$type], $flashes);
}
}

}
83 changes: 83 additions & 0 deletions src/Symfony/Component/HttpFoundation/FlashBagInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation;

/**
* FlashBagInterface.
*
* @author Drak <drak@zikula.org>
*/
interface FlashBagInterface
{
/**
* Initializes the FlashBag.
*
* @param array $flashes
*/
function initialize(array $flashes);

/**
* Adds a flash to the stack for a given type.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here.

*/
function add($type, $message);

/**
* Gets flash messages for a given type.
*
* @return array
*/
function get($type);

/**
* Sets an array of flash messages for a given type.
*
* @param string $type
* @param array $array
*/
function set($type, array $array);

/**
* Hass flash messages for a given type?
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo here.

*
* @return boolean
*/
function has($type);

/**
* Returns a list of all defined types.
*
* @return array
*/
function getTypes();

/**
* Gets all flash messages.
*
* @return array
*/
function all();

/**
* Clears flash messages for a given type.
*/
function clear($type);

/**
* Clears all flash messages.
*/
function clearAll();

/**
* Removes flash messages set in a previous request.
*/
function purgeOldFlashes();
}
Loading