Source for file PresenterHelpers.php

Documentation is available at PresenterHelpers.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Application
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: /**
  24. 24:  * Helpers for Presenter & PresenterComponent.
  25. 25:  *
  26. 26:  * @author     David Grudl
  27. 27:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  28. 28:  * @package    Nette\Application
  29. 29:  * @ignore internal
  30. 30:  */
  31. 31: final class PresenterHelpers
  32. 32: {
  33. 33:     /** @var array getPersistentParams cache */
  34. 34:     private static $ppCache array();
  35. 35:  
  36. 36:     /** @var array getPersistentComponents cache */
  37. 37:     private static $pcCache array();
  38. 38:  
  39. 39:     /** @var array isMethodCallable cache */
  40. 40:     private static $mcCache array();
  41. 41:  
  42. 42:     /** @var array getMethodParams cache */
  43. 43:     private static $mpCache array();
  44. 44:  
  45. 45:  
  46. 46:  
  47. 47:     /**
  48. 48:      * Static class - cannot be instantiated.
  49. 49:      */
  50. 50:     final public function __construct()
  51. 51:     {
  52. 52:         throw new LogicException("Cannot instantiate static class " get_class($this));
  53. 53:     }
  54. 54:  
  55. 55:  
  56. 56:  
  57. 57:     /**
  58. 58:      * Returns array of classes persistent parameters.
  59. 59:      * @param  string  class name
  60. 60:      * @return array 
  61. 61:      */
  62. 62:     public static function getPersistentParams($class)
  63. 63:     {
  64. 64:         $params self::$ppCache[$class];
  65. 65:         if ($params !== NULLreturn $params;
  66. 66:         $params array();
  67. 67:         if (is_subclass_of($class'PresenterComponent')) {
  68. 68:             // $class::getPersistentParams() in PHP 5.3
  69. 69:             $defaults get_class_vars($class);
  70. 70:             foreach (call_user_func(array($class'getPersistentParams')$classas $name => $meta{
  71. 71:                 if (is_string($meta)) $name $meta;
  72. 72:                 $params[$namearray(
  73. 73:                     'def' => $defaults[$name],
  74. 74:                     'since' => $class,
  75. 75:                 );
  76. 76:             }
  77. 77:             $params self::getPersistentParams(get_parent_class($class)) $params;
  78. 78:         }
  79. 79:         return $params;
  80. 80:     }
  81. 81:  
  82. 82:  
  83. 83:  
  84. 84:     /**
  85. 85:      * Returns array of classes persistent components.
  86. 86:      * @param  string  class name
  87. 87:      * @return array 
  88. 88:      */
  89. 89:     public static function getPersistentComponents($class)
  90. 90:     {
  91. 91:         $components self::$pcCache[$class];
  92. 92:         if ($components !== NULLreturn $components;
  93. 93:         $components array();
  94. 94:         if (is_subclass_of($class'Presenter')) {
  95. 95:             // $class::getPersistentComponents() in PHP 5.3
  96. 96:             foreach (call_user_func(array($class'getPersistentComponents')$classas $name => $meta{
  97. 97:                 if (is_string($meta)) $name $meta;
  98. 98:                 $components[$namearray('since' => $class);
  99. 99:             }
  100. 100:             $components self::getPersistentComponents(get_parent_class($class)) $components;
  101. 101:         }
  102. 102:         return $components;
  103. 103:     }
  104. 104:  
  105. 105:  
  106. 106:  
  107. 107:     /**
  108. 108:      * Is a method callable? It means class is instantiable and method has
  109. 109:      * public visibility, is non-static and non-abstract.
  110. 110:      * @param  string  class name
  111. 111:      * @param  string  method name
  112. 112:      * @return bool 
  113. 113:      */
  114. 114:     public static function isMethodCallable($class$method)
  115. 115:     {
  116. 116:         $cache self::$mcCache[strtolower($class ':' $method)];
  117. 117:         if ($cache !== NULLreturn $cache;
  118. 118:  
  119. 119:         try {
  120. 120:             $cache FALSE;
  121. 121:             // check class
  122. 122:             $rc new ReflectionClass($class);
  123. 123:             if (!$rc->isInstantiable()) {
  124. 124:                 return FALSE;
  125. 125:             }
  126. 126:  
  127. 127:             // check method
  128. 128:             $rm $rc->getMethod($method);
  129. 129:             if (!$rm || !$rm->isPublic(|| $rm->isAbstract(|| $rm->isStatic()) {
  130. 130:                 return FALSE;
  131. 131:             }
  132. 132:  
  133. 133:             return $cache TRUE;
  134. 134:  
  135. 135:         catch (ReflectionException $e{
  136. 136:             return FALSE;
  137. 137:         }
  138. 138:     }
  139. 139:  
  140. 140:  
  141. 141:  
  142. 142:     /**
  143. 143:      * Converts named parameters to list of arguments.
  144. 144:      * Used by PresenterComponent::tryCall()
  145. 145:      * @param  string  class name
  146. 146:      * @param  string  method name
  147. 147:      * @param  array   parameters - associative array
  148. 148:      * @return array   arguments  - list
  149. 149:      */
  150. 150:     public static function paramsToArgs($class$method$params)
  151. 151:     {
  152. 152:         $args array();
  153. 153:         $i 0;
  154. 154:         foreach (self::getMethodParams($class$methodas $name => $def{
  155. 155:             if (isset($params[$name])) // NULL treats as none value
  156. 156:                 $val $params[$name];
  157. 157:                 if ($def !== NULL{
  158. 158:                     settype($valgettype($def));
  159. 159:                 }
  160. 160:                 $args[$i++$val;
  161. 161:             else {
  162. 162:                 $args[$i++$def;
  163. 163:             }
  164. 164:         }
  165. 165:  
  166. 166:         return $args;
  167. 167:     }
  168. 168:  
  169. 169:  
  170. 170:  
  171. 171:     /**
  172. 172:      * Converts list of arguments to named parameters.
  173. 173:      * Used by Presenter::createRequest() & PresenterComponent::link()
  174. 174:      * @param  string  class name
  175. 175:      * @param  string  method name
  176. 176:      * @param  array   arguments
  177. 177:      * @param  array   supplemental arguments
  178. 178:      * @return void 
  179. 179:      * @throws InvalidLinkException
  180. 180:      */
  181. 181:     public static function argsToParams($class$method$args$supplemental array())
  182. 182:     {
  183. 183:         $i 0;
  184. 184:         foreach (self::getMethodParams($class$methodas $name => $def{
  185. 185:             if (array_key_exists($i$args)) {
  186. 186:                 $args[$name$args[$i];
  187. 187:                 unset($args[$i]);
  188. 188:                 $i++;
  189. 189:  
  190. 190:             elseif (array_key_exists($name$args)) {
  191. 191:                 // continue with process
  192. 192:  
  193. 193:             elseif (array_key_exists($name$supplemental)) {
  194. 194:                 $args[$name$supplemental[$name];
  195. 195:  
  196. 196:             else {
  197. 197:                 continue;
  198. 198:             }
  199. 199:  
  200. 200:             if ($def === NULL{
  201. 201:                 if ((string) $args[$name=== ''$args[$nameNULL// value transmit is unnecessary
  202. 202:             else {
  203. 203:                 settype($args[$name]gettype($def));
  204. 204:                 if ($args[$name=== $def$args[$nameNULL;
  205. 205:             }
  206. 206:         }
  207. 207:  
  208. 208:         if (array_key_exists($i$args)) {
  209. 209:             throw new InvalidLinkException("Extra parameter for signal '$class:$method'.");
  210. 210:         }
  211. 211:     }
  212. 212:  
  213. 213:  
  214. 214:  
  215. 215:     /**
  216. 216:      * Returns array of methods parameters and theirs default values.
  217. 217:      * @param  string  class name
  218. 218:      * @param  string  method name
  219. 219:      * @return array 
  220. 220:      */
  221. 221:     private static function getMethodParams($class$method)
  222. 222:     {
  223. 223:         $cache self::$mpCache[strtolower($class ':' $method)];
  224. 224:         if ($cache !== NULLreturn $cache;
  225. 225:         $rm new ReflectionMethod($class$method);
  226. 226:         $cache array();
  227. 227:         foreach ($rm->getParameters(as $param{
  228. 228:             $cache[$param->getName()$param->isDefaultValueAvailable()
  229. 229:                 ? $param->getDefaultValue()
  230. 230:                 : NULL;
  231. 231:         }
  232. 232:         return $cache;
  233. 233:     }
  234. 234: