-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WIP][HttpFoundation] Move flash messages out of Session class and change to bucket system. #2592
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7390a1e
[HttpFoundation] Move flash messages out of Session class and change …
b60e84e
[HttpFoundation] Added some tests and fixed logic and CS accordingly.
1b3d8d4
[HttpFoundation] Added tests.
cedc1f0
[HttpFoundation] remove optional driver for now.
91342f7
[HttpFoundation] Added some sensible defaults for flash types.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
phpunit.xml | ||
autoload.php | ||
/vendor/ | ||
/nbproject/private/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
* @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
83
src/Symfony/Component/HttpFoundation/FlashBagInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.