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