123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace Cube\Controller\Action\Helper;
- use Cube\Controller\Front,
- Cube\Session;
- class FlashMessenger extends AbstractHelper
- {
- const SESSION_NAMESPACE = 'FlashMessenger';
- const MESSAGES_NAMESPACE = 'Messages';
-
- protected $_name = null;
-
- protected $_messages = array();
-
- protected $_session;
-
- public function __construct($name = null)
- {
- $this->setName($name);
- $this->setSession();
- }
-
- public function getName()
- {
- return $this->_name;
- }
-
- public function setName($name = null)
- {
- if ($name === null) {
- $name = self::MESSAGES_NAMESPACE;
- }
- $this->_name = $name;
- return $this;
- }
-
- public function getSession()
- {
- if (!($this->_session instanceof Session)) {
- $this->setSession();
- }
- return $this->_session;
- }
-
- public function setSession(Session $session = null)
- {
- if ($session === null) {
- $session = Front::getInstance()->getBootstrap()->getResource('session');
- }
- if (!$session instanceof Session) {
- $session = new Session();
- $session->setNamespace(self::SESSION_NAMESPACE);
- }
- $this->_session = $session;
- return $this;
- }
-
- public function setMessage($message)
- {
- $this->_messages = $this->getMessages();
- array_push($this->_messages, $message);
- $this->_session->set($this->_name, $this->_messages);
- return $this;
- }
-
- public function getMessages()
- {
- $this->_messages = (array) $this->_session->get($this->_name);
- $this->_session->unregister($this->_name);
- return $this->_messages;
- }
- }
|