krak/auto-args

自动参数解析器

v0.3.1 2017-03-25 02:34 UTC

This package is auto-updated.

Last update: 2024-09-18 17:37:47 UTC


README

Auto Args 提供了一个自动解析任何可调用参数的系统。这也可以称为自动绑定。

安装

使用 composer 在 krak/auto-args 中安装。

用法

<?php

use Krak\AutoArgs;

$args = new AutoArgs();

$context = [
    'vars' => ['a' => 1],
    'objects' => [new SplStack()],
];

$func = function($a, SplDoublyLinkedList $stack, $b = 1) {
    assert($a == 1 && $b === 1);
};

$args->invoke($func, $context);

容器集成

<?php

use Krak\AutoArgs,
    Krak\Cargo,
    Interop\Container\ContainerInterface;

$args = new AutoArgs();
$c = Cargo\container();
$c[SplStack::class] = function() {
    return new SplStack();
};

$context = [
    'container' => $c->toInterop()
];

$func = function(ContainerInterface $container, SplStack $stack) {

};

$args->invoke($func, $context);

API

类 AutoArgs

__construct($resolve_arg = null)

接受一个参数解析器,该解析器将接受参数元数据和上下文,并返回适当的参数。如果没有提供,将创建并组成默认堆栈。

mixed invoke(callable $callable, array $context)

调用可调用项,并从参数解析器和给定上下文中解析参数。

mixed construct($class_name, array $context)

从类名创建对象并解析构造函数的参数

array resolveArguments(callable $callable, array $context)

返回给定可调用项解析参数的数组。如果没有解析到任何参数,将抛出异常。

Krak\Mw\MwStack ::createStack()

返回一个配置好的中间件堆栈实例。