dariorieke/callable-resolver

本包最新版本(dev-master)没有提供许可证信息。

一个从PSR-7请求和PSR-11容器中解析调用者和其参数的工具

dev-master 2021-05-12 20:38 UTC

This package is auto-updated.

Last update: 2024-09-13 04:16:41 UTC


README

解析调用者和其参数的工具

安装

通过composer安装

    "require": {
        "dariorieke/callable-resolver": "dev-master"
    }

测试

在存储库根目录下使用以下命令运行测试

./vendor/bin/phpunit  .\tests

用法

解析调用者

要解析抽象调用者到实际调用者,请使用resolve方法。如果调用者解析器无法解析调用者,将抛出CallableResolverExceptionInterface异常。

use DarioRieke\CallableResolver\CallableResolver;
use DarioRieke\CallableResolver\Exception\CallableResolverExceptionInterface;

$callableResolver = new CallableResolver();


$abstractCallable = 'mySpecialFunction';

try {
    $callable = $callableResolver->resolveCallable($abstractCallable);
    call_user_func($callable);
} 
catch (CallableResolverExceptionInterface $e) {
    //could not resolve the callable
}

可解析的可能抽象调用者

//closure
$abtractCallable1 = function() {};
//object without constructor arguments
//the callable resolver will create an instance of it 
$abtractCallable2 = ['SplQueue', 'rewind'];
//initialized object, regular callable
$abtractCallable3 = [new \SplQueue, 'rewind'];
// function name
$abtractCallable4 = 'phpinfo';
//static class method
$abtractCallable5 =  [TestClass::class, 'staticMethod'];
//static class method as string
$abtractCallable6 =  'TestClass::staticMethod';

如果你将PSR-11 Container传递给CallableResolver的构造函数,解析器将尝试从容器中收集任何未初始化的类。如果没有提供容器,类将通过new $class()进行初始化,因此构造函数参数仅在结合依赖注入容器的情况下是可能的。

### 解析参数

> 注意,目前你必须提供一个PSR-7请求实现才能使用ArgumentResolver。

要解析你的调用者的参数,请使用ArgumentResolver。其主要功能是从PSR-11容器中解析依赖注入参数和从PSR-7服务器请求中解析参数。

解析器使用通过构造函数传递的不同参数提供者策略来解析参数。这样,你可以添加自己的参数提供者。

use DarioRieke\CallableResolver\ArgumentResolver;
use DarioRieke\CallableResolver\ArgumentResolver\RequestArgumentProvider;
use DarioRieke\CallableResolver\ArgumentResolver\RequestAttributeArgumentProvider;
use DarioRieke\CallableResolver\ArgumentResolver\ServiceArgumentProvider;


$argumentResolver = new ArgumentResolver (
    //resolves the currently handled PSR-7 request
    new RequestArgumentProvider(),
    //resolves attributes from the PSR-7 request 
    new RequestAttributeArgumentProvider(), 
    //resolves arguments via the provided PSR-11 container 
    new ServiceArgumentProvider(/** PSR-11 implementation*/ $container),
);

参数解析示例

/**
 * type hint the arguments to let the ArgumentRresolver autoresolve them
 * works with RequestInterface and Interfaces and Classes registered in the DependencyInjectionContainer 
 * @param  string                $task    argument from the PSR-7 ServerRequest 
 * @param  int                   $id      argument from the PSR-7 ServerRequest
 * @param  RequestInterface      $request injects the current request
 * @param  LoggerInterface       $logger  a logger implementation resolved by type hint
 *                                        via PSR-11 container
 */
$callable = function (string $task, int $id, Psr\Http\Message\RequestInterface $request, LoggerInterface $logger) {
    // code
}

$args = $argumentResolver->resolveArguments($callable);

call_user_func_array($callable, $args);

DarioRieke\DependencyInjection\DependencyInjectionContainerInterface一起使用

此接口扩展了PSR-11 ContainerInterface。要从它解析参数,请使用ServiceParameterArgumentProvider,并将其构造函数中的DependencyInjectionContainerInterface实现传递给它。

	new ArgumentResolver (
		//...
		new ServiceParameterArgumentProvider($container)
	);

确保你已经安装了"dariorieke/dependency-injection"包才能使用此参数提供者。