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  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Nette\Object behaviour mixin.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette
  20. 20:  */
  21. 21: final class ObjectMixin
  22. 22: {
  23. 23:     /** @var array */
  24. 24:     private static $methods;
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28:     /**
  29. 29:      * Static class - cannot be instantiated.
  30. 30:      */
  31. 31:     final public function __construct()
  32. 32:     {
  33. 33:         throw new LogicException("Cannot instantiate static class " get_class($this));
  34. 34:     }
  35. 35:  
  36. 36:  
  37. 37:  
  38. 38:     /**
  39. 39:      * Call to undefined method.
  40. 40:      *
  41. 41:      * @param  string  method name
  42. 42:      * @param  array   arguments
  43. 43:      * @return mixed 
  44. 44:      * @throws MemberAccessException
  45. 45:      */
  46. 46:     public static function call($_this$name$args)
  47. 47:     {
  48. 48:         $class new ClassReflection($_this);
  49. 49:  
  50. 50:         if ($name === ''{
  51. 51:             throw new MemberAccessException("Call to class '$class->name' method without name.");
  52. 52:         }
  53. 53:  
  54. 54:         // event functionality
  55. 55:         if ($class->hasEventProperty($name)) {
  56. 56:             if (is_array($list $_this->$name|| $list instanceof Traversable{
  57. 57:                 foreach ($list as $handler{
  58. 58:                     callback($handler)->invokeArgs($args);
  59. 59:                 }
  60. 60:             }
  61. 61:             return NULL;
  62. 62:         }
  63. 63:  
  64. 64:         // extension methods
  65. 65:         if ($cb $class->getExtensionMethod($name)) {
  66. 66:             array_unshift($args$_this);
  67. 67:             return $cb->invokeArgs($args);
  68. 68:         }
  69. 69:  
  70. 70:         throw new MemberAccessException("Call to undefined method $class->name::$name().");
  71. 71:     }
  72. 72:  
  73. 73:  
  74. 74:  
  75. 75:     /**
  76. 76:      * Returns property value.
  77. 77:      *
  78. 78:      * @param  string  property name
  79. 79:      * @return mixed   property value
  80. 80:      * @throws MemberAccessException if the property is not defined.
  81. 81:      */
  82. 82:     public static function get($_this$name)
  83. 83:     {
  84. 84:         $class get_class($_this);
  85. 85:  
  86. 86:         if ($name === ''{
  87. 87:             throw new MemberAccessException("Cannot read a class '$class' property without name.");
  88. 88:         }
  89. 89:  
  90. 90:         if (!isset(self::$methods[$class])) {
  91. 91:             // get_class_methods returns ONLY PUBLIC methods of objects
  92. 92:             // but returns static methods too (nothing doing...)
  93. 93:             // and is much faster than reflection
  94. 94:             // (works good since 5.0.4)
  95. 95:             self::$methods[$classarray_flip(get_class_methods($class));
  96. 96:         }
  97. 97:  
  98. 98:         // property getter support
  99. 99:         $name[0$name[0"\xDF"// case-sensitive checking, capitalize first character
  100. 100:         $m 'get' $name;
  101. 101:         if (isset(self::$methods[$class][$m])) {
  102. 102:             // ampersands:
  103. 103:             // - uses &__get() because declaration should be forward compatible (e.g. with Nette\Web\Html)
  104. 104:             // - doesn't call &$_this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value';
  105. 105:             $val $_this->$m();
  106. 106:             return $val;
  107. 107:         }
  108. 108:  
  109. 109:         $m 'is' $name;
  110. 110:         if (isset(self::$methods[$class][$m])) {
  111. 111:             $val $_this->$m();
  112. 112:             return $val;
  113. 113:         }
  114. 114:  
  115. 115:         $name func_get_arg(1);
  116. 116:         throw new MemberAccessException("Cannot read an undeclared property $class::\$$name.");
  117. 117:     }
  118. 118:  
  119. 119:  
  120. 120:  
  121. 121:     /**
  122. 122:      * Sets value of a property.
  123. 123:      *
  124. 124:      * @param  string  property name
  125. 125:      * @param  mixed   property value
  126. 126:      * @return void 
  127. 127:      * @throws MemberAccessException if the property is not defined or is read-only
  128. 128:      */
  129. 129:     public static function set($_this$name$value)
  130. 130:     {
  131. 131:         $class get_class($_this);
  132. 132:  
  133. 133:         if ($name === ''{
  134. 134:             throw new MemberAccessException("Cannot assign to a class '$class' property without name.");
  135. 135:         }
  136. 136:  
  137. 137:         if (!isset(self::$methods[$class])) {
  138. 138:             self::$methods[$classarray_flip(get_class_methods($class));
  139. 139:         }
  140. 140:  
  141. 141:         // property setter support
  142. 142:         $name[0$name[0"\xDF"// case-sensitive checking, capitalize first character
  143. 143:         if (isset(self::$methods[$class]['get' $name]|| isset(self::$methods[$class]['is' $name])) {
  144. 144:             $m 'set' $name;
  145. 145:             if (isset(self::$methods[$class][$m])) {
  146. 146:                 $_this->$m($value);
  147. 147:                 return;
  148. 148:  
  149. 149:             else {
  150. 150:                 $name func_get_arg(1);
  151. 151:                 throw new MemberAccessException("Cannot assign to a read-only property $class::\$$name.");
  152. 152:             }
  153. 153:         }
  154. 154:  
  155. 155:         $name func_get_arg(1);
  156. 156:         throw new MemberAccessException("Cannot assign to an undeclared property $class::\$$name.");
  157. 157:     }
  158. 158:  
  159. 159:  
  160. 160:  
  161. 161:     /**
  162. 162:      * Is property defined?
  163. 163:      *
  164. 164:      * @param  string  property name
  165. 165:      * @return bool 
  166. 166:      */
  167. 167:     public static function has($_this$name)
  168. 168:     {
  169. 169:         if ($name === ''{
  170. 170:             return FALSE;
  171. 171:         }
  172. 172:  
  173. 173:         $class get_class($_this);
  174. 174:         if (!isset(self::$methods[$class])) {
  175. 175:             self::$methods[$classarray_flip(get_class_methods($class));
  176. 176:         }
  177. 177:  
  178. 178:         $name[0$name[0"\xDF";
  179. 179:         return isset(self::$methods[$class]['get' $name]|| isset(self::$methods[$class]['is' $name]);
  180. 180:     }
  181. 181: