Source for file ObjectMixin.php

Documentation is available at ObjectMixin.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
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/compatibility.php';
  24. 24:  
  25. 25: require_once dirname(__FILE__'/exceptions.php';
  26. 26:  
  27. 27:  
  28. 28:  
  29. 29: /**
  30. 30:  * Nette\Object behaviour mixin.
  31. 31:  *
  32. 32:  * @author     David Grudl
  33. 33:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  34. 34:  * @package    Nette
  35. 35:  */
  36. 36: final class ObjectMixin
  37. 37: {
  38. 38:     /** @var array (method => array(type => callback)) */
  39. 39:     private static $extMethods;
  40. 40:  
  41. 41:     /** @var array */
  42. 42:     private static $methods;
  43. 43:  
  44. 44:  
  45. 45:  
  46. 46:     /**
  47. 47:      * Static class - cannot be instantiated.
  48. 48:      */
  49. 49:     final public function __construct()
  50. 50:     {
  51. 51:         throw new LogicException("Cannot instantiate static class " get_class($this));
  52. 52:     }
  53. 53:  
  54. 54:  
  55. 55:  
  56. 56:     /**
  57. 57:      * Call to undefined method.
  58. 58:      *
  59. 59:      * @param  string  method name
  60. 60:      * @param  array   arguments
  61. 61:      * @return mixed 
  62. 62:      * @throws MemberAccessException
  63. 63:      */
  64. 64:     public static function call($_this$name$args)
  65. 65:     {
  66. 66:         $class get_class($_this);
  67. 67:  
  68. 68:         if ($name === ''{
  69. 69:             throw new MemberAccessException("Call to class '$class' method without name.");
  70. 70:         }
  71. 71:  
  72. 72:         // event functionality
  73. 73:         if (preg_match('#^on[A-Z]#'$name)) {
  74. 74:             $rp new ReflectionProperty($class$name);
  75. 75:             if ($rp->isPublic(&& !$rp->isStatic()) {
  76. 76:                 $list $_this->$name;
  77. 77:                 if (is_array($list|| $list instanceof Traversable{
  78. 78:                     foreach ($list as $handler{
  79. 79:                         fixCallback($handler);
  80. 80:                         if (!is_callable($handler)) {
  81. 81:                             $able is_callable($handlerTRUE$textual);
  82. 82:                             throw new InvalidStateException("Event handler '$textual' is not ($able 'callable.' 'valid PHP callback.'));
  83. 83:                         }
  84. 84:                         call_user_func_array($handler$args);
  85. 85:                     }
  86. 86:                 }
  87. 87:                 return NULL;
  88. 88:             }
  89. 89:         }
  90. 90:  
  91. 91:         // extension methods
  92. 92:         if ($cb self::extensionMethod($class$name)) {
  93. 93:             array_unshift($args$_this);
  94. 94:             return call_user_func_array($cb$args);
  95. 95:         }
  96. 96:  
  97. 97:         throw new MemberAccessException("Call to undefined method $class::$name().");
  98. 98:     }
  99. 99:  
  100. 100:  
  101. 101:  
  102. 102:     /**
  103. 103:      * Adding method to class.
  104. 104:      *
  105. 105:      * @param  string  class name
  106. 106:      * @param  string  method name
  107. 107:      * @param  mixed   callback or closure
  108. 108:      * @return mixed 
  109. 109:      */
  110. 110:     public static function extensionMethod($class$name$callback NULL)
  111. 111:     {
  112. 112:         if (self::$extMethods === NULL || $name === NULL// for backwards compatibility
  113. 113:             $list get_defined_functions();
  114. 114:             foreach ($list['user'as $fce{
  115. 115:                 $pair explode('_prototype_'$fce);
  116. 116:                 if (count($pair=== 2{
  117. 117:                     self::$extMethods[$pair[1]][$pair[0]] $fce;
  118. 118:                     self::$extMethods[$pair[1]][''NULL;
  119. 119:                 }
  120. 120:             }
  121. 121:             if ($name === NULLreturn NULL;
  122. 122:         }
  123. 123:  
  124. 124:         $class strtolower($class);
  125. 125:         $l self::$extMethods[strtolower($name)];
  126. 126:  
  127. 127:         if ($callback !== NULL// works as setter
  128. 128:             fixCallback($callback);
  129. 129:             if (!is_callable($callback)) {
  130. 130:                 $able is_callable($callbackTRUE$textual);
  131. 131:                 throw new InvalidArgumentException("Extension method handler '$textual' is not ($able 'callable.' 'valid PHP callback.'));
  132. 132:             }
  133. 133:             $l[$class$callback;
  134. 134:             $l[''NULL;
  135. 135:             return NULL;
  136. 136:         }
  137. 137:  
  138. 138:         // works as getter
  139. 139:         if (empty($l)) {
  140. 140:             return FALSE;
  141. 141:  
  142. 142:         elseif (isset($l[''][$class])) // cached value
  143. 143:             return $l[''][$class];
  144. 144:         }
  145. 145:  
  146. 146:         $cl $class;
  147. 147:         do {
  148. 148:             $cl strtolower($cl);
  149. 149:             if (isset($l[$cl])) {
  150. 150:                 return $l[''][$class$l[$cl];
  151. 151:             }
  152. 152:         while (($cl get_parent_class($cl)) !== FALSE);
  153. 153:  
  154. 154:         foreach (class_implements($classas $cl{
  155. 155:             $cl strtolower($cl);
  156. 156:             if (isset($l[$cl])) {
  157. 157:                 return $l[''][$class$l[$cl];
  158. 158:             }
  159. 159:         }
  160. 160:         return $l[''][$classFALSE;
  161. 161:     }
  162. 162:  
  163. 163:  
  164. 164:  
  165. 165:     /**
  166. 166:      * Returns property value.
  167. 167:      *
  168. 168:      * @param  string  property name
  169. 169:      * @return mixed   property value
  170. 170:      * @throws MemberAccessException if the property is not defined.
  171. 171:      */
  172. 172:     public static function get($_this$name)
  173. 173:     {
  174. 174:         $class get_class($_this);
  175. 175:  
  176. 176:         if ($name === ''{
  177. 177:             throw new MemberAccessException("Cannot read a class '$class' property without name.");
  178. 178:         }
  179. 179:  
  180. 180:         if (!isset(self::$methods[$class])) {
  181. 181:             // get_class_methods returns ONLY PUBLIC methods of objects
  182. 182:             // but returns static methods too (nothing doing...)
  183. 183:             // and is much faster than reflection
  184. 184:             // (works good since 5.0.4)
  185. 185:             self::$methods[$classarray_flip(get_class_methods($class));
  186. 186:         }
  187. 187:  
  188. 188:         // property getter support
  189. 189:         $name[0$name[0"\xDF"// case-sensitive checking, capitalize first character
  190. 190:         $m 'get' $name;
  191. 191:         if (isset(self::$methods[$class][$m])) {
  192. 192:             // ampersands:
  193. 193:             // - uses &__get() because declaration should be forward compatible (e.g. with Nette\Web\Html)
  194. 194:             // - doesn't call &$_this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value';
  195. 195:             $val $_this->$m();
  196. 196:             return $val;
  197. 197:         }
  198. 198:  
  199. 199:         $m 'is' $name;
  200. 200:         if (isset(self::$methods[$class][$m])) {
  201. 201:             $val $_this->$m();
  202. 202:             return $val;
  203. 203:         }
  204. 204:  
  205. 205:         $name func_get_arg(1);
  206. 206:         throw new MemberAccessException("Cannot read an undeclared property $class::\$$name.");
  207. 207:     }
  208. 208:  
  209. 209:  
  210. 210:  
  211. 211:     /**
  212. 212:      * Sets value of a property.
  213. 213:      *
  214. 214:      * @param  string  property name
  215. 215:      * @param  mixed   property value
  216. 216:      * @return void 
  217. 217:      * @throws MemberAccessException if the property is not defined or is read-only
  218. 218:      */
  219. 219:     public static function set($_this$name$value)
  220. 220:     {
  221. 221:         $class get_class($_this);
  222. 222:  
  223. 223:         if ($name === ''{
  224. 224:             throw new MemberAccessException("Cannot assign to a class '$class' property without name.");
  225. 225:         }
  226. 226:  
  227. 227:         if (!isset(self::$methods[$class])) {
  228. 228:             self::$methods[$classarray_flip(get_class_methods($class));
  229. 229:         }
  230. 230:  
  231. 231:         // property setter support
  232. 232:         $name[0$name[0"\xDF"// case-sensitive checking, capitalize first character
  233. 233:         if (isset(self::$methods[$class]['get' $name]|| isset(self::$methods[$class]['is' $name])) {
  234. 234:             $m 'set' $name;
  235. 235:             if (isset(self::$methods[$class][$m])) {
  236. 236:                 $_this->$m($value);
  237. 237:                 return;
  238. 238:  
  239. 239:             else {
  240. 240:                 $name func_get_arg(1);
  241. 241:                 throw new MemberAccessException("Cannot assign to a read-only property $class::\$$name.");
  242. 242:             }
  243. 243:         }
  244. 244:  
  245. 245:         $name func_get_arg(1);
  246. 246:         throw new MemberAccessException("Cannot assign to an undeclared property $class::\$$name.");
  247. 247:     }
  248. 248:  
  249. 249:  
  250. 250:  
  251. 251:     /**
  252. 252:      * Is property defined?
  253. 253:      *
  254. 254:      * @param  string  property name
  255. 255:      * @return bool 
  256. 256:      */
  257. 257:     public static function has($_this$name)
  258. 258:     {
  259. 259:         if ($name === ''{
  260. 260:             return FALSE;
  261. 261:         }
  262. 262:  
  263. 263:         $class get_class($_this);
  264. 264:         if (!isset(self::$methods[$class])) {
  265. 265:             self::$methods[$classarray_flip(get_class_methods($class));
  266. 266:         }
  267. 267:  
  268. 268:         $name[0$name[0"\xDF";
  269. 269:         return isset(self::$methods[$class]['get' $name]|| isset(self::$methods[$class]['is' $name]);
  270. 270:     }
  271. 271: