| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use ReflectionAttribute; |
| 6 |
use ReflectionClass; |
| 7 |
use ReflectionEnum; |
| 8 |
use ReflectionMethod; |
| 9 |
use ReflectionNamedType; |
| 10 |
use ReflectionUnionType; |
| 11 |
|
| 12 |
class Reflector |
| 13 |
{ |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public static function isCallable($var, $syntaxOnly = false) |
| 22 |
{ |
| 23 |
if (! is_array($var)) { |
| 24 |
return is_callable($var, $syntaxOnly); |
| 25 |
} |
| 26 |
|
| 27 |
if (! isset($var[0], $var[1]) || ! is_string($var[1] ?? null)) { |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
if ($syntaxOnly && |
| 32 |
(is_string($var[0]) || is_object($var[0])) && |
| 33 |
is_string($var[1])) { |
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
$class = is_object($var[0]) ? get_class($var[0]) : $var[0]; |
| 38 |
|
| 39 |
$method = $var[1]; |
| 40 |
|
| 41 |
if (! class_exists($class)) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
if (method_exists($class, $method)) { |
| 46 |
return (new ReflectionMethod($class, $method))->isPublic(); |
| 47 |
} |
| 48 |
|
| 49 |
if (is_object($var[0]) && method_exists($class, '__call')) { |
| 50 |
return (new ReflectionMethod($class, '__call'))->isPublic(); |
| 51 |
} |
| 52 |
|
| 53 |
if (! is_object($var[0]) && method_exists($class, '__callStatic')) { |
| 54 |
return (new ReflectionMethod($class, '__callStatic'))->isPublic(); |
| 55 |
} |
| 56 |
|
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static function getClassAttribute($objectOrClass, $attribute, $ascend = false) |
| 71 |
{ |
| 72 |
return static::getClassAttributes($objectOrClass, $attribute, $ascend)->flatten()->first(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static function getClassAttributes($objectOrClass, $attribute, $includeParents = false) |
| 87 |
{ |
| 88 |
$reflectionClass = new ReflectionClass($objectOrClass); |
| 89 |
|
| 90 |
$attributes = []; |
| 91 |
|
| 92 |
do { |
| 93 |
$attributes[$reflectionClass->name] = new Collection(array_map( |
| 94 |
fn (ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance(), |
| 95 |
$reflectionClass->getAttributes($attribute) |
| 96 |
)); |
| 97 |
} while ($includeParents && false !== $reflectionClass = $reflectionClass->getParentClass()); |
| 98 |
|
| 99 |
return $includeParents ? new Collection($attributes) : array_first($attributes); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static function getParameterClassName($parameter) |
| 109 |
{ |
| 110 |
$type = $parameter->getType(); |
| 111 |
|
| 112 |
if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
return static::getTypeName($parameter, $type); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public static function getParameterClassNames($parameter) |
| 126 |
{ |
| 127 |
$type = $parameter->getType(); |
| 128 |
|
| 129 |
if (! $type instanceof ReflectionUnionType) { |
| 130 |
return array_filter([static::getParameterClassName($parameter)]); |
| 131 |
} |
| 132 |
|
| 133 |
$unionTypes = []; |
| 134 |
|
| 135 |
foreach ($type->getTypes() as $listedType) { |
| 136 |
if (! $listedType instanceof ReflectionNamedType || $listedType->isBuiltin()) { |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
$unionTypes[] = static::getTypeName($parameter, $listedType); |
| 141 |
} |
| 142 |
|
| 143 |
return array_filter($unionTypes); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
protected static function getTypeName($parameter, $type) |
| 154 |
{ |
| 155 |
$name = $type->getName(); |
| 156 |
|
| 157 |
if (! is_null($class = $parameter->getDeclaringClass())) { |
| 158 |
if ($name === 'self') { |
| 159 |
return $class->getName(); |
| 160 |
} |
| 161 |
|
| 162 |
if ($name === 'parent' && $parent = $class->getParentClass()) { |
| 163 |
return $parent->getName(); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return $name; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public static function isParameterSubclassOf($parameter, $className) |
| 178 |
{ |
| 179 |
$paramClassName = static::getParameterClassName($parameter); |
| 180 |
|
| 181 |
return $paramClassName |
| 182 |
&& (class_exists($paramClassName) || interface_exists($paramClassName)) |
| 183 |
&& (new ReflectionClass($paramClassName))->isSubclassOf($className); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public static function isParameterBackedEnumWithStringBackingType($parameter) |
| 193 |
{ |
| 194 |
if (! $parameter->getType() instanceof ReflectionNamedType) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
$backedEnumClass = $parameter->getType()?->getName(); |
| 199 |
|
| 200 |
if (is_null($backedEnumClass)) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
if (enum_exists($backedEnumClass)) { |
| 205 |
$reflectionBackedEnum = new ReflectionEnum($backedEnumClass); |
| 206 |
|
| 207 |
return $reflectionBackedEnum->isBacked() |
| 208 |
&& $reflectionBackedEnum->getBackingType()->getName() == 'string'; |
| 209 |
} |
| 210 |
|
| 211 |
return false; |
| 212 |
} |
| 213 |
} |