Skip to content

Instantly share code, notes, and snippets.

@robertlemke
Created October 11, 2011 13:31
Show Gist options
  • Save robertlemke/1278081 to your computer and use it in GitHub Desktop.
Save robertlemke/1278081 to your computer and use it in GitHub Desktop.
New Flash Message Container
<?php
namespace TYPO3\FLOW3\MVC\Controller;
/* *
* This script belongs to the FLOW3 framework. *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* This is a container for all Flash Messages.
*
* @scope session
* @api
*/
class FlashMessageContainer {
/**
* @var array
*/
protected $messages = array();
/**
* Add a flash message object.
*
* @param \TYPO3\FLOW3\MVC\Controller\FlashMessage $message
* @param string $messageTitle optional message title
* @return void
* @api
*/
public function addMessage(FlashMessage $message) {
$this->messages[] = $message;
}
/**
* Returns all currently stored flash messages.
*
* @param string $severity severity of the message (One of the FlashMessage::SEVERITY_* constants) to return.
* @return array<FlashMessage>
* @session autoStart=false
* @api
*/
public function getMessages($severity = NULL) {
if ($severity === NULL) {
return $this->messages;
}
$messages = array();
foreach($this->messages as $message) {
if ($message->getSeverity() === $severity) {
$messages[] = $message;
}
}
return $messages;
}
/**
* Remove messages from this container.
*
* @param string $severity severity of the message (One of the FlashMessage::SEVERITY_* constants) to remove
* @return void
* @api
*/
public function flush($severity = NULL) {
if ($severity === NULL) {
$this->messages = array();
} else {
foreach($this->messages as $index => $message) {
if ($message->getSeverity() === $severity) {
unset($this->messages[$index]);
}
}
}
}
/**
* Get all flash messages currently available and remove them from the container.
*
* @param string $severity severity of of the message (One of the FlashMessage::SEVERITY_* constants)
* @return array<FlashMessage>
* @session autoStart=false
* @api
*/
public function getMessagesAndFlush($severity = NULL) {
$messages = $this->getMessages($severity);
if (count($messages) > 0) {
$this->flush($severity);
}
return $messages;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment