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