3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Controller\Plugin
;
14 use IteratorAggregate
;
15 use Zend\Session\Container
;
16 use Zend\Session\ManagerInterface
as Manager
;
17 use Zend\Stdlib\SplQueue
;
20 * Flash Messenger - implement session-based messages
22 class FlashMessenger
extends AbstractPlugin
implements IteratorAggregate
, Countable
25 * Default messages namespace
27 const NAMESPACE_DEFAULT
= 'default';
30 * Success messages namespace
32 const NAMESPACE_SUCCESS
= 'success';
35 * Warning messages namespace
37 const NAMESPACE_WARNING
= 'warning';
40 * Error messages namespace
42 const NAMESPACE_ERROR
= 'error';
45 * Info messages namespace
47 const NAMESPACE_INFO
= 'info';
55 * Messages from previous request
58 protected $messages = [];
66 * Whether a message has been added during this request
70 protected $messageAdded = false;
73 * Instance namespace, default is 'default'
77 protected $namespace = self
::NAMESPACE_DEFAULT
;
80 * Set the session manager
82 * @param Manager $manager
83 * @return FlashMessenger
85 public function setSessionManager(Manager
$manager)
87 $this->session
= $manager;
93 * Retrieve the session manager
95 * If none composed, lazy-loads a SessionManager instance
99 public function getSessionManager()
101 if (!$this->session
instanceof Manager
) {
102 $this->setSessionManager(Container
::getDefaultManager());
105 return $this->session
;
109 * Get session container for flash messages
113 public function getContainer()
115 if ($this->container
instanceof Container
) {
116 return $this->container
;
119 $manager = $this->getSessionManager();
120 $this->container
= new Container('FlashMessenger', $manager);
122 return $this->container
;
126 * Change the namespace messages are added to
128 * Useful for per action controller messaging between requests
130 * @param string $namespace
131 * @return FlashMessenger Provides a fluent interface
133 public function setNamespace($namespace = 'default')
135 $this->namespace = $namespace;
141 * Get the message namespace
145 public function getNamespace()
147 return $this->namespace;
153 * @param string $message
154 * @param null|string $namespace
155 * @param null|int $hops
156 * @return FlashMessenger Provides a fluent interface
158 public function addMessage($message, $namespace = null, $hops = 1)
160 $container = $this->getContainer();
162 if (null === $namespace) {
163 $namespace = $this->getNamespace();
166 if (! $this->messageAdded
) {
167 $this->getMessagesFromContainer();
168 $container->setExpirationHops($hops, null);
171 if (! isset($container->{$namespace})
172 ||
! $container->{$namespace} instanceof SplQueue
174 $container->{$namespace} = new SplQueue();
177 $container->{$namespace}->push($message);
179 $this->messageAdded
= true;
185 * Add a message with "info" type
187 * @param string $message
188 * @return FlashMessenger
190 public function addInfoMessage($message)
192 $this->addMessage($message, self
::NAMESPACE_INFO
);
198 * Add a message with "success" type
200 * @param string $message
201 * @return FlashMessenger
203 public function addSuccessMessage($message)
205 $this->addMessage($message, self
::NAMESPACE_SUCCESS
);
211 * Add a message with "warning" type
213 * @param string $message
214 * @return FlashMessenger
216 public function addWarningMessage($message)
218 $this->addMessage($message, self
::NAMESPACE_WARNING
);
224 * Add a message with "error" type
226 * @param string $message
227 * @return FlashMessenger
229 public function addErrorMessage($message)
231 $this->addMessage($message, self
::NAMESPACE_ERROR
);
237 * Whether a specific namespace has messages
239 * @param string $namespace
242 public function hasMessages($namespace = null)
244 if (null === $namespace) {
245 $namespace = $this->getNamespace();
248 $this->getMessagesFromContainer();
250 return isset($this->messages
[$namespace]);
254 * Whether "info" namespace has messages
258 public function hasInfoMessages()
260 return $this->hasMessages(self
::NAMESPACE_INFO
);
264 * Whether "success" namespace has messages
268 public function hasSuccessMessages()
270 return $this->hasMessages(self
::NAMESPACE_SUCCESS
);
274 * Whether "warning" namespace has messages
278 public function hasWarningMessages()
280 return $this->hasMessages(self
::NAMESPACE_WARNING
);
284 * Whether "error" namespace has messages
288 public function hasErrorMessages()
290 return $this->hasMessages(self
::NAMESPACE_ERROR
);
294 * Get messages from a specific namespace
296 * @param string $namespace
299 public function getMessages($namespace = null)
301 if (null === $namespace) {
302 $namespace = $this->getNamespace();
305 if ($this->hasMessages($namespace)) {
306 return $this->messages
[$namespace]->toArray();
313 * Get messages from "info" namespace
317 public function getInfoMessages()
319 return $this->getMessages(self
::NAMESPACE_INFO
);
323 * Get messages from "success" namespace
327 public function getSuccessMessages()
329 return $this->getMessages(self
::NAMESPACE_SUCCESS
);
333 * Get messages from "warning" namespace
337 public function getWarningMessages()
339 return $this->getMessages(self
::NAMESPACE_WARNING
);
343 * Get messages from "error" namespace
347 public function getErrorMessages()
349 return $this->getMessages(self
::NAMESPACE_ERROR
);
353 * Clear all messages from the previous request & current namespace
355 * @param string $namespace
356 * @return bool True if messages were cleared, false if none existed
358 public function clearMessages($namespace = null)
360 if (null === $namespace) {
361 $namespace = $this->getNamespace();
364 if ($this->hasMessages($namespace)) {
365 unset($this->messages
[$namespace]);
374 * Clear all messages from specific namespace
376 * @param string $namespaceToClear
377 * @return bool True if messages were cleared, false if none existed
379 public function clearMessagesFromNamespace($namespaceToClear)
381 return $this->clearMessages($namespaceToClear);
385 * Clear all messages from the container
387 * @return bool True if messages were cleared, false if none existed
389 public function clearMessagesFromContainer()
391 $this->getMessagesFromContainer();
392 if (empty($this->messages
)) {
395 unset($this->messages
);
396 $this->messages
= [];
402 * Check to see if messages have been added to the current
403 * namespace within this request
405 * @param string $namespace
408 public function hasCurrentMessages($namespace = null)
410 $container = $this->getContainer();
411 if (null === $namespace) {
412 $namespace = $this->getNamespace();
415 return isset($container->{$namespace});
419 * Check to see if messages have been added to "info"
420 * namespace within this request
424 public function hasCurrentInfoMessages()
426 return $this->hasCurrentMessages(self
::NAMESPACE_INFO
);
430 * Check to see if messages have been added to "success"
431 * namespace within this request
435 public function hasCurrentSuccessMessages()
437 return $this->hasCurrentMessages(self
::NAMESPACE_SUCCESS
);
441 * Check to see if messages have been added to "warning"
442 * namespace within this request
446 public function hasCurrentWarningMessages()
448 return $this->hasCurrentMessages(self
::NAMESPACE_WARNING
);
452 * Check to see if messages have been added to "error"
453 * namespace within this request
457 public function hasCurrentErrorMessages()
459 return $this->hasCurrentMessages(self
::NAMESPACE_ERROR
);
463 * Get messages that have been added to the current
464 * namespace within this request
466 * @param string $namespace
469 public function getCurrentMessages($namespace = null)
471 if (null === $namespace) {
472 $namespace = $this->getNamespace();
475 if ($this->hasCurrentMessages($namespace)) {
476 $container = $this->getContainer();
478 return $container->{$namespace}->toArray();
485 * Get messages that have been added to the "info"
486 * namespace within this request
490 public function getCurrentInfoMessages()
492 return $this->getCurrentMessages(self
::NAMESPACE_INFO
);
496 * Get messages that have been added to the "success"
497 * namespace within this request
501 public function getCurrentSuccessMessages()
503 return $this->getCurrentMessages(self
::NAMESPACE_SUCCESS
);
507 * Get messages that have been added to the "warning"
508 * namespace within this request
512 public function getCurrentWarningMessages()
514 return $this->getCurrentMessages(self
::NAMESPACE_WARNING
);
518 * Get messages that have been added to the "error"
519 * namespace within this request
523 public function getCurrentErrorMessages()
525 return $this->getCurrentMessages(self
::NAMESPACE_ERROR
);
529 * Get messages that have been added to the current
530 * namespace in specific namespace
532 * @param string $namespaceToGet
535 public function getCurrentMessagesFromNamespace($namespaceToGet)
537 return $this->getCurrentMessages($namespaceToGet);
541 * Clear messages from the current request and current namespace
543 * @param string $namespace
544 * @return bool True if current messages were cleared, false if none existed.
546 public function clearCurrentMessages($namespace = null)
548 if (null === $namespace) {
549 $namespace = $this->getNamespace();
552 if ($this->hasCurrentMessages($namespace)) {
553 $container = $this->getContainer();
554 unset($container->{$namespace});
563 * Clear messages from the current namespace
565 * @param string $namespaceToClear
566 * @return bool True if current messages were cleared from the given namespace, false if none existed.
568 public function clearCurrentMessagesFromNamespace($namespaceToClear)
570 return $this->clearCurrentMessages($namespaceToClear);
574 * Clear messages from the container
576 * @return bool True if current messages were cleared from the container, false if none existed.
578 public function clearCurrentMessagesFromContainer()
580 $container = $this->getContainer();
583 foreach ($container as $namespace => $messages) {
584 $namespaces[] = $namespace;
587 if (empty($namespaces)) {
591 foreach ($namespaces as $namespace) {
592 unset($container->{$namespace});
599 * Complete the IteratorAggregate interface, for iterating
601 * @return ArrayIterator
603 public function getIterator()
605 if ($this->hasMessages()) {
606 return new ArrayIterator($this->getMessages());
609 return new ArrayIterator();
613 * Complete the countable interface
617 public function count()
619 if ($this->hasMessages()) {
620 return count($this->getMessages());
627 * Get messages from a specific namespace
629 * @param string $namespaceToGet
632 public function getMessagesFromNamespace($namespaceToGet)
634 return $this->getMessages($namespaceToGet);
638 * Pull messages from the session container
640 * Iterates through the session container, removing messages into the local
645 protected function getMessagesFromContainer()
647 if (!empty($this->messages
) ||
$this->messageAdded
) {
651 $container = $this->getContainer();
654 foreach ($container as $namespace => $messages) {
655 $this->messages
[$namespace] = $messages;
656 $namespaces[] = $namespace;
659 foreach ($namespaces as $namespace) {
660 unset($container->{$namespace});