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 integer or 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 while adding component '$name'.");
  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:         if (is_int($name)) {
  139. 139:             $name = (string) $name;
  140. 140:  
  141. 141:         elseif (!is_string($name)) {
  142. 142:             throw new InvalidArgumentException("Component name must be integer or string, " gettype($name" given.");
  143. 143:  
  144. 144:         else {
  145. 145:             $a strpos($nameself::NAME_SEPARATOR);
  146. 146:             if ($a !== FALSE{
  147. 147:                 $ext = (string) substr($name$a 1);
  148. 148:                 $name substr($name0$a);
  149. 149:             }
  150. 150:  
  151. 151:             if ($name === ''{
  152. 152:                 throw new InvalidArgumentException("Component or subcomponent name must not be empty string.");
  153. 153:             }
  154. 154:         }
  155. 155:  
  156. 156:         if (!isset($this->components[$name])) {
  157. 157:             $this->createComponent($name);
  158. 158:         }
  159. 159:  
  160. 160:         if (isset($this->components[$name])) {
  161. 161:             if (!isset($ext)) {
  162. 162:                 return $this->components[$name];
  163. 163:  
  164. 164:             elseif ($this->components[$nameinstanceof IComponentContainer{
  165. 165:                 return $this->components[$name]->getComponent($ext$need);
  166. 166:  
  167. 167:             elseif ($need{
  168. 168:                 throw new InvalidArgumentException("Component with name '$name' is not container and cannot have '$ext' component.");
  169. 169:             }
  170. 170:  
  171. 171:         elseif ($need{
  172. 172:             throw new InvalidArgumentException("Component with name '$name' does not exist.");
  173. 173:         }
  174. 174:     }
  175. 175:  
  176. 176:  
  177. 177:  
  178. 178:     /**
  179. 179:      * Component factory. Delegates the creation of components to a createComponent<Name> method.
  180. 180:      * @param  string  component name
  181. 181:      * @return void 
  182. 182:      */
  183. 183:     protected function createComponent($name)
  184. 184:     {
  185. 185:         $ucname ucfirst($name);
  186. 186:         $method 'createComponent' $ucname;
  187. 187:         if ($ucname !== $name && method_exists($this$method&& $this->getReflection()->getMethod($method)->getName(=== $method{
  188. 188:             $component $this->$method($name);
  189. 189:             if ($component instanceof IComponent && $component->getParent(=== NULL{
  190. 190:                 $this->addComponent($component$name);
  191. 191:             }
  192. 192:         }
  193. 193:     }
  194. 194:  
  195. 195:  
  196. 196:  
  197. 197:     /**
  198. 198:      * Iterates over a components.
  199. 199:      * @param  bool    recursive?
  200. 200:      * @param  string  class types filter
  201. 201:      * @return ArrayIterator 
  202. 202:      */
  203. 203:     final public function getComponents($deep FALSE$filterType NULL)
  204. 204:     {
  205. 205:         $iterator new RecursiveComponentIterator($this->components);
  206. 206:         if ($deep{
  207. 207:             $deep $deep RecursiveIteratorIterator::SELF_FIRST RecursiveIteratorIterator::CHILD_FIRST;
  208. 208:             $iterator new RecursiveIteratorIterator($iterator$deep);
  209. 209:         }
  210. 210:         if ($filterType{
  211. 211:             fixNamespace($filterType);
  212. 212:             $iterator new InstanceFilterIterator($iterator$filterType);
  213. 213:         }
  214. 214:         return $iterator;
  215. 215:     }
  216. 216:  
  217. 217:  
  218. 218:  
  219. 219:     /**
  220. 220:      * Descendant can override this method to disallow insert a child by throwing an \InvalidStateException.
  221. 221:      * @param  IComponent 
  222. 222:      * @return void 
  223. 223:      * @throws InvalidStateException
  224. 224:      */
  225. 225:     protected function validateChildComponent(IComponent $child)
  226. 226:     {
  227. 227:     }
  228. 228:  
  229. 229:  
  230. 230:  
  231. 231:     /********************* cloneable, serializable ****************d*g**/
  232. 232:  
  233. 233:  
  234. 234:  
  235. 235:     /**
  236. 236:      * Object cloning.
  237. 237:      */
  238. 238:     public function __clone()
  239. 239:     {
  240. 240:         if ($this->components{
  241. 241:             $oldMyself reset($this->components)->getParent();
  242. 242:             $oldMyself->cloning $this;
  243. 243:             foreach ($this->components as $name => $component{
  244. 244:                 $this->components[$nameclone $component;
  245. 245:             }
  246. 246:             $oldMyself->cloning NULL;
  247. 247:         }
  248. 248:         parent::__clone();
  249. 249:     }
  250. 250:  
  251. 251:  
  252. 252:  
  253. 253:     /**
  254. 254:      * Is container cloning now?
  255. 255:      * @return NULL|IComponent
  256. 256:      * @ignore internal
  257. 257:      */
  258. 258:     public function _isCloning()
  259. 259:     {
  260. 260:         return $this->cloning;
  261. 261:     }
  262. 262:  
  263. 264:  
  264. 265:  
  265. 266:  
  266. 267: /**
  267. 268:  * Recursive component iterator. See ComponentContainer::getComponents().
  268. 269:  *
  269. 270:  * @author     David Grudl
  270. 271:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  271. 272:  * @package    Nette
  272. 273:  */
  273. 274: class RecursiveComponentIterator extends RecursiveArrayIterator implements Countable
  274. 276:  
  275. 277:     /**
  276. 278:      * Has the current element has children?
  277. 279:      * @return bool 
  278. 280:      */
  279. 281:     public function hasChildren()
  280. 282:     {
  281. 283:         return $this->current(instanceof IComponentContainer;
  282. 284:     }
  283. 285:  
  284. 286:  
  285. 287:  
  286. 288:     /**
  287. 289:      * The sub-iterator for the current element.
  288. 290:      * @return RecursiveIterator 
  289. 291:      */
  290. 292:     public function getChildren()
  291. 293:     {
  292. 294:         return $this->current()->getComponents();
  293. 295:     }
  294. 296:  
  295. 297:  
  296. 298:  
  297. 299:     /**
  298. 300:      * Returns the count of elements.
  299. 301:      * @return int 
  300. 302:      */
  301. 303:     public function count()
  302. 304:     {
  303. 305:         return iterator_count($this);
  304. 306:     }
  305. 307: