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

Classes

Interfaces

Exceptions

  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  * @package Nette\Application\UI
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * PresenterComponent is the base class for all Presenter components.
 13:  *
 14:  * Components are persistent objects located on a presenter. They have ability to own
 15:  * other child components, and interact with user. Components have properties
 16:  * for storing their status, and responds to user command.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property-read Presenter $presenter
 21:  * @property-read string $uniqueId
 22:  * @package Nette\Application\UI
 23:  */
 24: abstract class PresenterComponent extends ComponentContainer implements ISignalReceiver, IStatePersistent, ArrayAccess
 25: {
 26:     /** @var array */
 27:     protected $params = array();
 28: 
 29: 
 30:     /**
 31:      * Returns the presenter where this component belongs to.
 32:      * @param  bool   throw exception if presenter doesn't exist?
 33:      * @return Presenter|NULL
 34:      */
 35:     public function getPresenter($need = TRUE)
 36:     {
 37:         return $this->lookup('Presenter', $need);
 38:     }
 39: 
 40: 
 41:     /**
 42:      * Returns a fully-qualified name that uniquely identifies the component
 43:      * within the presenter hierarchy.
 44:      * @return string
 45:      */
 46:     public function getUniqueId()
 47:     {
 48:         return $this->lookupPath('Presenter', TRUE);
 49:     }
 50: 
 51: 
 52:     /**
 53:      * This method will be called when the component (or component's parent)
 54:      * becomes attached to a monitored object. Do not call this method yourself.
 55:      * @param  IComponent
 56:      * @return void
 57:      */
 58:     protected function attached($presenter)
 59:     {
 60:         if ($presenter instanceof Presenter) {
 61:             $this->loadState($presenter->popGlobalParameters($this->getUniqueId()));
 62:         }
 63:     }
 64: 
 65: 
 66:     /**
 67:      * @return void
 68:      */
 69:     protected function validateParent(IComponentContainer $parent)
 70:     {
 71:         parent::validateParent($parent);
 72:         $this->monitor('Presenter');
 73:     }
 74: 
 75: 
 76:     /**
 77:      * Calls public method if exists.
 78:      * @param  string
 79:      * @param  array
 80:      * @return bool  does method exist?
 81:      */
 82:     protected function tryCall($method, array $params)
 83:     {
 84:         $rc = $this->getReflection();
 85:         if ($rc->hasMethod($method)) {
 86:             $rm = $rc->getMethod($method);
 87:             if ($rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic()) {
 88:                 $this->checkRequirements($rm);
 89:                 $rm->invokeArgs($this, $rc->combineArgs($rm, $params));
 90:                 return TRUE;
 91:             }
 92:         }
 93:         return FALSE;
 94:     }
 95: 
 96: 
 97:     /**
 98:      * Checks for requirements such as authorization.
 99:      * @return void
100:      */
101:     public function checkRequirements($element)
102:     {
103:     }
104: 
105: 
106:     /**
107:      * Access to reflection.
108:      * @return PresenterComponentReflection
109:      */
110:     public function getReflection()
111:     {
112:         return new PresenterComponentReflection($this);
113:     }
114: 
115: 
116:     /********************* interface IStatePersistent ****************d*g**/
117: 
118: 
119:     /**
120:      * Loads state informations.
121:      * @param  array
122:      * @return void
123:      */
124:     public function loadState(array $params)
125:     {
126:         $reflection = $this->getReflection();
127:         foreach ($reflection->getPersistentParams() as $name => $meta) {
128:             if (isset($params[$name])) { // NULLs are ignored
129:                 $type = gettype($meta['def'] === NULL ? $params[$name] : $meta['def']); // compatible with 2.0.x
130:                 if (!$reflection->convertType($params[$name], $type)) {
131:                     throw new BadRequestException("Invalid value for persistent parameter '$name' in '{$this->getName()}', expected " . ($type === 'NULL' ? 'scalar' : $type) . ".");
132:                 }
133:                 $this->$name = & $params[$name];
134:             } else {
135:                 $params[$name] = & $this->$name;
136:             }
137:         }
138:         $this->params = $params;
139:     }
140: 
141: 
142:     /**
143:      * Saves state informations for next request.
144:      * @param  array
145:      * @param  PresenterComponentReflection (internal, used by Presenter)
146:      * @return void
147:      */
148:     public function saveState(array & $params, $reflection = NULL)
149:     {
150:         $reflection = $reflection === NULL ? $this->getReflection() : $reflection;
151:         foreach ($reflection->getPersistentParams() as $name => $meta) {
152: 
153:             if (isset($params[$name])) {
154:                 // injected value
155: 
156:             } elseif (array_key_exists($name, $params)) { // NULLs are skipped
157:                 continue;
158: 
159:             } elseif (!isset($meta['since']) || $this instanceof $meta['since']) {
160:                 $params[$name] = $this->$name; // object property value
161: 
162:             } else {
163:                 continue; // ignored parameter
164:             }
165: 
166:             $type = gettype($meta['def'] === NULL ? $params[$name] : $meta['def']); // compatible with 2.0.x
167:             if (!PresenterComponentReflection::convertType($params[$name], $type)) {
168:                 throw new InvalidLinkException(sprintf("Invalid value for persistent parameter '%s' in '%s', expected %s.", $name, $this->getName(), $type === 'NULL' ? 'scalar' : $type));
169:             }
170: 
171:             if ($params[$name] === $meta['def'] || ($meta['def'] === NULL && is_scalar($params[$name]) && (string) $params[$name] === '')) {
172:                 $params[$name] = NULL; // value transmit is unnecessary
173:             }
174:         }
175:     }
176: 
177: 
178:     /**
179:      * Returns component param.
180:      * If no key is passed, returns the entire array.
181:      * @param  string key
182:      * @param  mixed  default value
183:      * @return mixed
184:      */
185:     public function getParameter($name = NULL, $default = NULL)
186:     {
187:         if (func_num_args() === 0) {
188:             return $this->params;
189: 
190:         } elseif (isset($this->params[$name])) {
191:             return $this->params[$name];
192: 
193:         } else {
194:             return $default;
195:         }
196:     }
197: 
198: 
199:     /**
200:      * Returns a fully-qualified name that uniquely identifies the parameter.
201:      * @param  string
202:      * @return string
203:      */
204:     public function getParameterId($name)
205:     {
206:         $uid = $this->getUniqueId();
207:         return $uid === '' ? $name : $uid . self::NAME_SEPARATOR . $name;
208:     }
209: 
210: 
211:     /** @deprecated */
212:     function getParam($name = NULL, $default = NULL)
213:     {
214:         return func_num_args() ? $this->getParameter($name, $default) : $this->getParameter();
215:     }
216: 
217: 
218:     /** @deprecated */
219:     function getParamId($name)
220:     {
221:         trigger_error(__METHOD__ . '() is deprecated; use getParameterId() instead.', E_USER_WARNING);
222:         return $this->getParameterId($name);
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 ClassReflection(func_get_arg(0));
234:         $params = array();
235:         foreach ($rc->getProperties(ReflectionProperty::IS_PUBLIC) as $rp) {
236:             if (!$rp->isStatic() && $rp->hasAnnotation('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 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" or "signal!" or "this"
279:      * @param  array|mixed
280:      * @return string
281:      * @throws InvalidLinkException
282:      */
283:     public function link($destination, $args = array())
284:     {
285:         try {
286:             $_args=func_get_args(); return $this->getPresenter()->createRequest($this, $destination, is_array($args) ? $args : array_slice($_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:]view" or "signal!"
297:      * @param  array|mixed
298:      * @return Link
299:      */
300:     public function lazyLink($destination, $args = array())
301:     {
302:         $_args=func_get_args(); return new Link($this, $destination, is_array($args) ? $args : array_slice($_args, 1));
303:     }
304: 
305: 
306:     /**
307:      * Determines whether it links to the current page.
308:      * @param  string   destination in format "[[module:]presenter:]action" or "signal!" or "this"
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:             $_args=func_get_args(); $this->getPresenter()->createRequest($this, $destination, is_array($args) ? $args : array_slice($_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:]view" or "signal!"
326:      * @param  array|mixed
327:      * @return void
328:      * @throws 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  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 IComponent
366:      * @throws 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 Framework 2.0.18 (for PHP 5.2, un-prefixed) API documentation generated by ApiGen 2.8.0