Source for file PresenterHelpers.php
Documentation is available at PresenterHelpers.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
17: * @package Nette\Application
24: * Helpers for Presenter & PresenterComponent.
26: * @author David Grudl
27: * @copyright Copyright (c) 2004, 2009 David Grudl
28: * @package Nette\Application
31: final class PresenterHelpers
33: /** @var array getPersistentParams cache */
34: private static $ppCache =
array();
36: /** @var array getPersistentComponents cache */
37: private static $pcCache =
array();
39: /** @var array isMethodCallable cache */
40: private static $mcCache =
array();
42: /** @var array getMethodParams cache */
43: private static $mpCache =
array();
48: * Static class - cannot be instantiated.
50: final public function __construct()
52: throw new LogicException("Cannot instantiate static class " .
get_class($this));
58: * Returns array of classes persistent parameters.
59: * @param string class name
62: public static function getPersistentParams($class)
64: $params =
& self::$ppCache[$class];
65: if ($params !==
NULL) return $params;
68: // $class::getPersistentParams() in PHP 5.3
70: foreach (call_user_func(array($class, 'getPersistentParams'), $class) as $name =>
$meta) {
72: $params[$name] =
array(
73: 'def' =>
$defaults[$name],
85: * Returns array of classes persistent components.
86: * @param string class name
89: public static function getPersistentComponents($class)
91: $components =
& self::$pcCache[$class];
92: if ($components !==
NULL) return $components;
93: $components =
array();
95: // $class::getPersistentComponents() in PHP 5.3
96: foreach (call_user_func(array($class, 'getPersistentComponents'), $class) as $name =>
$meta) {
98: $components[$name] =
array('since' =>
$class);
108: * Is a method callable? It means class is instantiable and method has
109: * public visibility, is non-static and non-abstract.
110: * @param string class name
111: * @param string method name
114: public static function isMethodCallable($class, $method)
116: $cache =
& self::$mcCache[strtolower($class .
':' .
$method)];
117: if ($cache !==
NULL) return $cache;
122: $rc =
new ReflectionClass($class);
123: if (!$rc->isInstantiable()) {
128: $rm =
$rc->getMethod($method);
129: if (!$rm ||
!$rm->isPublic() ||
$rm->isAbstract() ||
$rm->isStatic()) {
133: return $cache =
TRUE;
135: } catch (ReflectionException $e) {
143: * Converts named parameters to list of arguments.
144: * Used by PresenterComponent::tryCall()
145: * @param string class name
146: * @param string method name
147: * @param array parameters - associative array
148: * @return array arguments - list
150: public static function paramsToArgs($class, $method, $params)
154: foreach (self::getMethodParams($class, $method) as $name =>
$def) {
155: if (isset($params[$name])) { // NULL treats as none value
156: $val =
$params[$name];
157: if ($def !==
NULL) {
172: * Converts list of arguments to named parameters.
173: * Used by Presenter::createRequest() & PresenterComponent::link()
174: * @param string class name
175: * @param string method name
176: * @param array arguments
177: * @param array supplemental arguments
179: * @throws InvalidLinkException
181: public static function argsToParams($class, $method, & $args, $supplemental =
array())
184: foreach (self::getMethodParams($class, $method) as $name =>
$def) {
186: $args[$name] =
$args[$i];
191: // continue with process
194: $args[$name] =
$supplemental[$name];
200: if ($def ===
NULL) {
201: if ((string)
$args[$name] ===
'') $args[$name] =
NULL; // value transmit is unnecessary
204: if ($args[$name] ===
$def) $args[$name] =
NULL;
216: * Returns array of methods parameters and theirs default values.
217: * @param string class name
218: * @param string method name
221: private static function getMethodParams($class, $method)
223: $cache =
& self::$mpCache[strtolower($class .
':' .
$method)];
224: if ($cache !==
NULL) return $cache;
225: $rm =
new ReflectionMethod($class, $method);
227: foreach ($rm->getParameters() as $param) {
228: $cache[$param->getName()] =
$param->isDefaultValueAvailable()
229: ?
$param->getDefaultValue()