Source of file FlashMessenger.php
Size: 17,802 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Mvc/Controller/Plugin/FlashMessenger.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Mvc\Controller\Plugin; use ArrayIterator; use Countable; use IteratorAggregate; use Zend\Session\Container; use Zend\Session\ManagerInterface as Manager; use Zend\Stdlib\SplQueue; /** * Flash Messenger - implement session-based messages */ class FlashMessenger extends AbstractPlugin implements IteratorAggregate, Countable { /** * Default messages namespace */ const NAMESPACE_DEFAULT = 'default'; /** * Success messages namespace */ const NAMESPACE_SUCCESS = 'success'; /** * Warning messages namespace */ const NAMESPACE_WARNING = 'warning'; /** * Error messages namespace */ const NAMESPACE_ERROR = 'error'; /** * Info messages namespace */ const NAMESPACE_INFO = 'info'; /** * @var Container */ protected $container; /** * Messages from previous request * @var array */ protected $messages = array(); /** * @var Manager */ protected $session; /** * Whether a message has been added during this request * * @var bool */ protected $messageAdded = false; /** * Instance namespace, default is 'default' * * @var string */ protected $namespace = self::NAMESPACE_DEFAULT; /** * Set the session manager * * @param Manager $manager * @return FlashMessenger */ public function setSessionManager(Manager $manager) { $this->session = $manager; return $this; } /** * Retrieve the session manager * * If none composed, lazy-loads a SessionManager instance * * @return Manager */ public function getSessionManager() { if (!$this->session instanceof Manager) { $this->setSessionManager(Container::getDefaultManager()); } return $this->session; } /** * Get session container for flash messages * * @return Container */ public function getContainer() { if ($this->container instanceof Container) { return $this->container; } $manager = $this->getSessionManager(); $this->container = new Container('FlashMessenger', $manager); return $this->container; } /** * Change the namespace messages are added to * * Useful for per action controller messaging between requests * * @param string $namespace * @return FlashMessenger Provides a fluent interface */ public function setNamespace($namespace = 'default') { $this->namespace = $namespace; return $this; } /** * Get the message namespace * * @return string */ public function getNamespace() { return $this->namespace; } /** * Add a message * * @param string $message * @return FlashMessenger Provides a fluent interface */ public function addMessage($message) { $container = $this->getContainer(); $namespace = $this->getNamespace(); if (!$this->messageAdded) { $this->getMessagesFromContainer(); $container->setExpirationHops(1, null); } if (!isset($container->{$namespace}) || !($container->{$namespace} instanceof SplQueue) ) { $container->{$namespace} = new SplQueue(); } $container->{$namespace}->push($message); $this->messageAdded = true; return $this; } /** * Add a message with "info" type * * @param string $message * @return FlashMessenger */ public function addInfoMessage($message) { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_INFO); $this->addMessage($message); $this->setNamespace($namespace); return $this; } /** * Add a message with "success" type * * @param string $message * @return FlashMessenger */ public function addSuccessMessage($message) { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_SUCCESS); $this->addMessage($message); $this->setNamespace($namespace); return $this; } /** * Add a message with "warning" type * * @param string $message * @return FlashMessenger */ public function addWarningMessage($message) { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_WARNING); $this->addMessage($message); $this->setNamespace($namespace); return $this; } /** * Add a message with "error" type * * @param string $message * @return FlashMessenger */ public function addErrorMessage($message) { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_ERROR); $this->addMessage($message); $this->setNamespace($namespace); return $this; } /** * Whether a specific namespace has messages * * @return bool */ public function hasMessages() { $this->getMessagesFromContainer(); return isset($this->messages[$this->getNamespace()]); } /** * Whether "info" namespace has messages * * @return bool */ public function hasInfoMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_INFO); $hasMessages = $this->hasMessages(); $this->setNamespace($namespace); return $hasMessages; } /** * Whether "success" namespace has messages * * @return bool */ public function hasSuccessMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_SUCCESS); $hasMessages = $this->hasMessages(); $this->setNamespace($namespace); return $hasMessages; } /** * Whether "warning" namespace has messages * * @return bool */ public function hasWarningMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_WARNING); $hasMessages = $this->hasMessages(); $this->setNamespace($namespace); return $hasMessages; } /** * Whether "error" namespace has messages * * @return bool */ public function hasErrorMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_ERROR); $hasMessages = $this->hasMessages(); $this->setNamespace($namespace); return $hasMessages; } /** * Get messages from a specific namespace * * @return array */ public function getMessages() { if ($this->hasMessages()) { return $this->messages[$this->getNamespace()]->toArray(); } return array(); } /** * Get messages from "info" namespace * * @return array */ public function getInfoMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_INFO); $messages = $this->getMessages(); $this->setNamespace($namespace); return $messages; } /** * Get messages from "success" namespace * * @return array */ public function getSuccessMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_SUCCESS); $messages = $this->getMessages(); $this->setNamespace($namespace); return $messages; } /** * Get messages from "warning" namespace * * @return array */ public function getWarningMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_WARNING); $messages = $this->getMessages(); $this->setNamespace($namespace); return $messages; } /** * Get messages from "error" namespace * * @return array */ public function getErrorMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_ERROR); $messages = $this->getMessages(); $this->setNamespace($namespace); return $messages; } /** * Clear all messages from the previous request & current namespace * * @return bool True if messages were cleared, false if none existed */ public function clearMessages() { if ($this->hasMessages()) { unset($this->messages[$this->getNamespace()]); return true; } return false; } /** * Clear all messages from specific namespace * * @param string $namespaceToClear * @return bool True if messages were cleared, false if none existed */ public function clearMessagesFromNamespace($namespaceToClear) { $namespace = $this->getNamespace(); $this->setNamespace($namespaceToClear); $cleared = $this->clearMessages(); $this->setNamespace($namespace); return $cleared; } /** * Clear all messages from the container * * @return bool True if messages were cleared, false if none existed */ public function clearMessagesFromContainer() { $this->getMessagesFromContainer(); if (empty($this->messages)) { return false; } unset($this->messages); $this->messages = array(); return true; } /** * Check to see if messages have been added to the current * namespace within this request * * @return bool */ public function hasCurrentMessages() { $container = $this->getContainer(); $namespace = $this->getNamespace(); return isset($container->{$namespace}); } /** * Check to see if messages have been added to "info" * namespace within this request * * @return bool */ public function hasCurrentInfoMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_INFO); $hasMessages = $this->hasCurrentMessages(); $this->setNamespace($namespace); return $hasMessages; } /** * Check to see if messages have been added to "success" * namespace within this request * * @return bool */ public function hasCurrentSuccessMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_SUCCESS); $hasMessages = $this->hasCurrentMessages(); $this->setNamespace($namespace); return $hasMessages; } /** * Check to see if messages have been added to "warning" * namespace within this request * * @return bool */ public function hasCurrentWarningMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_WARNING); $hasMessages = $this->hasCurrentMessages(); $this->setNamespace($namespace); return $hasMessages; } /** * Check to see if messages have been added to "error" * namespace within this request * * @return bool */ public function hasCurrentErrorMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_ERROR); $hasMessages = $this->hasCurrentMessages(); $this->setNamespace($namespace); return $hasMessages; } /** * Get messages that have been added to the current * namespace within this request * * @return array */ public function getCurrentMessages() { if ($this->hasCurrentMessages()) { $container = $this->getContainer(); $namespace = $this->getNamespace(); return $container->{$namespace}->toArray(); } return array(); } /** * Get messages that have been added to the "info" * namespace within this request * * @return array */ public function getCurrentInfoMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_INFO); $messages = $this->getCurrentMessages(); $this->setNamespace($namespace); return $messages; } /** * Get messages that have been added to the "success" * namespace within this request * * @return array */ public function getCurrentSuccessMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_SUCCESS); $messages = $this->getCurrentMessages(); $this->setNamespace($namespace); return $messages; } /** * Get messages that have been added to the "warning" * namespace within this request * * @return array */ public function getCurrentWarningMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_WARNING); $messages = $this->getCurrentMessages(); $this->setNamespace($namespace); return $messages; } /** * Get messages that have been added to the "error" * namespace within this request * * @return array */ public function getCurrentErrorMessages() { $namespace = $this->getNamespace(); $this->setNamespace(self::NAMESPACE_ERROR); $messages = $this->getCurrentMessages(); $this->setNamespace($namespace); return $messages; } /** * Get messages that have been added to the current * namespace in specific namespace * * @param string $namespaceToGet * @return array */ public function getCurrentMessagesFromNamespace($namespaceToGet) { $namespace = $this->getNamespace(); $this->setNamespace($namespaceToGet); $messages = $this->getCurrentMessages(); $this->setNamespace($namespace); return $messages; } /** * Clear messages from the current request and current namespace * * @return bool True if current messages were cleared, false if none existed. */ public function clearCurrentMessages() { if ($this->hasCurrentMessages()) { $container = $this->getContainer(); $namespace = $this->getNamespace(); unset($container->{$namespace}); return true; } return false; } /** * Clear messages from the current namespace * * @param string $namespaceToClear * @return bool True if current messages were cleared from the given namespace, false if none existed. */ public function clearCurrentMessagesFromNamespace($namespaceToClear) { $namespace = $this->getNamespace(); $this->setNamespace($namespaceToClear); $cleared = $this->clearCurrentMessages(); $this->setNamespace($namespace); return $cleared; } /** * Clear messages from the container * * @return bool True if current messages were cleared from the container, false if none existed. */ public function clearCurrentMessagesFromContainer() { $container = $this->getContainer(); $namespaces = array(); foreach ($container as $namespace => $messages) { $namespaces[] = $namespace; } if (empty($namespaces)) { return false; } foreach ($namespaces as $namespace) { unset($container->{$namespace}); } return true; } /** * Complete the IteratorAggregate interface, for iterating * * @return ArrayIterator */ public function getIterator() { if ($this->hasMessages()) { return new ArrayIterator($this->getMessages()); } return new ArrayIterator(); } /** * Complete the countable interface * * @return int */ public function count() { if ($this->hasMessages()) { return count($this->getMessages()); } return 0; } /** * Get messages from a specific namespace * * @param string $namespaceToGet * @return array */ public function getMessagesFromNamespace($namespaceToGet) { $namespace = $this->getNamespace(); $this->setNamespace($namespaceToGet); $messages = $this->getMessages(); $this->setNamespace($namespace); return $messages; } /** * Pull messages from the session container * * Iterates through the session container, removing messages into the local * scope. * * @return void */ protected function getMessagesFromContainer() { if (!empty($this->messages) || $this->messageAdded) { return; } $container = $this->getContainer(); $namespaces = array(); foreach ($container as $namespace => $messages) { $this->messages[$namespace] = $messages; $namespaces[] = $namespace; } foreach ($namespaces as $namespace) { unset($container->{$namespace}); } } } |