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 http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
17: * @package Nette\Application
23: * Helpers for Presenter & PresenterComponent.
25: * @author David Grudl
26: * @copyright Copyright (c) 2004, 2009 David Grudl
27: * @package Nette\Application
30: final class PresenterHelpers
32: /** @var array getPersistentParams cache */
33: private static $ppCache =
array();
35: /** @var array getPersistentComponents cache */
36: private static $pcCache =
array();
38: /** @var array isMethodCallable cache */
39: private static $mcCache =
array();
41: /** @var array getMethodParams cache */
42: private static $mpCache =
array();
47: * Static class - cannot be instantiated.
49: final public function __construct()
51: throw new LogicException("Cannot instantiate static class " .
get_class($this));
57: * Returns array of classes persistent parameters.
58: * @param string class name
61: public static function getPersistentParams($class)
63: $params =
& self::$ppCache[$class];
64: if ($params !==
NULL) return $params;
67: // $class::getPersistentParams() in PHP 5.3
69: foreach (call_user_func(array($class, 'getPersistentParams'), $class) as $name =>
$meta) {
71: $params[$name] =
array(
72: 'def' =>
$defaults[$name],
84: * Returns array of classes persistent components.
85: * @param string class name
88: public static function getPersistentComponents($class)
90: $components =
& self::$pcCache[$class];
91: if ($components !==
NULL) return $components;
92: $components =
array();
94: // $class::getPersistentComponents() in PHP 5.3
95: foreach (call_user_func(array($class, 'getPersistentComponents'), $class) as $name =>
$meta) {
97: $components[$name] =
array('since' =>
$class);
107: * Is a method callable? It means class is instantiable and method has
108: * public visibility, is non-static and non-abstract.
109: * @param string class name
110: * @param string method name
113: public static function isMethodCallable($class, $method)
115: $cache =
& self::$mcCache[strtolower($class .
':' .
$method)];
116: if ($cache !==
NULL) return $cache;
121: $rc =
new ReflectionClass($class);
122: if (!$rc->isInstantiable()) {
127: $rm =
$rc->getMethod($method);
128: if (!$rm ||
!$rm->isPublic() ||
$rm->isAbstract() ||
$rm->isStatic()) {
132: return $cache =
TRUE;
134: } catch (ReflectionException $e) {
142: * Converts named parameters to list of arguments.
143: * Used by PresenterComponent::tryCall()
144: * @param string class name
145: * @param string method name
146: * @param array parameters - associative array
147: * @return array arguments - list
149: public static function paramsToArgs($class, $method, $params)
153: foreach (self::getMethodParams($class, $method) as $name =>
$def) {
154: if (isset($params[$name])) { // NULL treats as none value
155: $val =
$params[$name];
156: if ($def !==
NULL) {
171: * Converts list of arguments to named parameters.
172: * Used by Presenter::createRequest() & PresenterComponent::link()
173: * @param string class name
174: * @param string method name
175: * @param array arguments
176: * @param array supplemental arguments
178: * @throws InvalidLinkException
180: public static function argsToParams($class, $method, & $args, $supplemental =
array())
183: foreach (self::getMethodParams($class, $method) as $name =>
$def) {
185: $args[$name] =
$args[$i];
190: // continue with process
193: $args[$name] =
$supplemental[$name];
199: if ($def ===
NULL) {
200: if ((string)
$args[$name] ===
'') $args[$name] =
NULL; // value transmit is unnecessary
203: if ($args[$name] ===
$def) $args[$name] =
NULL;
215: * Returns array of methods parameters and theirs default values.
216: * @param string class name
217: * @param string method name
220: private static function getMethodParams($class, $method)
222: $cache =
& self::$mpCache[strtolower($class .
':' .
$method)];
223: if ($cache !==
NULL) return $cache;
224: $rm =
new ReflectionMethod($class, $method);
226: foreach ($rm->getParameters() as $param) {
227: $cache[$param->getName()] =
$param->isDefaultValueAvailable()
228: ?
$param->getDefaultValue()