承接 tobento/service-autowire 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tobento/service-autowire

最新稳定版本:2.0.1

Composer 安装命令:

composer require tobento/service-autowire

包简介

Autowiring for PSR-11 containers.

README 文档

README

The Autowire Service provides autowiring for PSR-11 containers.

Table of Contents

Getting started

Add the latest version of the autowire service running this command.

composer require tobento/service-autowire

Requirements

  • PHP 8.4 or greater

Highlights

  • Framework-agnostic, will work with any project

Simple Example

Here is a simple example of how to use the Autowire service.

use Tobento\Service\Autowire\Autowire;

// Autowiring an object
$foo = new Autowire($container)->resolve(Foo::class);

// Call method using autowiring
$value = new Autowire($container)->call([Foo::class, 'method']);

Documentation

Resolve

Define any build-in parameters which are not resolvable, either by parameter name or position.

use Tobento\Service\Autowire\Autowire;

// By name
$foo = new Autowire($container)->resolve(Foo::class, ['name' => 'value']);

// By position
$foo = new Autowire($container)->resolve(Foo::class, [2 => 'value']);

You might use a try/catch block:

use Tobento\Service\Autowire\Autowire;
use Tobento\Service\Autowire\AutowireException;

try {
    $foo = new Autowire($container)->resolve([Foo::class, 'method']);
} catch (AutowireException $e) {
    // not resolvable
}

Call

Define any build-in parameters which are not resolvable, either by parameter name or position.

use Tobento\Service\Autowire\Autowire;

// Using array callable
$value = new Autowire($container)->call([Foo::class, 'method'], ['name' => 'value']);

// Using closure
$value = new Autowire($container)->call(function(Foo $foo, $name) {
    return $name;
}, ['name' => 'value']);

var_dump($value); // string(5) "value"

// Using class with __invoke
$value = new Autowire($container)->call(Invokable::class, ['name' => 'value']);

// Using Class::method syntax
$value = new Autowire($container)->call('Foo::method', ['name' => 'value']);

Credits

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-17