| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfMixer |
|---|
| 20 |
{ |
|---|
| 21 |
static protected |
|---|
| 22 |
$mixins = array(), |
|---|
| 23 |
$mixinParameters = array(), |
|---|
| 24 |
$mixinInstances = array(); |
|---|
| 25 |
|
|---|
| 26 |
static public function register($name, $callable, $multiple = false) |
|---|
| 27 |
{ |
|---|
| 28 |
$lazy = false; |
|---|
| 29 |
|
|---|
| 30 |
if (is_array($callable)) |
|---|
| 31 |
{ |
|---|
| 32 |
$mixinClass = $callable[0]; |
|---|
| 33 |
$mixinMethod = $callable[1]; |
|---|
| 34 |
if (!is_object($mixinClass)) |
|---|
| 35 |
{ |
|---|
| 36 |
$rc = new ReflectionClass($mixinClass); |
|---|
| 37 |
$rm = $rc->getMethod($mixinMethod); |
|---|
| 38 |
if (!$rm->isStatic()) |
|---|
| 39 |
{ |
|---|
| 40 |
$lazy = true; |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
} |
|---|
| 44 |
else |
|---|
| 45 |
{ |
|---|
| 46 |
$mixinMethod = $callable; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
$tmp = explode(':', $name); |
|---|
| 50 |
$class = $tmp[0]; |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
if (isset($tmp[1])) |
|---|
| 54 |
{ |
|---|
| 55 |
$method = $tmp[1]; |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
if (isset($tmp[2])) |
|---|
| 59 |
{ |
|---|
| 60 |
$hook = $tmp[2]; |
|---|
| 61 |
} |
|---|
| 62 |
else |
|---|
| 63 |
{ |
|---|
| 64 |
$hook = $method; |
|---|
| 65 |
$name .= ':'.$hook; |
|---|
| 66 |
} |
|---|
| 67 |
} |
|---|
| 68 |
else |
|---|
| 69 |
{ |
|---|
| 70 |
|
|---|
| 71 |
$method = $mixinMethod; |
|---|
| 72 |
$name = $class.':'.$method; |
|---|
| 73 |
$hook = ''; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
if (!$multiple) |
|---|
| 78 |
{ |
|---|
| 79 |
foreach (self::getCallables($name) as $localCallable) |
|---|
| 80 |
{ |
|---|
| 81 |
if (is_array($localCallable) && is_array($callable)) |
|---|
| 82 |
{ |
|---|
| 83 |
if ($callable[1] == $localCallable[1] && (is_object($callable[0]) ? get_class($callable[0]) : $callable[0]) == (is_object($localCallable[0]) ? get_class($localCallable[0]) : $localCallable[0])) |
|---|
| 84 |
{ |
|---|
| 85 |
return; |
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
else if (is_string($localCallable) && is_string($callable) && $callable == $localCallable) |
|---|
| 89 |
{ |
|---|
| 90 |
return; |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
if (!$hook && isset(self::$mixins[$name])) |
|---|
| 97 |
{ |
|---|
| 98 |
throw new Exception(sprintf('The class "%s" has already a mixin for method "%s".', $class, $mixinMethod)); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
if (!isset(self::$mixins[$name])) |
|---|
| 103 |
{ |
|---|
| 104 |
self::$mixins[$name] = array(); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
if (!isset(self::$mixinParameters[$name])) |
|---|
| 108 |
{ |
|---|
| 109 |
self::$mixinParameters[$name] = array(); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
self::$mixins[$name][] = $callable; |
|---|
| 113 |
self::$mixinParameters[$name][] = array( |
|---|
| 114 |
'lazy' => $lazy, |
|---|
| 115 |
'class' => $class, |
|---|
| 116 |
'method' => $method, |
|---|
| 117 |
'hook' => $hook, |
|---|
| 118 |
); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
static public function getMixinInstance($name) |
|---|
| 122 |
{ |
|---|
| 123 |
if (!isset(self::$mixins[$name])) |
|---|
| 124 |
{ |
|---|
| 125 |
return; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
foreach (self::$mixins[$name] as $i => $mixin) |
|---|
| 129 |
{ |
|---|
| 130 |
if (!self::$mixinParameters[$name][$i]['lazy']) |
|---|
| 131 |
{ |
|---|
| 132 |
continue; |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
$class = $mixin[0]; |
|---|
| 136 |
if (!isset(self::$mixinInstances[$class])) |
|---|
| 137 |
{ |
|---|
| 138 |
self::$mixinInstances[$class] = new $class(); |
|---|
| 139 |
if (method_exists(self::$mixinInstances[$class], 'initialize')) |
|---|
| 140 |
{ |
|---|
| 141 |
self::$mixinInstances[$class]->initialize(); |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
self::$mixinParameters[$name][$i]['lazy'] = false; |
|---|
| 146 |
self::$mixins[$name][$i][0] = self::$mixinInstances[$class]; |
|---|
| 147 |
} |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
static public function getCallables($name) |
|---|
| 151 |
{ |
|---|
| 152 |
self::getMixinInstance($name); |
|---|
| 153 |
|
|---|
| 154 |
return isset(self::$mixins[$name]) ? self::$mixins[$name] : array(); |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
static public function getCallable($name) |
|---|
| 158 |
{ |
|---|
| 159 |
self::getMixinInstance($name); |
|---|
| 160 |
|
|---|
| 161 |
return isset(self::$mixins[$name]) ? self::$mixins[$name][0] : null; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
static public function callMixins($hookName = null, $moreParams = array()) |
|---|
| 165 |
{ |
|---|
| 166 |
$traces = debug_backtrace(); |
|---|
| 167 |
$function = $traces[1]['function']; |
|---|
| 168 |
$parameters = $traces[1]['args']; |
|---|
| 169 |
$class = $traces[1]['class']; |
|---|
| 170 |
$type = $traces[1]['type']; |
|---|
| 171 |
if ('__call' == $function) |
|---|
| 172 |
{ |
|---|
| 173 |
$method = $parameters[0]; |
|---|
| 174 |
$parameters = $parameters[1]; |
|---|
| 175 |
} |
|---|
| 176 |
else |
|---|
| 177 |
{ |
|---|
| 178 |
$method = $function; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
if ('->' == $type) |
|---|
| 182 |
{ |
|---|
| 183 |
array_unshift($parameters, $traces[1]['object']); |
|---|
| 184 |
} |
|---|
| 185 |
else |
|---|
| 186 |
{ |
|---|
| 187 |
array_unshift($parameters, $class); |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
$parameters = array_merge($parameters, (array) $moreParams); |
|---|
| 192 |
|
|---|
| 193 |
if ('__call' == $function) |
|---|
| 194 |
{ |
|---|
| 195 |
if ($callable = self::getCallable($class.':'.$method)) |
|---|
| 196 |
{ |
|---|
| 197 |
return call_user_func_array($callable, $parameters); |
|---|
| 198 |
} |
|---|
| 199 |
else |
|---|
| 200 |
{ |
|---|
| 201 |
throw new Exception(sprintf('Call to undefined method %s::%s.', $class, $method)); |
|---|
| 202 |
} |
|---|
| 203 |
} |
|---|
| 204 |
else |
|---|
| 205 |
{ |
|---|
| 206 |
$hookName = $hookName ? $hookName : $method; |
|---|
| 207 |
foreach (self::getCallables($class.':'.$method.':'.$hookName) as $callable) |
|---|
| 208 |
{ |
|---|
| 209 |
call_user_func_array($callable, $parameters); |
|---|
| 210 |
} |
|---|
| 211 |
} |
|---|
| 212 |
} |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|