abstract reflection utils

This commit is contained in:
sunxyw 2022-04-12 17:52:51 +08:00
parent 33f517333f
commit 48f6cc644d
No known key found for this signature in database
GPG Key ID: CEA01A083E98C578
2 changed files with 46 additions and 41 deletions

View File

@ -4,12 +4,8 @@ declare(strict_types=1);
namespace ZM\Container;
use Closure;
use InvalidArgumentException;
use ReflectionException;
use ReflectionFunction;
use ReflectionFunctionAbstract;
use ReflectionMethod;
use ReflectionParameter;
use ZM\Utils\ReflectionUtil;
@ -34,7 +30,7 @@ class BoundMethod
$callback = [$callback, $default_method];
}
if (self::isCallingNonStaticMethod($callback)) {
if (ReflectionUtil::isNonStaticMethod($callback)) {
$callback[0] = $container->make($callback[0]);
}
@ -45,21 +41,6 @@ class BoundMethod
return call_user_func_array($callback, self::getMethodDependencies($container, $callback, $parameters));
}
/**
* 判断调用的是否为非静态方法
*
* @param array|string $callback
* @throws ReflectionException
*/
protected static function isCallingNonStaticMethod($callback): bool
{
if (is_array($callback) && is_string($callback[0])) {
$reflection = new ReflectionMethod($callback[0], $callback[1]);
return !$reflection->isStatic();
}
return false;
}
/**
* Get all dependencies for a given method.
*
@ -70,33 +51,13 @@ class BoundMethod
{
$dependencies = [];
foreach (static::getCallReflector($callback)->getParameters() as $parameter) {
foreach (ReflectionUtil::getCallReflector($callback)->getParameters() as $parameter) {
static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies);
}
return array_merge($dependencies, array_values($parameters));
}
/**
* Get the proper reflection instance for the given callback.
*
* @param callable|string $callback
* @throws \ReflectionException
* @return ReflectionFunctionAbstract
*/
protected static function getCallReflector($callback)
{
if (is_string($callback) && str_contains($callback, '::')) {
$callback = explode('::', $callback);
} elseif (is_object($callback) && !$callback instanceof Closure) {
$callback = [$callback, '__invoke'];
}
return is_array($callback)
? new ReflectionMethod($callback[0], $callback[1])
: new ReflectionFunction($callback);
}
/**
* Get the dependency for the given call parameter.
*

View File

@ -4,6 +4,11 @@ declare(strict_types=1);
namespace ZM\Utils;
use Closure;
use ReflectionException;
use ReflectionFunction;
use ReflectionFunctionAbstract;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
@ -76,4 +81,43 @@ class ReflectionUtil
return 'unknown';
}
}
/**
* 判断传入的回调是否为任意类的非静态方法
*
* @param callable|string $callback 回调
* @throws ReflectionException
*/
public static function isNonStaticMethod($callback): bool
{
if (is_array($callback) && is_string($callback[0])) {
$reflection = new ReflectionMethod($callback[0], $callback[1]);
return !$reflection->isStatic();
}
return false;
}
/**
* 获取传入的回调的反射实例
*
* 如果传入的是类方法,则会返回 {@link ReflectionMethod} 实例
* 否则将返回 {@link ReflectionFunction} 实例
*
* 可传入实现了 __invoke 的类
*
* @param callable|string $callback 回调
* @throws ReflectionException
*/
public static function getCallReflector($callback): ReflectionFunctionAbstract
{
if (is_string($callback) && str_contains($callback, '::')) {
$callback = explode('::', $callback);
} elseif (is_object($callback) && !$callback instanceof Closure) {
$callback = [$callback, '__invoke'];
}
return is_array($callback)
? new ReflectionMethod($callback[0], $callback[1])
: new ReflectionFunction($callback);
}
}