mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 16:45:35 +08:00
abstract reflection utils
This commit is contained in:
@@ -4,12 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace ZM\Container;
|
namespace ZM\Container;
|
||||||
|
|
||||||
use Closure;
|
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use ReflectionFunction;
|
|
||||||
use ReflectionFunctionAbstract;
|
|
||||||
use ReflectionMethod;
|
|
||||||
use ReflectionParameter;
|
use ReflectionParameter;
|
||||||
use ZM\Utils\ReflectionUtil;
|
use ZM\Utils\ReflectionUtil;
|
||||||
|
|
||||||
@@ -34,7 +30,7 @@ class BoundMethod
|
|||||||
$callback = [$callback, $default_method];
|
$callback = [$callback, $default_method];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self::isCallingNonStaticMethod($callback)) {
|
if (ReflectionUtil::isNonStaticMethod($callback)) {
|
||||||
$callback[0] = $container->make($callback[0]);
|
$callback[0] = $container->make($callback[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,21 +41,6 @@ class BoundMethod
|
|||||||
return call_user_func_array($callback, self::getMethodDependencies($container, $callback, $parameters));
|
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.
|
* Get all dependencies for a given method.
|
||||||
*
|
*
|
||||||
@@ -70,33 +51,13 @@ class BoundMethod
|
|||||||
{
|
{
|
||||||
$dependencies = [];
|
$dependencies = [];
|
||||||
|
|
||||||
foreach (static::getCallReflector($callback)->getParameters() as $parameter) {
|
foreach (ReflectionUtil::getCallReflector($callback)->getParameters() as $parameter) {
|
||||||
static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies);
|
static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_merge($dependencies, array_values($parameters));
|
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.
|
* Get the dependency for the given call parameter.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -4,6 +4,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace ZM\Utils;
|
namespace ZM\Utils;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use ReflectionException;
|
||||||
|
use ReflectionFunction;
|
||||||
|
use ReflectionFunctionAbstract;
|
||||||
|
use ReflectionMethod;
|
||||||
use ReflectionNamedType;
|
use ReflectionNamedType;
|
||||||
use ReflectionParameter;
|
use ReflectionParameter;
|
||||||
|
|
||||||
@@ -76,4 +81,43 @@ class ReflectionUtil
|
|||||||
return 'unknown';
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user