Registry.php 4.8 KB

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