定制 dantleech/argument-resolver 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dantleech/argument-resolver

最新稳定版本:1.1.0

Composer 安装命令:

composer require dantleech/argument-resolver

包简介

Resolve method arguments from an associative array

README 文档

README

Resolve and validate method arguments from an associative array.

  • Resolves arguments including default values from an associative array.
  • Throws exception if required arguments are missing.
  • Throws exception if extra arguments are provided.

Example

class Foobar
{
    public function execute(string $foobar, $barfoo = 'foobar')
    {
    }
}

$argumentResolver = new ArgumentResolver();
$arguments = $argumentResolver->resolveArguments(Foobar::class, 'execute', [
    'foobar' => 'hello'
]);

var_dump($arguments);
// array(2) {                       
//   [0] =>                         
//   string(5) "hello"              
//   [1] =>                         
//   string(6) "foobar"             
// }

$result = call_user_func([$myClass, 'myMethod'], $arguments);

Motivation

It is often desired that service have some runtime invocation configuration which is provided by the user (similar to Symfony Forms and the Symfony OptionResolver component, which does something like:

$resolver = new OptionsResolver();
$resolver->setDefault([
    'barfoo' => 'hello',
]);
$resolver->setRequired('foobar')l
$options = $resolver->resolve(['foobar' => 'hello']);

$myService->doSomething($options);

I have used this pattern in PHPBench and found it very useful for configuring services invocations at runtime. However it has drawbacks:

  • Your service is passed an untyped array.
  • The service has an implicit dependency on the options resolver.

This package does something similar to the options resolver, but uses the reflection information from the method to resolve the arguments.

Drawbacks

This method does mean that you cannot implement a interface for your service (e.g. execute($blah, array $config) as each service will have it's own signature execute($blah, $timeout = 1234, $iterations = 33).

Similar Libraries

https://github.com/sroze/ArgumentResolver does almost exactly the same thing

统计信息

  • 总下载量: 444.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固