Source for file PresenterComponent.php

Documentation is available at PresenterComponent.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Application
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * PresenterComponent is the base class for all presenters components.
  17. 17:  *
  18. 18:  * Components are persistent objects located on a presenter. They have ability to own
  19. 19:  * other child components, and interact with user. Components have properties
  20. 20:  * for storing their status, and responds to user command.
  21. 21:  *
  22. 22:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  23. 23:  * @package    Nette\Application
  24. 24:  *
  25. 25:  * @property-read Presenter $presenter 
  26. 26:  */
  27. 27: abstract class PresenterComponent extends ComponentContainer implements ISignalReceiverIStatePersistentArrayAccess
  28. 28: {
  29. 29:     /** @var array */
  30. 30:     protected $params = array();
  31. 31:  
  32. 32:  
  33. 33:  
  34. 34:     /**
  35. 35:      */
  36. 36:     public function __construct(IComponentContainer $parent NULL$name NULL)
  37. 37:     {
  38. 38:         $this->monitor('Nette\Application\Presenter');
  39. 39:         parent::__construct($parent$name);
  40. 40:     }
  41. 41:  
  42. 42:  
  43. 43:  
  44. 44:     /**
  45. 45:      * Returns the presenter where this component belongs to.
  46. 46:      * @param  bool   throw exception if presenter doesn't exist?
  47. 47:      * @return Presenter|NULL
  48. 48:      */
  49. 49:     public function getPresenter($need TRUE)
  50. 50:     {
  51. 51:         return $this->lookup('Nette\Application\Presenter'$need);
  52. 52:     }
  53. 53:  
  54. 54:  
  55. 55:  
  56. 56:     /**
  57. 57:      * Returns a fully-qualified name that uniquely identifies the component
  58. 58:      * within the presenter hierarchy.
  59. 59:      * @return string 
  60. 60:      */
  61. 61:     public function getUniqueId()
  62. 62:     {
  63. 63:         return $this->lookupPath('Nette\Application\Presenter'TRUE);
  64. 64:     }
  65. 65:  
  66. 66:  
  67. 67:  
  68. 68:     /**
  69. 69:      * This method will be called when the component (or component's parent)
  70. 70:      * becomes attached to a monitored object. Do not call this method yourself.
  71. 71:      * @param  IComponent 
  72. 72:      * @return void 
  73. 73:      */
  74. 74:     protected function attached($presenter)
  75. 75:     {
  76. 76:         if ($presenter instanceof Presenter{
  77. 77:             $this->loadState($presenter->popGlobalParams($this->getUniqueId()));
  78. 78:         }
  79. 79:     }
  80. 80:  
  81. 81:  
  82. 82:  
  83. 83:     /**
  84. 84:      * Calls public method if exists.
  85. 85:      * @param  string 
  86. 86:      * @param  array 
  87. 87:      * @return bool  does method exist?
  88. 88:      */
  89. 89:     protected function tryCall($methodarray $params)
  90. 90:     {
  91. 91:         $rc $this->getReflection();
  92. 92:         if ($rc->hasMethod($method)) {
  93. 93:             $rm $rc->getMethod($method);
  94. 94:             if ($rm->isPublic(&& !$rm->isAbstract(&& !$rm->isStatic()) {
  95. 95:                 $rm->invokeNamedArgs($this$params);
  96. 96:                 return TRUE;
  97. 97:             }
  98. 98:         }
  99. 99:         return FALSE;
  100. 100:     }
  101. 101:  
  102. 102:  
  103. 103:  
  104. 104:     /**
  105. 105:      * Access to reflection.
  106. 106:      * @return PresenterComponentReflection 
  107. 107:      */
  108. 108:     public function getReflection()
  109. 109:     {
  110. 110:         return new PresenterComponentReflection($this);
  111. 111:     }
  112. 112:  
  113. 113:  
  114. 114:  
  115. 115:     /********************* interface IStatePersistent ****************d*g**/
  116. 116:  
  117. 117:  
  118. 118:  
  119. 119:     /**
  120. 120:      * Loads state informations.
  121. 121:      * @param  array 
  122. 122:      * @return void 
  123. 123:      */
  124. 124:     public function loadState(array $params)
  125. 125:     {
  126. 126:         foreach ($this->getReflection()->getPersistentParams(as $nm => $meta)
  127. 127:         {
  128. 128:             if (isset($params[$nm])) // ignore NULL values
  129. 129:                 if (isset($meta['def'])) {
  130. 130:                     if (is_array($params[$nm]&& !is_array($meta['def'])) {
  131. 131:                         $params[$nm$meta['def']// prevents array to scalar conversion
  132. 132:                     else {
  133. 133:                         settype($params[$nm]gettype($meta['def']));
  134. 134:                     }
  135. 135:                 }
  136. 136:                 $this->$nm $params[$nm];
  137. 137:             }
  138. 138:         }
  139. 139:         $this->params $params;
  140. 140:     }
  141. 141:  
  142. 142:  
  143. 143:  
  144. 144:     /**
  145. 145:      * Saves state informations for next request.
  146. 146:      * @param  array 
  147. 147:      * @param  PresenterComponentReflection (internal, used by Presenter)
  148. 148:      * @return void 
  149. 149:      */
  150. 150:     public function saveState(array $params$reflection NULL)
  151. 151:     {
  152. 152:         $reflection $reflection === NULL $this->getReflection($reflection;
  153. 153:         foreach ($reflection->getPersistentParams(as $nm => $meta)
  154. 154:         {
  155. 155:             if (isset($params[$nm])) {
  156. 156:                 $val $params[$nm]// injected value
  157. 157:  
  158. 158:             elseif (array_key_exists($nm$params)) // $params[$nm] === NULL
  159. 159:                 continue// means skip
  160. 160:  
  161. 161:             elseif (!isset($meta['since']|| $this instanceof $meta['since']{
  162. 162:                 $val $this->$nm// object property value
  163. 163:  
  164. 164:             else {
  165. 165:                 continue// ignored parameter
  166. 166:             }
  167. 167:  
  168. 168:             if (is_object($val)) {
  169. 169:                 throw new InvalidStateException("Persistent parameter must be scalar or array, {$this->reflection->name}::\$$nm is gettype($val));
  170. 170:  
  171. 171:             else {
  172. 172:                 if (isset($meta['def'])) {
  173. 173:                     settype($valgettype($meta['def']));
  174. 174:                     if ($val === $meta['def']$val NULL;
  175. 175:                 else {
  176. 176:                     if ((string) $val === ''$val NULL;
  177. 177:                 }
  178. 178:                 $params[$nm$val;
  179. 179:             }
  180. 180:         }
  181. 181:     }
  182. 182:  
  183. 183:  
  184. 184:  
  185. 185:     /**
  186. 186:      * Returns component param.
  187. 187:      * If no key is passed, returns the entire array.
  188. 188:      * @param  string key
  189. 189:      * @param  mixed  default value
  190. 190:      * @return mixed 
  191. 191:      */
  192. 192:     final public function getParam($name NULL$default NULL)
  193. 193:     {
  194. 194:         if (func_num_args(=== 0{
  195. 195:             return $this->params;
  196. 196:  
  197. 197:         elseif (isset($this->params[$name])) {
  198. 198:             return $this->params[$name];
  199. 199:  
  200. 200:         else {
  201. 201:             return $default;
  202. 202:         }
  203. 203:     }
  204. 204:  
  205. 205:  
  206. 206:  
  207. 207:     /**
  208. 208:      * Returns a fully-qualified name that uniquely identifies the parameter.
  209. 209:      * @return string 
  210. 210:      */
  211. 211:     final public function getParamId($name)
  212. 212:     {
  213. 213:         $uid $this->getUniqueId();
  214. 214:         return $uid === '' $name $uid self::NAME_SEPARATOR $name;
  215. 215:     }
  216. 216:  
  217. 217:  
  218. 218:  
  219. 219:     /**
  220. 220:      * Returns array of classes persistent parameters. They have public visibility and are non-static.
  221. 221:      * This default implementation detects persistent parameters by annotation @persistent.
  222. 222:      * @return array 
  223. 223:      */
  224. 224:     public static function getPersistentParams()
  225. 225:     {
  226. 226:         $rc new ClassReflection(func_get_arg(0));
  227. 227:         $params array();
  228. 228:         foreach ($rc->getProperties(ReflectionProperty::IS_PUBLICas $rp{
  229. 229:             if (!$rp->isStatic(&& $rp->hasAnnotation('persistent')) {
  230. 230:                 $params[$rp->getName();
  231. 231:             }
  232. 232:         }
  233. 233:         return $params;
  234. 234:     }
  235. 235:  
  236. 236:  
  237. 237:  
  238. 238:     /********************* interface ISignalReceiver ****************d*g**/
  239. 239:  
  240. 240:  
  241. 241:  
  242. 242:     /**
  243. 243:      * Calls signal handler method.
  244. 244:      * @param  string 
  245. 245:      * @return void 
  246. 246:      * @throws BadSignalException if there is not handler method
  247. 247:      */
  248. 248:     public function signalReceived($signal)
  249. 249:     {
  250. 250:         if (!$this->tryCall($this->formatSignalMethod($signal)$this->params)) {
  251. 251:             throw new BadSignalException("There is no handler for signal '$signal' in {$this->reflection->name} class.");
  252. 252:         }
  253. 253:     }
  254. 254:  
  255. 255:  
  256. 256:  
  257. 257:     /**
  258. 258:      * Formats signal handler method name -> case sensitivity doesn't matter.
  259. 259:      * @param  string 
  260. 260:      * @return string 
  261. 261:      */
  262. 262:     public function formatSignalMethod($signal)
  263. 263:     {
  264. 264:         return $signal == NULL NULL 'handle' $signal// intentionally ==
  265. 265:     }
  266. 266:  
  267. 267:  
  268. 268:  
  269. 269:     /********************* navigation ****************d*g**/
  270. 270:  
  271. 271:  
  272. 272:  
  273. 273:     /**
  274. 274:      * Generates URL to presenter, action or signal.
  275. 275:      * @param  string   destination in format "[[module:]presenter:]action" or "signal!"
  276. 276:      * @param  array|mixed
  277. 277:      * @return string 
  278. 278:      * @throws InvalidLinkException
  279. 279:      */
  280. 280:     public function link($destination$args array())
  281. 281:     {
  282. 282:         if (!is_array($args)) {
  283. 283:             $args func_get_args();
  284. 284:             array_shift($args);
  285. 285:         }
  286. 286:  
  287. 287:         try {
  288. 288:             return $this->getPresenter()->createRequest($this$destination$args'link');
  289. 289:  
  290. 290:         catch (InvalidLinkException $e{
  291. 291:             return $this->getPresenter()->handleInvalidLink($e);
  292. 292:         }
  293. 293:     }
  294. 294:  
  295. 295:  
  296. 296:  
  297. 297:     /**
  298. 298:      * Returns destination as Link object.
  299. 299:      * @param  string   destination in format "[[module:]presenter:]view" or "signal!"
  300. 300:      * @param  array|mixed
  301. 301:      * @return Link 
  302. 302:      */
  303. 303:     public function lazyLink($destination$args array())
  304. 304:     {
  305. 305:         if (!is_array($args)) {
  306. 306:             $args func_get_args();
  307. 307:             array_shift($args);
  308. 308:         }
  309. 309:  
  310. 310:         return new Link($this$destination$args);
  311. 311:     }
  312. 312:  
  313. 313:  
  314. 314:  
  315. 315:     /**
  316. 316:      * @deprecated
  317. 317:      */
  318. 318:     public function ajaxLink($destination$args array())
  319. 319:     {
  320. 320:         throw new DeprecatedException(__METHOD__ . '() is deprecated.');
  321. 321:     }
  322. 322:  
  323. 323:  
  324. 324:  
  325. 325:     /**
  326. 326:      * Redirect to another presenter, action or signal.
  327. 327:      * @param  int      [optional] HTTP error code
  328. 328:      * @param  string   destination in format "[[module:]presenter:]view" or "signal!"
  329. 329:      * @param  array|mixed
  330. 330:      * @return void 
  331. 331:      * @throws AbortException
  332. 332:      */
  333. 333:     public function redirect($code$destination NULL$args array())
  334. 334:     {
  335. 335:         if (!is_numeric($code)) // first parameter is optional
  336. 336:             $args $destination;
  337. 337:             $destination $code;
  338. 338:             $code NULL;
  339. 339:         }
  340. 340:  
  341. 341:         if (!is_array($args)) {
  342. 342:             $args func_get_args();
  343. 343:             if (is_numeric(array_shift($args))) array_shift($args);
  344. 344:         }
  345. 345:  
  346. 346:         $presenter $this->getPresenter();
  347. 347:         $presenter->redirectUri($presenter->createRequest($this$destination$args'redirect')$code);
  348. 348:     }
  349. 349:  
  350. 350:  
  351. 351:  
  352. 352:     /********************* interface \ArrayAccess ****************d*g**/
  353. 353:  
  354. 354:  
  355. 355:  
  356. 356:     /**
  357. 357:      * Adds the component to the container.
  358. 358:      * @param  string  component name
  359. 359:      * @param  IComponent 
  360. 360:      * @return void. 
  361. 361:      */
  362. 362:     final public function offsetSet($name$component)
  363. 363:     {
  364. 364:         $this->addComponent($component$name);
  365. 365:     }
  366. 366:  
  367. 367:  
  368. 368:  
  369. 369:     /**
  370. 370:      * Returns component specified by name. Throws exception if component doesn't exist.
  371. 371:      * @param  string  component name
  372. 372:      * @return IComponent 
  373. 373:      * @throws InvalidArgumentException
  374. 374:      */
  375. 375:     final public function offsetGet($name)
  376. 376:     {
  377. 377:         return $this->getComponent($nameTRUE);
  378. 378:     }
  379. 379:  
  380. 380:  
  381. 381:  
  382. 382:     /**
  383. 383:      * Does component specified by name exists?
  384. 384:      * @param  string  component name
  385. 385:      * @return bool 
  386. 386:      */
  387. 387:     final public function offsetExists($name)
  388. 388:     {
  389. 389:         return $this->getComponent($nameFALSE!== NULL;
  390. 390:     }
  391. 391:  
  392. 392:  
  393. 393:  
  394. 394:     /**
  395. 395:      * Removes component from the container. Throws exception if component doesn't exist.
  396. 396:      * @param  string  component name
  397. 397:      * @return void 
  398. 398:      */
  399. 399:     final public function offsetUnset($name)
  400. 400:     {
  401. 401:         $component $this->getComponent($nameFALSE);
  402. 402:         if ($component !== NULL{
  403. 403:             $this->removeComponent($component);
  404. 404:         }
  405. 405:     }
  406. 406: