Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • Control
  • Form
  • Multiplier
  • Presenter
  • PresenterComponent

Interfaces

  • IRenderable
  • ISignalReceiver
  • IStatePersistent
  • ITemplate
  • ITemplateFactory

Exceptions

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