1<?php 2/** 3 * Zend Framework (http://framework.zend.com/) 4 * 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 8 */ 9 10namespace Zend\View\Helper\Placeholder; 11 12use Zend\View\Exception; 13 14/** 15 * Registry for placeholder containers 16 */ 17class Registry 18{ 19 /** 20 * Singleton instance 21 * 22 * @var Registry 23 */ 24 protected static $instance; 25 26 /** 27 * Default container class 28 * 29 * @var string 30 */ 31 protected $containerClass = 'Zend\View\Helper\Placeholder\Container'; 32 33 /** 34 * Placeholder containers 35 * 36 * @var array 37 */ 38 protected $items = array(); 39 40 /** 41 * Retrieve or create registry instance 42 * 43 * @return Registry 44 */ 45 public static function getRegistry() 46 { 47 trigger_error('Placeholder view helpers should no longer use a singleton registry', E_USER_DEPRECATED); 48 if (null === static::$instance) { 49 static::$instance = new static(); 50 } 51 52 return static::$instance; 53 } 54 55 /** 56 * Unset the singleton 57 * 58 * Primarily useful for testing purposes; sets {@link $instance} to null. 59 * 60 * @return void 61 */ 62 public static function unsetRegistry() 63 { 64 trigger_error('Placeholder view helpers should no longer use a singleton registry', E_USER_DEPRECATED); 65 static::$instance = null; 66 } 67 68 /** 69 * Set the container for an item in the registry 70 * 71 * @param string $key 72 * @param Container\AbstractContainer $container 73 * @return Registry 74 */ 75 public function setContainer($key, Container\AbstractContainer $container) 76 { 77 $key = (string) $key; 78 $this->items[$key] = $container; 79 80 return $this; 81 } 82 83 /** 84 * Retrieve a placeholder container 85 * 86 * @param string $key 87 * @return Container\AbstractContainer 88 */ 89 public function getContainer($key) 90 { 91 $key = (string) $key; 92 if (isset($this->items[$key])) { 93 return $this->items[$key]; 94 } 95 96 $container = $this->createContainer($key); 97 98 return $container; 99 } 100 101 /** 102 * Does a particular container exist? 103 * 104 * @param string $key 105 * @return bool 106 */ 107 public function containerExists($key) 108 { 109 $key = (string) $key; 110 111 return array_key_exists($key, $this->items); 112 } 113 114 /** 115 * createContainer 116 * 117 * @param string $key 118 * @param array $value 119 * @return Container\AbstractContainer 120 */ 121 public function createContainer($key, array $value = array()) 122 { 123 $key = (string) $key; 124 125 $this->items[$key] = new $this->containerClass($value); 126 127 return $this->items[$key]; 128 } 129 130 /** 131 * Delete a container 132 * 133 * @param string $key 134 * @return bool 135 */ 136 public function deleteContainer($key) 137 { 138 $key = (string) $key; 139 if (isset($this->items[$key])) { 140 unset($this->items[$key]); 141 return true; 142 } 143 144 return false; 145 } 146 147 /** 148 * Set the container class to use 149 * 150 * @param string $name 151 * @throws Exception\InvalidArgumentException 152 * @throws Exception\DomainException 153 * @return Registry 154 */ 155 public function setContainerClass($name) 156 { 157 if (!class_exists($name)) { 158 throw new Exception\DomainException( 159 sprintf( 160 '%s expects a valid registry class name; received "%s", which did not resolve', 161 __METHOD__, 162 $name 163 ) 164 ); 165 } 166 167 if (!in_array('Zend\View\Helper\Placeholder\Container\AbstractContainer', class_parents($name))) { 168 throw new Exception\InvalidArgumentException('Invalid Container class specified'); 169 } 170 171 $this->containerClass = $name; 172 173 return $this; 174 } 175 176 /** 177 * Retrieve the container class 178 * 179 * @return string 180 */ 181 public function getContainerClass() 182 { 183 return $this->containerClass; 184 } 185} 186