Registry.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Registry */
  23. require_once 'Zend/Registry.php';
  24. /** Zend_View_Helper_Placeholder_Container_Abstract */
  25. require_once 'Zend/View/Helper/Placeholder/Container/Abstract.php';
  26. /** Zend_View_Helper_Placeholder_Container */
  27. require_once 'Zend/View/Helper/Placeholder/Container.php';
  28. /**
  29. * Registry for placeholder containers
  30. *
  31. * @package Zend_View
  32. * @subpackage Helper
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_View_Helper_Placeholder_Registry
  37. {
  38. /**
  39. * Zend_Registry key under which placeholder registry exists
  40. * @const string
  41. */
  42. const REGISTRY_KEY = 'Zend_View_Helper_Placeholder_Registry';
  43. /**
  44. * Default container class
  45. * @var string
  46. */
  47. protected $_containerClass = 'Zend_View_Helper_Placeholder_Container';
  48. /**
  49. * Placeholder containers
  50. * @var array
  51. */
  52. protected $_items = array();
  53. /**
  54. * Retrieve or create registry instnace
  55. *
  56. * @return void
  57. */
  58. public static function getRegistry()
  59. {
  60. if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
  61. $registry = Zend_Registry::get(self::REGISTRY_KEY);
  62. } else {
  63. $registry = new self();
  64. Zend_Registry::set(self::REGISTRY_KEY, $registry);
  65. }
  66. return $registry;
  67. }
  68. /**
  69. * createContainer
  70. *
  71. * @param string $key
  72. * @param array $value
  73. * @return Zend_View_Helper_Placeholder_Container_Abstract
  74. */
  75. public function createContainer($key, array $value = array())
  76. {
  77. $key = (string) $key;
  78. $this->_items[$key] = new $this->_containerClass($value);
  79. return $this->_items[$key];
  80. }
  81. /**
  82. * Retrieve a placeholder container
  83. *
  84. * @param string $key
  85. * @return Zend_View_Helper_Placeholder_Container_Abstract
  86. */
  87. public function getContainer($key)
  88. {
  89. $key = (string) $key;
  90. if (isset($this->_items[$key])) {
  91. return $this->_items[$key];
  92. }
  93. $container = $this->createContainer($key);
  94. return $container;
  95. }
  96. /**
  97. * Does a particular container exist?
  98. *
  99. * @param string $key
  100. * @return bool
  101. */
  102. public function containerExists($key)
  103. {
  104. $key = (string) $key;
  105. $return = array_key_exists($key, $this->_items);
  106. return $return;
  107. }
  108. /**
  109. * Set the container for an item in the registry
  110. *
  111. * @param string $key
  112. * @param Zend_View_Placeholder_Container_Abstract $container
  113. * @return Zend_View_Placeholder_Registry
  114. */
  115. public function setContainer($key, Zend_View_Helper_Placeholder_Container_Abstract $container)
  116. {
  117. $key = (string) $key;
  118. $this->_items[$key] = $container;
  119. return $this;
  120. }
  121. /**
  122. * Delete a container
  123. *
  124. * @param string $key
  125. * @return bool
  126. */
  127. public function deleteContainer($key)
  128. {
  129. $key = (string) $key;
  130. if (isset($this->_items[$key])) {
  131. unset($this->_items[$key]);
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. * Set the container class to use
  138. *
  139. * @param string $name
  140. * @return Zend_View_Helper_Placeholder_Registry
  141. */
  142. public function setContainerClass($name)
  143. {
  144. if (!class_exists($name)) {
  145. require_once 'Zend/Loader.php';
  146. Zend_Loader::loadClass($name);
  147. }
  148. $reflection = new ReflectionClass($name);
  149. if (!$reflection->isSubclassOf(new ReflectionClass('Zend_View_Helper_Placeholder_Container_Abstract'))) {
  150. require_once 'Zend/View/Helper/Placeholder/Registry/Exception.php';
  151. $e = new Zend_View_Helper_Placeholder_Registry_Exception('Invalid Container class specified');
  152. $e->setView($this->view);
  153. throw $e;
  154. }
  155. $this->_containerClass = $name;
  156. return $this;
  157. }
  158. /**
  159. * Retrieve the container class
  160. *
  161. * @return string
  162. */
  163. public function getContainerClass()
  164. {
  165. return $this->_containerClass;
  166. }
  167. }