Source for file ComponentContainer.php

Documentation is available at ComponentContainer.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/Component.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/IComponentContainer.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * ComponentContainer is default implementation of IComponentContainer.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette
  34. 34:  *
  35. 35:  * @property-read ArrayIterator $components 
  36. 36:  */
  37. 37: class ComponentContainer extends Component implements IComponentContainer
  38. 38: {
  39. 39:     /** @var array of IComponent */
  40. 40:     private $components array();
  41. 41:  
  42. 42:     /** @var IComponent|NULL*/
  43. 43:     private $cloning;
  44. 44:  
  45. 45:  
  46. 46:  
  47. 47:     /********************* interface IComponentContainer ****************d*g**/
  48. 48:  
  49. 49:  
  50. 50:  
  51. 51:     /**
  52. 52:      * Adds the specified component to the IComponentContainer.
  53. 53:      * @param  IComponent 
  54. 54:      * @param  string 
  55. 55:      * @param  string 
  56. 56:      * @return void 
  57. 57:      * @throws InvalidStateException
  58. 58:      */
  59. 59:     public function addComponent(IComponent $component$name$insertBefore NULL)
  60. 60:     {
  61. 61:         if ($name === NULL{
  62. 62:             $name $component->getName();
  63. 63:         }
  64. 64:  
  65. 65:         if (is_int($name)) {
  66. 66:             $name = (string) $name;
  67. 67:  
  68. 68:         elseif (!is_string($name)) {
  69. 69:             throw new InvalidArgumentException("Component name must be string, " gettype($name" given.");
  70. 70:  
  71. 71:         elseif (!preg_match('#^[a-zA-Z0-9_]+$#'$name)) {
  72. 72:             throw new InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given.");
  73. 73:         }
  74. 74:  
  75. 75:         if (isset($this->components[$name])) {
  76. 76:             throw new InvalidStateException("Component with name '$name' already exists.");
  77. 77:         }
  78. 78:  
  79. 79:         // check circular reference
  80. 80:         $obj $this;
  81. 81:         do {
  82. 82:             if ($obj === $component{
  83. 83:                 throw new InvalidStateException("Circular reference detected.");
  84. 84:             }
  85. 85:             $obj $obj->getParent();
  86. 86:         while ($obj !== NULL);
  87. 87:  
  88. 88:         // user checking
  89. 89:         $this->validateChildComponent($component);
  90. 90:  
  91. 91:         try {
  92. 92:             if (isset($this->components[$insertBefore])) {
  93. 93:                 $tmp array();
  94. 94:                 foreach ($this->components as $k => $v{
  95. 95:                     if ($k === $insertBefore$tmp[$name$component;
  96. 96:                     $tmp[$k$v;
  97. 97:                 }
  98. 98:                 $this->components $tmp;
  99. 99:             else {
  100. 100:                 $this->components[$name$component;
  101. 101:             }
  102. 102:             $component->setParent($this$name);
  103. 103:  
  104. 104:         catch (Exception $e{
  105. 105:             unset($this->components[$name])// undo
  106. 106:             throw $e;
  107. 107:         }
  108. 108:     }
  109. 109:  
  110. 110:  
  111. 111:  
  112. 112:     /**
  113. 113:      * Removes a component from the IComponentContainer.
  114. 114:      * @param  IComponent 
  115. 115:      * @return void 
  116. 116:      */
  117. 117:     public function removeComponent(IComponent $component)
  118. 118:     {
  119. 119:         $name $component->getName();
  120. 120:         if (!isset($this->components[$name]|| $this->components[$name!== $component{
  121. 121:             throw new InvalidArgumentException("Component named '$name' is not located in this container.");
  122. 122:         }
  123. 123:  
  124. 124:         unset($this->components[$name]);
  125. 125:         $component->setParent(NULL);
  126. 126:     }
  127. 127:  
  128. 128:  
  129. 129:  
  130. 130:     /**
  131. 131:      * Returns component specified by name or path.
  132. 132:      * @param  string 
  133. 133:      * @param  bool   throw exception if component doesn't exist?
  134. 134:      * @return IComponent|NULL
  135. 135:      */
  136. 136:     final public function getComponent($name$need TRUE)
  137. 137:     {
  138. 138:         $a strpos($nameself::NAME_SEPARATOR);
  139. 139:         if ($a !== FALSE{
  140. 140:             $ext substr($name$a 1);
  141. 141:             $name substr($name0$a);
  142. 142:         }
  143. 143:  
  144. 144:         if ($name == NULL{
  145. 145:             throw new InvalidArgumentException("Component or subcomponent name must be non-empty alphanumeric string, '$name' given.");
  146. 146:         }
  147. 147:  
  148. 148:         if (!isset($this->components[$name])) {
  149. 149:             $this->createComponent($name);
  150. 150:         }
  151. 151:  
  152. 152:         if (isset($this->components[$name])) {
  153. 153:             return $a === FALSE $this->components[$name$this->components[$name]->getComponent($ext$need);
  154. 154:  
  155. 155:         elseif ($need{
  156. 156:             throw new InvalidArgumentException("Component with name '$name' does not exist.");
  157. 157:  
  158. 158:         else {
  159. 159:             return NULL;
  160. 160:         }
  161. 161:     }
  162. 162:  
  163. 163:  
  164. 164:  
  165. 165:     /**
  166. 166:      * Component factory. Descendant can override this method to enable lazy component loading.
  167. 167:      * @param  string  component name
  168. 168:      * @return void 
  169. 169:      */
  170. 170:     protected function createComponent($name)
  171. 171:     {
  172. 172:     }
  173. 173:  
  174. 174:  
  175. 175:  
  176. 176:     /**
  177. 177:      * Iterates over a components.
  178. 178:      * @param  bool    recursive?
  179. 179:      * @param  string  class types filter
  180. 180:      * @return ArrayIterator 
  181. 181:      */
  182. 182:     final public function getComponents($deep FALSE$filterType NULL)
  183. 183:     {
  184. 184:         $iterator new RecursiveComponentIterator($this->components);
  185. 185:         if ($deep{
  186. 186:             $deep $deep RecursiveIteratorIterator::SELF_FIRST RecursiveIteratorIterator::CHILD_FIRST;
  187. 187:             $iterator new RecursiveIteratorIterator($iterator$deep);
  188. 188:         }
  189. 189:         if ($filterType{
  190. 190:             fixNamespace($filterType);
  191. 191:             $iterator new InstanceFilterIterator($iterator$filterType);
  192. 192:         }
  193. 193:         return $iterator;
  194. 194:     }
  195. 195:  
  196. 196:  
  197. 197:  
  198. 198:     /**
  199. 199:      * Descendant can override this method to disallow insert a child by throwing an \InvalidStateException.
  200. 200:      * @param  IComponent 
  201. 201:      * @return void 
  202. 202:      * @throws InvalidStateException
  203. 203:      */
  204. 204:     protected function validateChildComponent(IComponent $child)
  205. 205:     {
  206. 206:     }
  207. 207:  
  208. 208:  
  209. 209:  
  210. 210:     /********************* cloneable, serializable ****************d*g**/
  211. 211:  
  212. 212:  
  213. 213:  
  214. 214:     /**
  215. 215:      * Object cloning.
  216. 216:      */
  217. 217:     public function __clone()
  218. 218:     {
  219. 219:         if ($this->components{
  220. 220:             $oldMyself reset($this->components)->getParent();
  221. 221:             $oldMyself->cloning $this;
  222. 222:             foreach ($this->components as $name => $component{
  223. 223:                 $this->components[$nameclone $component;
  224. 224:             }
  225. 225:             $oldMyself->cloning NULL;
  226. 226:         }
  227. 227:         parent::__clone();
  228. 228:     }
  229. 229:  
  230. 230:  
  231. 231:  
  232. 232:     /**
  233. 233:      * Is container cloning now?
  234. 234:      * @return NULL|IComponent
  235. 235:      * @ignore internal
  236. 236:      */
  237. 237:     public function isCloning()
  238. 238:     {
  239. 239:         return $this->cloning;
  240. 240:     }
  241. 241:  
  242. 243:  
  243. 244:  
  244. 245:  
  245. 246: /**
  246. 247:  * Recursive component iterator. See ComponentContainer::getComponents().
  247. 248:  *
  248. 249:  * @author     David Grudl
  249. 250:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  250. 251:  * @package    Nette
  251. 252:  */
  252. 253: class RecursiveComponentIterator extends RecursiveArrayIterator
  253. 255:  
  254. 256:     /**
  255. 257:      * Has the current element has children?
  256. 258:      * @return bool 
  257. 259:      */
  258. 260:     public function hasChildren()
  259. 261:     {
  260. 262:         return $this->current(instanceof IComponentContainer;
  261. 263:     }
  262. 264:  
  263. 265:  
  264. 266:  
  265. 267:     /**
  266. 268:      * The sub-iterator for the current element.
  267. 269:      * @return RecursiveIterator 
  268. 270:      */
  269. 271:     public function getChildren()
  270. 272:     {
  271. 273:         return $this->current()->getComponents();
  272. 274:     }
  273. 275: