Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • AppForm
  • Control
  • Multiplier
  • Presenter
  • PresenterComponent

Interfaces

  • IRenderable
  • ISignalReceiver
  • IStatePersistent

Exceptions

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