/vendor/magento/zendframework1/library/Zend/Controller/Action/Helper/FlashMessenger.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 313 lines · 132 code · 39 blank · 142 comment · 36 complexity · 13871214da8cde62131ae1e560633709 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Zend_Controller_Action_Helper
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Session
  23. */
  24. #require_once 'Zend/Session.php';
  25. /**
  26. * @see Zend_Controller_Action_Helper_Abstract
  27. */
  28. #require_once 'Zend/Controller/Action/Helper/Abstract.php';
  29. /**
  30. * Flash Messenger - implement session-based messages
  31. *
  32. * @uses Zend_Controller_Action_Helper_Abstract
  33. * @category Zend
  34. * @package Zend_Controller
  35. * @subpackage Zend_Controller_Action_Helper
  36. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @version $Id$
  39. */
  40. class Zend_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_Abstract implements IteratorAggregate, Countable
  41. {
  42. /**
  43. * $_messages - Messages from previous request
  44. *
  45. * @var array
  46. */
  47. static protected $_messages = array();
  48. /**
  49. * $_session - Zend_Session storage object
  50. *
  51. * @var Zend_Session
  52. */
  53. static protected $_session = null;
  54. /**
  55. * $_messageAdded - Wether a message has been previously added
  56. *
  57. * @var boolean
  58. */
  59. static protected $_messageAdded = false;
  60. /**
  61. * $_namespace - Instance namespace, default is 'default'
  62. *
  63. * @var string
  64. */
  65. protected $_namespace = 'default';
  66. /**
  67. * __construct() - Instance constructor, needed to get iterators, etc
  68. *
  69. * @param string $namespace
  70. * @return void
  71. */
  72. public function __construct()
  73. {
  74. if (!self::$_session instanceof Zend_Session_Namespace) {
  75. self::$_session = new Zend_Session_Namespace($this->getName());
  76. foreach (self::$_session as $namespace => $messages) {
  77. self::$_messages[$namespace] = $messages;
  78. unset(self::$_session->{$namespace});
  79. }
  80. }
  81. }
  82. /**
  83. * postDispatch() - runs after action is dispatched, in this
  84. * case, it is resetting the namespace in case we have forwarded to a different
  85. * action, Flashmessage will be 'clean' (default namespace)
  86. *
  87. * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
  88. */
  89. public function postDispatch()
  90. {
  91. $this->resetNamespace();
  92. return $this;
  93. }
  94. /**
  95. * setNamespace() - change the namespace messages are added to, useful for
  96. * per action controller messaging between requests
  97. *
  98. * @param string $namespace
  99. * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
  100. */
  101. public function setNamespace($namespace = 'default')
  102. {
  103. $this->_namespace = $namespace;
  104. return $this;
  105. }
  106. /**
  107. * getNamespace() - return the current namepsace
  108. *
  109. * @return string
  110. */
  111. public function getNamespace()
  112. {
  113. return $this->_namespace;
  114. }
  115. /**
  116. * resetNamespace() - reset the namespace to the default
  117. *
  118. * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
  119. */
  120. public function resetNamespace()
  121. {
  122. $this->setNamespace();
  123. return $this;
  124. }
  125. /**
  126. * addMessage() - Add a message to flash message
  127. *
  128. * @param string $message
  129. * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
  130. */
  131. public function addMessage($message, $namespace = null)
  132. {
  133. if (!is_string($namespace) || $namespace == '') {
  134. $namespace = $this->getNamespace();
  135. }
  136. if (self::$_messageAdded === false) {
  137. self::$_session->setExpirationHops(1, null, true);
  138. }
  139. if (!is_array(self::$_session->{$namespace})) {
  140. self::$_session->{$namespace} = array();
  141. }
  142. self::$_session->{$namespace}[] = $message;
  143. self::$_messageAdded = true;
  144. return $this;
  145. }
  146. /**
  147. * hasMessages() - Wether a specific namespace has messages
  148. *
  149. * @return boolean
  150. */
  151. public function hasMessages($namespace = null)
  152. {
  153. if (!is_string($namespace) || $namespace == '') {
  154. $namespace = $this->getNamespace();
  155. }
  156. return isset(self::$_messages[$namespace]);
  157. }
  158. /**
  159. * getMessages() - Get messages from a specific namespace
  160. *
  161. * @return array
  162. */
  163. public function getMessages($namespace = null)
  164. {
  165. if (!is_string($namespace) || $namespace == '') {
  166. $namespace = $this->getNamespace();
  167. }
  168. if ($this->hasMessages($namespace)) {
  169. return self::$_messages[$namespace];
  170. }
  171. return array();
  172. }
  173. /**
  174. * Clear all messages from the previous request & current namespace
  175. *
  176. * @return boolean True if messages were cleared, false if none existed
  177. */
  178. public function clearMessages($namespace = null)
  179. {
  180. if (!is_string($namespace) || $namespace == '') {
  181. $namespace = $this->getNamespace();
  182. }
  183. if ($this->hasMessages($namespace)) {
  184. unset(self::$_messages[$namespace]);
  185. return true;
  186. }
  187. return false;
  188. }
  189. /**
  190. * hasCurrentMessages() - check to see if messages have been added to current
  191. * namespace within this request
  192. *
  193. * @return boolean
  194. */
  195. public function hasCurrentMessages($namespace = null)
  196. {
  197. if (!is_string($namespace) || $namespace == '') {
  198. $namespace = $this->getNamespace();
  199. }
  200. return isset(self::$_session->{$namespace});
  201. }
  202. /**
  203. * getCurrentMessages() - get messages that have been added to the current
  204. * namespace within this request
  205. *
  206. * @return array
  207. */
  208. public function getCurrentMessages($namespace = null)
  209. {
  210. if (!is_string($namespace) || $namespace == '') {
  211. $namespace = $this->getNamespace();
  212. }
  213. if ($this->hasCurrentMessages($namespace)) {
  214. return self::$_session->{$namespace};
  215. }
  216. return array();
  217. }
  218. /**
  219. * clear messages from the current request & current namespace
  220. *
  221. * @return boolean
  222. */
  223. public function clearCurrentMessages($namespace = null)
  224. {
  225. if (!is_string($namespace) || $namespace == '') {
  226. $namespace = $this->getNamespace();
  227. }
  228. if ($this->hasCurrentMessages($namespace)) {
  229. unset(self::$_session->{$namespace});
  230. return true;
  231. }
  232. return false;
  233. }
  234. /**
  235. * getIterator() - complete the IteratorAggregate interface, for iterating
  236. *
  237. * @return ArrayObject
  238. */
  239. public function getIterator($namespace = null)
  240. {
  241. if (!is_string($namespace) || $namespace == '') {
  242. $namespace = $this->getNamespace();
  243. }
  244. if ($this->hasMessages($namespace)) {
  245. return new ArrayObject($this->getMessages($namespace));
  246. }
  247. return new ArrayObject();
  248. }
  249. /**
  250. * count() - Complete the countable interface
  251. *
  252. * @return int
  253. */
  254. public function count($namespace = null)
  255. {
  256. if (!is_string($namespace) || $namespace == '') {
  257. $namespace = $this->getNamespace();
  258. }
  259. if ($this->hasMessages($namespace)) {
  260. return count($this->getMessages($namespace));
  261. }
  262. return 0;
  263. }
  264. /**
  265. * Strategy pattern: proxy to addMessage()
  266. *
  267. * @param string $message
  268. * @return void
  269. */
  270. public function direct($message, $namespace=NULL)
  271. {
  272. return $this->addMessage($message, $namespace);
  273. }
  274. }