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