|
20 | 20 | */
|
21 | 21 | class ReflectionCaster
|
22 | 22 | {
|
| 23 | + private static $extraMap = array( |
| 24 | + 'docComment' => 'getDocComment', |
| 25 | + 'extension' => 'getExtensionName', |
| 26 | + 'isDisabled' => 'isDisabled', |
| 27 | + 'isDeprecated' => 'isDeprecated', |
| 28 | + 'isInternal' => 'isInternal', |
| 29 | + 'isUserDefined' => 'isUserDefined', |
| 30 | + 'isGenerator' => 'isGenerator', |
| 31 | + 'isVariadic' => 'isVariadic', |
| 32 | + ); |
| 33 | + |
| 34 | + /** |
| 35 | + * @deprecated since Symfony 2.7, to be removed in 3.0. |
| 36 | + */ |
23 | 37 | public static function castReflector(\Reflector $c, array $a, Stub $stub, $isNested)
|
24 | 38 | {
|
25 |
| - $a["\0~\0reflection"] = $c->__toString(); |
| 39 | + trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0.', E_USER_DEPRECATED); |
| 40 | + $a[Caster::PREFIX_VIRTUAL.'reflection'] = $c->__toString(); |
26 | 41 |
|
27 | 42 | return $a;
|
28 | 43 | }
|
29 | 44 |
|
30 | 45 | public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
|
31 | 46 | {
|
| 47 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 48 | + $c = new \ReflectionFunction($c); |
| 49 | + |
32 | 50 | $stub->class = 'Closure'; // HHVM generates unique class names for closures
|
33 |
| - $a = static::castReflector(new \ReflectionFunction($c), $a, $stub, $isNested); |
34 |
| - unset($a["\0+\0000"], $a['name'], $a["\0+\0this"], $a["\0+\0parameter"]); |
| 51 | + $a = static::castFunctionAbstract($c, $a, $stub, $isNested); |
| 52 | + |
| 53 | + if (isset($a[$prefix.'parameters'])) { |
| 54 | + foreach ($a[$prefix.'parameters'] as &$v) { |
| 55 | + $param = $v; |
| 56 | + $v = array(); |
| 57 | + foreach (static::castParameter($param, array(), $stub, true) as $k => $param) { |
| 58 | + if ("\0" === $k[0]) { |
| 59 | + $v[substr($k, 3)] = $param; |
| 60 | + } |
| 61 | + } |
| 62 | + unset($v['position'], $v['isVariadic'], $v['byReference'], $v); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if ($f = $c->getFileName()) { |
| 67 | + $a[$prefix.'file'] = $f; |
| 68 | + $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine(); |
| 69 | + } |
| 70 | + |
| 71 | + $prefix = Caster::PREFIX_DYNAMIC; |
| 72 | + unset($a['name'], $a[$prefix.'0'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']); |
| 73 | + |
| 74 | + return $a; |
| 75 | + } |
| 76 | + |
| 77 | + public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0) |
| 78 | + { |
| 79 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 80 | + |
| 81 | + if ($n = \Reflection::getModifierNames($c->getModifiers())) { |
| 82 | + $a[$prefix.'modifiers'] = implode(' ', $n); |
| 83 | + } |
| 84 | + |
| 85 | + self::addMap($a, $c, array( |
| 86 | + 'extends' => 'getParentClass', |
| 87 | + 'implements' => 'getInterfaceNames', |
| 88 | + 'constants' => 'getConstants', |
| 89 | + )); |
| 90 | + |
| 91 | + foreach ($c->getProperties() as $n) { |
| 92 | + $a[$prefix.'properties'][$n->name] = $n; |
| 93 | + } |
| 94 | + |
| 95 | + foreach ($c->getMethods() as $n) { |
| 96 | + $a[$prefix.'methods'][$n->name] = $n; |
| 97 | + } |
| 98 | + |
| 99 | + if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) { |
| 100 | + self::addExtra($a, $c); |
| 101 | + } |
35 | 102 |
|
36 | 103 | return $a;
|
37 | 104 | }
|
| 105 | + |
| 106 | + public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0) |
| 107 | + { |
| 108 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 109 | + |
| 110 | + self::addMap($a, $c, array( |
| 111 | + 'returnsReference' => 'returnsReference', |
| 112 | + 'class' => 'getClosureScopeClass', |
| 113 | + 'this' => 'getClosureThis', |
| 114 | + )); |
| 115 | + |
| 116 | + if (isset($a[$prefix.'this'])) { |
| 117 | + $a[$prefix.'this'] = new CutStub($a[$prefix.'this']); |
| 118 | + } |
| 119 | + |
| 120 | + foreach ($c->getParameters() as $v) { |
| 121 | + $k = '$'.$v->name; |
| 122 | + if ($v->isPassedByReference()) { |
| 123 | + $k = '&'.$k; |
| 124 | + } |
| 125 | + if (method_exists($v, 'isVariadic') && $v->isVariadic()) { |
| 126 | + $k = '...'.$k; |
| 127 | + } |
| 128 | + $a[$prefix.'parameters'][$k] = $v; |
| 129 | + } |
| 130 | + |
| 131 | + if ($v = $c->getStaticVariables()) { |
| 132 | + foreach ($v as $k => &$v) { |
| 133 | + $a[$prefix.'use']['$'.$k] =& $v; |
| 134 | + } |
| 135 | + unset($v); |
| 136 | + } |
| 137 | + |
| 138 | + if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) { |
| 139 | + self::addExtra($a, $c); |
| 140 | + } |
| 141 | + |
| 142 | + return $a; |
| 143 | + } |
| 144 | + |
| 145 | + public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested) |
| 146 | + { |
| 147 | + $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers())); |
| 148 | + |
| 149 | + return $a; |
| 150 | + } |
| 151 | + |
| 152 | + public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested) |
| 153 | + { |
| 154 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 155 | + |
| 156 | + self::addMap($a, $c, array( |
| 157 | + 'position' => 'getPosition', |
| 158 | + 'isVariadic' => 'isVariadic', |
| 159 | + 'byReference' => 'isPassedByReference', |
| 160 | + )); |
| 161 | + |
| 162 | + try { |
| 163 | + if ($c->isArray()) { |
| 164 | + $a[$prefix.'typeHint'] = 'array'; |
| 165 | + } elseif (method_exists($c, 'isCallable') && $c->isCallable()) { |
| 166 | + $a[$prefix.'typeHint'] = 'callable'; |
| 167 | + } elseif ($v = $c->getClass()) { |
| 168 | + $a[$prefix.'typeHint'] = $v->name; |
| 169 | + } |
| 170 | + } catch (\ReflectionException $e) { |
| 171 | + } |
| 172 | + |
| 173 | + try { |
| 174 | + $a[$prefix.'default'] = $v = $c->getDefaultValue(); |
| 175 | + if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) { |
| 176 | + $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v); |
| 177 | + } |
| 178 | + } catch (\ReflectionException $e) { |
| 179 | + } |
| 180 | + |
| 181 | + return $a; |
| 182 | + } |
| 183 | + |
| 184 | + public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested) |
| 185 | + { |
| 186 | + $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers())); |
| 187 | + self::addExtra($a, $c); |
| 188 | + |
| 189 | + return $a; |
| 190 | + } |
| 191 | + |
| 192 | + public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested) |
| 193 | + { |
| 194 | + self::addMap($a, $c, array( |
| 195 | + 'version' => 'getVersion', |
| 196 | + 'dependencies' => 'getDependencies', |
| 197 | + 'iniEntries' => 'getIniEntries', |
| 198 | + 'isPersistent' => 'isPersistent', |
| 199 | + 'isTemporary' => 'isTemporary', |
| 200 | + 'constants' => 'getConstants', |
| 201 | + 'functions' => 'getFunctions', |
| 202 | + 'classes' => 'getClasses', |
| 203 | + )); |
| 204 | + |
| 205 | + return $a; |
| 206 | + } |
| 207 | + |
| 208 | + public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested) |
| 209 | + { |
| 210 | + self::addMap($a, $c, array( |
| 211 | + 'version' => 'getVersion', |
| 212 | + 'author' => 'getAuthor', |
| 213 | + 'copyright' => 'getCopyright', |
| 214 | + 'url' => 'getURL', |
| 215 | + )); |
| 216 | + |
| 217 | + return $a; |
| 218 | + } |
| 219 | + |
| 220 | + private static function addExtra(&$a, \Reflector $c) |
| 221 | + { |
| 222 | + $a =& $a[Caster::PREFIX_VIRTUAL.'extra']; |
| 223 | + |
| 224 | + if (method_exists($c, 'getFileName') && $m = $c->getFileName()) { |
| 225 | + $a['file'] = $m; |
| 226 | + $a['line'] = $c->getStartLine().' to '.$c->getEndLine(); |
| 227 | + } |
| 228 | + |
| 229 | + self::addMap($a, $c, self::$extraMap, ''); |
| 230 | + } |
| 231 | + |
| 232 | + private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL) |
| 233 | + { |
| 234 | + foreach ($map as $k => $m) { |
| 235 | + if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) { |
| 236 | + $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m; |
| 237 | + } |
| 238 | + } |
| 239 | + } |
38 | 240 | }
|
0 commit comments