Source for file ObjectMixin.php
Documentation is available at ObjectMixin.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
22: require_once dirname(__FILE__) .
'/compatibility.php';
24: require_once dirname(__FILE__) .
'/exceptions.php';
29: * Nette\Object behaviour mixin.
31: * @author David Grudl
32: * @copyright Copyright (c) 2004, 2009 David Grudl
37: /** @var array (method => array(type => callback)) */
38: private static $extMethods;
41: private static $methods;
46: * Static class - cannot be instantiated.
50: throw new LogicException("Cannot instantiate static class " .
get_class($this));
56: * Call to undefined method.
58: * @param string method name
59: * @param array arguments
61: * @throws MemberAccessException
63: public static function call($_this, $name, $args)
71: // event functionality
73: $rp =
new ReflectionProperty($class, $name);
74: if ($rp->isPublic() &&
!$rp->isStatic()) {
75: $list =
$_this->$name;
76: if (is_array($list) ||
$list instanceof
Traversable) {
77: foreach ($list as $handler) {
81: throw new InvalidStateException("Event handler '$textual' is not " .
($able ?
'callable.' :
'valid PHP callback.'));
91: if ($cb =
self::extensionMethod($class, $name)) {
102: * Adding method to class.
104: * @param string class name
105: * @param string method name
106: * @param mixed callback or closure
111: if (self::$extMethods ===
NULL ||
$name ===
NULL) { // for backwards compatibility
113: foreach ($list['user'] as $fce) {
116: self::$extMethods[$pair[1]][$pair[0]] =
$fce;
117: self::$extMethods[$pair[1]][''] =
NULL;
120: if ($name ===
NULL) return NULL;
124: $l =
& self::$extMethods[strtolower($name)];
126: if ($callback !==
NULL) { // works as setter
130: throw new InvalidArgumentException("Extension method handler '$textual' is not " .
($able ?
'callable.' :
'valid PHP callback.'));
132: $l[$class] =
$callback;
141: } elseif (isset($l[''][$class])) { // cached value
142: return $l[''][$class];
148: if (isset($l[$cl])) {
149: return $l[''][$class] =
$l[$cl];
155: if (isset($l[$cl])) {
156: return $l[''][$class] =
$l[$cl];
159: return $l[''][$class] =
FALSE;
165: * Returns property value.
167: * @param string property name
168: * @return mixed property value
169: * @throws MemberAccessException if the property is not defined.
171: public static function & get($_this, $name)
179: if (!isset(self::$methods[$class])) {
180: // get_class_methods returns ONLY PUBLIC methods of objects
181: // but returns static methods too (nothing doing...)
182: // and is much faster than reflection
183: // (works good since 5.0.4)
187: // property getter support
188: $name[0] =
$name[0] & "\xDF"; // case-sensitive checking, capitalize first character
190: if (isset(self::$methods[$class][$m])) {
192: // - uses &__get() because declaration should be forward compatible (e.g. with Nette\Web\Html)
193: // - doesn't call &$_this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value';
194: $val =
$_this->$m();
199: if (isset(self::$methods[$class][$m])) {
200: $val =
$_this->$m();
204: $name =
func_get_arg(1);
211: * Sets value of a property.
213: * @param string property name
214: * @param mixed property value
216: * @throws MemberAccessException if the property is not defined or is read-only
218: public static function set($_this, $name, $value)
226: if (!isset(self::$methods[$class])) {
230: // property setter support
231: $name[0] =
$name[0] & "\xDF"; // case-sensitive checking, capitalize first character
232: if (isset(self::$methods[$class]['get' .
$name]) ||
isset(self::$methods[$class]['is' .
$name])) {
234: if (isset(self::$methods[$class][$m])) {
239: $name =
func_get_arg(1);
251: * Is property defined?
253: * @param string property name
256: public static function has($_this, $name)
263: if (!isset(self::$methods[$class])) {
267: $name[0] =
$name[0] & "\xDF";
268: return isset(self::$methods[$class]['get' .
$name]) ||
isset(self::$methods[$class]['is' .
$name]);