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