Source for file Component.php

Documentation is available at Component.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__'/IComponent.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/Object.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Component is the base class for all components.
  30. 30:  *
  31. 31:  * Components are objects implementing IComponent. They has parent component and own name.
  32. 32:  *
  33. 33:  * @author     David Grudl
  34. 34:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  35. 35:  * @package    Nette
  36. 36:  *
  37. 37:  * @property-read string $name 
  38. 38:  * @property IComponentContainer $parent 
  39. 39:  */
  40. 40: abstract class Component extends Object implements IComponent
  41. 41: {
  42. 42:     /** @var IComponentContainer */
  43. 43:     private $parent;
  44. 44:  
  45. 45:     /** @var string */
  46. 46:     private $name;
  47. 47:  
  48. 48:     /** @var array of [type => [obj, depth, path, is_monitored?]] */
  49. 49:     private $monitors array();
  50. 50:  
  51. 51:  
  52. 52:  
  53. 53:     /**
  54. 54:      */
  55. 55:     public function __construct(IComponentContainer $parent NULL$name NULL)
  56. 56:     {
  57. 57:         if ($parent !== NULL{
  58. 58:             $parent->addComponent($this$name);
  59. 59:  
  60. 60:         elseif (is_string($name)) {
  61. 61:             $this->name $name;
  62. 62:         }
  63. 63:     }
  64. 64:  
  65. 65:  
  66. 66:  
  67. 67:     /**
  68. 68:      * Lookup hierarchy for component specified by class or interface name.
  69. 69:      * @param  string class/interface type
  70. 70:      * @param  bool   throw exception if component doesn't exist?
  71. 71:      * @return IComponent 
  72. 72:      */
  73. 73:     public function lookup($type$need TRUE)
  74. 74:     {
  75. 75:         fixNamespace($type);
  76. 76:  
  77. 77:         if (!isset($this->monitors[$type])) // not monitored or not processed yet
  78. 78:             $obj $this->parent;
  79. 79:             $path self::NAME_SEPARATOR $this->name;
  80. 80:             $depth 1;
  81. 81:             while ($obj !== NULL{
  82. 82:                 if ($obj instanceof $typebreak;
  83. 83:                 $path self::NAME_SEPARATOR $obj->getName($path;
  84. 84:                 $depth++;
  85. 85:                 $obj $obj->getParent()// IComponent::getParent()
  86. 86:                 if ($obj === $this$obj NULL// prevent cycling
  87. 87:             }
  88. 88:  
  89. 89:             if ($obj{
  90. 90:                 $this->monitors[$typearray($obj$depthsubstr($path1)FALSE);
  91. 91:  
  92. 92:             else {
  93. 93:                 $this->monitors[$typearray(NULLNULLNULLFALSE)// not found
  94. 94:             }
  95. 95:         }
  96. 96:  
  97. 97:         if ($need && $this->monitors[$type][0=== NULL{
  98. 98:             throw new InvalidStateException("Component '$this->name' is not attached to '$type'.");
  99. 99:         }
  100. 100:  
  101. 101:         return $this->monitors[$type][0];
  102. 102:     }
  103. 103:  
  104. 104:  
  105. 105:  
  106. 106:     /**
  107. 107:      * Lookup for component specified by class or interface name. Returns backtrace path.
  108. 108:      * A path is the concatenation of component names separated by self::NAME_SEPARATOR.
  109. 109:      * @param  string class/interface type
  110. 110:      * @param  bool   throw exception if component doesn't exist?
  111. 111:      * @return string 
  112. 112:      */
  113. 113:     public function lookupPath($type$need TRUE)
  114. 114:     {
  115. 115:         fixNamespace($type);
  116. 116:         $this->lookup($type$need);
  117. 117:         return $this->monitors[$type][2];
  118. 118:     }
  119. 119:  
  120. 120:  
  121. 121:  
  122. 122:     /**
  123. 123:      * Starts monitoring.
  124. 124:      * @param  string class/interface type
  125. 125:      * @return void 
  126. 126:      */
  127. 127:     public function monitor($type)
  128. 128:     {
  129. 129:         fixNamespace($type);
  130. 130:         if (empty($this->monitors[$type][3])) {
  131. 131:             if ($obj $this->lookup($typeFALSE)) {
  132. 132:                 $this->attached($obj);
  133. 133:             }
  134. 134:             $this->monitors[$type][3TRUE// mark as monitored
  135. 135:         }
  136. 136:     }
  137. 137:  
  138. 138:  
  139. 139:  
  140. 140:     /**
  141. 141:      * Stops monitoring.
  142. 142:      * @param  string class/interface type
  143. 143:      * @return void 
  144. 144:      */
  145. 145:     public function unmonitor($type)
  146. 146:     {
  147. 147:         fixNamespace($type);
  148. 148:         unset($this->monitors[$type]);
  149. 149:     }
  150. 150:  
  151. 151:  
  152. 152:  
  153. 153:     /**
  154. 154:      * This method will be called when the component (or component's parent)
  155. 155:      * becomes attached to a monitored object. Do not call this method yourself.
  156. 156:      * @param  IComponent 
  157. 157:      * @return void 
  158. 158:      */
  159. 159:     protected function attached($obj)
  160. 160:     {
  161. 161:     }
  162. 162:  
  163. 163:  
  164. 164:  
  165. 165:     /**
  166. 166:      * This method will be called before the component (or component's parent)
  167. 167:      * becomes detached from a monitored object. Do not call this method yourself.
  168. 168:      * @param  IComponent 
  169. 169:      * @return void 
  170. 170:      */
  171. 171:     protected function detached($obj)
  172. 172:     {
  173. 173:     }
  174. 174:  
  175. 175:  
  176. 176:  
  177. 177:     /********************* interface IComponent ****************d*g**/
  178. 178:  
  179. 179:  
  180. 180:  
  181. 181:     /**
  182. 182:      * @return string 
  183. 183:      */
  184. 184:     final public function getName()
  185. 185:     {
  186. 186:         return $this->name;
  187. 187:     }
  188. 188:  
  189. 189:  
  190. 190:  
  191. 191:     /**
  192. 192:      * Returns the container if any.
  193. 193:      * @return IComponentContainer|NULL
  194. 194:      */
  195. 195:     final public function getParent()
  196. 196:     {
  197. 197:         return $this->parent;
  198. 198:     }
  199. 199:  
  200. 200:  
  201. 201:  
  202. 202:     /**
  203. 203:      * Sets the parent of this component. This method is managed by containers and should.
  204. 204:      * not be called by applications
  205. 205:      *
  206. 206:      * @param  IComponentContainer  New parent or null if this component is being removed from a parent
  207. 207:      * @param  string 
  208. 208:      * @return Component  provides a fluent interface
  209. 209:      * @throws InvalidStateException
  210. 210:      */
  211. 211:     public function setParent(IComponentContainer $parent NULL$name NULL)
  212. 212:     {
  213. 213:         if ($parent === NULL && $this->parent === NULL && $name !== NULL{
  214. 214:             $this->name $name// just rename
  215. 215:             return $this;
  216. 216:  
  217. 217:         elseif ($parent === $this->parent && $name === NULL{
  218. 218:             return $this// nothing to do
  219. 219:         }
  220. 220:  
  221. 221:         // A component cannot be given a parent if it already has a parent.
  222. 222:         if ($this->parent !== NULL && $parent !== NULL{
  223. 223:             throw new InvalidStateException("Component '$this->name' already has a parent.");
  224. 224:         }
  225. 225:  
  226. 226:         // remove from parent?
  227. 227:         if ($parent === NULL{
  228. 228:             // parent cannot be removed if is still this component contains
  229. 229:             if ($this->parent->getComponent($this->nameFALSE=== $this{
  230. 230:                 throw new InvalidStateException("The current parent still recognizes component '$this->name' as its child.");
  231. 231:             }
  232. 232:  
  233. 233:             $this->refreshMonitors(0);
  234. 234:             $this->parent NULL;
  235. 235:  
  236. 236:         else // add to parent
  237. 237:             // Given parent container does not already recognize this component as its child.
  238. 238:             if ($parent->getComponent($nameFALSE!== $this{
  239. 239:                 throw new InvalidStateException("The given parent does not recognize component '$name' as its child.");
  240. 240:             }
  241. 241:  
  242. 242:             $this->validateParent($parent);
  243. 243:             $this->parent $parent;
  244. 244:             if ($name !== NULL$this->name $name;
  245. 245:  
  246. 246:             $tmp array();
  247. 247:             $this->refreshMonitors(0$tmp);
  248. 248:         }
  249. 249:         return $this;
  250. 250:     }
  251. 251:  
  252. 252:  
  253. 253:  
  254. 254:     /**
  255. 255:      * Is called by a component when it is about to be set new parent. Descendant can
  256. 256:      * override this method to disallow a parent change by throwing an \InvalidStateException
  257. 257:      * @param  IComponentContainer 
  258. 258:      * @return void 
  259. 259:      * @throws InvalidStateException
  260. 260:      */
  261. 261:     protected function validateParent(IComponentContainer $parent)
  262. 262:     {
  263. 263:     }
  264. 264:  
  265. 265:  
  266. 266:  
  267. 267:     /**
  268. 268:      * Refreshes monitors.
  269. 269:      * @param  int 
  270. 270:      * @param  array|NULL(array = attaching, NULL = detaching)
  271. 271:      * @param  array 
  272. 272:      * @return void 
  273. 273:      */
  274. 274:     private function refreshMonitors($depth$missing NULL$listeners array())
  275. 275:     {
  276. 276:         if ($this instanceof IComponentContainer{
  277. 277:             foreach ($this->getComponents(as $component{
  278. 278:                 if ($component instanceof Component{
  279. 279:                     $component->refreshMonitors($depth 1$missing$listeners);
  280. 280:                 }
  281. 281:             }
  282. 282:         }
  283. 283:  
  284. 284:         if ($missing === NULL// detaching
  285. 285:             foreach ($this->monitors as $type => $rec{
  286. 286:                 if (isset($rec[1]&& $rec[1$depth{
  287. 287:                     if ($rec[3]// monitored
  288. 288:                         $this->monitors[$typearray(NULLNULLNULLTRUE);
  289. 289:                         $listeners[array($this$rec[0]);
  290. 290:                     else // not monitored, just randomly cached
  291. 291:                         unset($this->monitors[$type]);
  292. 292:                     }
  293. 293:                 }
  294. 294:             }
  295. 295:  
  296. 296:         else // attaching
  297. 297:             foreach ($this->monitors as $type => $rec{
  298. 298:                 if (isset($rec[0])) // is in cache yet
  299. 299:                     continue;
  300. 300:  
  301. 301:                 elseif (!$rec[3]// not monitored, just randomly cached
  302. 302:                     unset($this->monitors[$type]);
  303. 303:  
  304. 304:                 elseif (isset($missing[$type])) // known from previous lookup
  305. 305:                     $this->monitors[$typearray(NULLNULLNULLTRUE);
  306. 306:  
  307. 307:                 else {
  308. 308:                     $this->monitors[$typeNULL// forces re-lookup
  309. 309:                     if ($obj $this->lookup($typeFALSE)) {
  310. 310:                         $listeners[array($this$obj);
  311. 311:                     else {
  312. 312:                         $missing[$typeTRUE;
  313. 313:                     }
  314. 314:                     $this->monitors[$type][3TRUE// mark as monitored
  315. 315:                 }
  316. 316:             }
  317. 317:         }
  318. 318:  
  319. 319:         if ($depth === 0// call listeners
  320. 320:             $method $missing === NULL 'detached' 'attached';
  321. 321:             foreach ($listeners as $item{
  322. 322:                 $item[0]->$method($item[1]);
  323. 323:             }
  324. 324:         }
  325. 325:     }
  326. 326:  
  327. 327:  
  328. 328:  
  329. 329:     /********************* cloneable, serializable ****************d*g**/
  330. 330:  
  331. 331:  
  332. 332:  
  333. 333:     /**
  334. 334:      * Object cloning.
  335. 335:      */
  336. 336:     public function __clone()
  337. 337:     {
  338. 338:         if ($this->parent === NULL{
  339. 339:             return;
  340. 340:  
  341. 341:         elseif ($this->parent instanceof ComponentContainer{
  342. 342:             $this->parent $this->parent->_isCloning();
  343. 343:             if ($this->parent === NULL// not cloning
  344. 344:                 $this->refreshMonitors(0);
  345. 345:             }
  346. 346:  
  347. 347:         else {
  348. 348:             $this->parent NULL;
  349. 349:             $this->refreshMonitors(0);
  350. 350:         }
  351. 351:     }
  352. 352:  
  353. 353:  
  354. 354:  
  355. 355:     /**
  356. 356:      * Prevents unserialization.
  357. 357:      */
  358. 358:     final public function __wakeup()
  359. 359:     {
  360. 360:         throw new NotImplementedException;
  361. 361:     }
  362. 362: