PHPackages                             beige/invoker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. beige/invoker

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

beige/invoker
=============

A component for injection and execution.

2.0.0(6y ago)019MITPHPPHP &gt;=7.1.0

Since Aug 14Pushed 6y ago1 watchersCompare

[ Source](https://github.com/speed-sonic/beige-invoker)[ Packagist](https://packagist.org/packages/beige/invoker)[ RSS](/packages/beige-invoker/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

Beige Invoker
=============

[](#beige-invoker)

[![GitHub license](https://camo.githubusercontent.com/d8699554f7186f5447b96dfee3e972a31d2c7143a944fa0ad785407cd64a70a4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f616c69656e776f772f536e6f774c656f706172642e737667)](https://github.com/alienwow/SnowLeopard/blob/master/LICENSE)[![LICENSE](https://camo.githubusercontent.com/be80b8cb211ceb2263744e99fdb161a40124901906fd7c7f47d6361760dd7e8b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d416e74692532303939362d626c75652e737667)](https://github.com/996icu/996.ICU/blob/master/LICENSE)[![Coverage 100%](https://camo.githubusercontent.com/964e6727b820bf4bd13789e84922b6359a9493323aa7d455ec675f1cf403209c/68747470733a2f2f696d672e736869656c64732e696f2f617a7572652d6465766f70732f636f7665726167652f7377656c6c6162792f6f70656e736f757263652f32352e737667)](https://github.com/speed-sonic/beige-route)

基于容器技术的轻量级调用器
-------------

[](#基于容器技术的轻量级调用器)

A lightweight invoker based on container.

简介
--

[](#简介)

invoker 负责调用或实例化用户指定程序，并且利用 PSR-11 容器标准获取程序的依赖参数的实例。它是一个高效的，不关心参数顺序的依赖注入工具，可以给与你最大限度的灵活度和自由度。

安装
--

[](#安装)

```
composer require beige/invoker

```

使用
--

[](#使用)

`Beige\Invoker\Invoker` 继承于 `Beige\Invoker\Interfaces\InvokerInterface`，实现了三个标准方法：

### `Beige\Invoker\Invoker::call(callable $function[, array $parameters])`: mixed:

[](#beigeinvokerinvokercallcallable-function-array-parameters-mixed)

调用一个 `callable` 对象或函数，并注入依赖项到`callable` 对象或函数：

参数：

- `$function`: 可调用对象或函数
- `$parameters`: 如果 `$function` 对象除了依赖参数外，还有额外的参数，则可以通过这个参数以数组的形式传入。i.e. 如果参数名称为 `$foo` 则 `$parameters` 为 `['foo' => 'bar']`.

返回值：

- 返回 `$function` 的返回值

```
use Beige\Invoker\Invoker;
use Beige\Psr11\Container;

$container = new Container([
    'Test' => ...
]);

$invoker = new Invoker($container);
$invoker->call(function(Test $test, $foo) {
    ...
}, ['foo' => 'bar']);
```

上面的例子简化了具体的代码实现，首先，`Beige\Invoker\Invoker` 的构造方法接受一个 `Psr\Container\ContainerInterface` 容器实例，我们需要将 `Test` 作为容器项索引提前实例放进容器中，`$function` 通过声明参数类型 `Test` 来获取对应的实例，这个 Test 的实例就是从容器中获取到的。

上例中的 `$function` 函数中还包含一个额外的参数 `$foo`，我们通过 Invoker 第二参数 `$parameters` 传入。`$parameters` 是一个数组，它的键必须与需要绑定的参数名称相同。实际上，`$function` 的参数不需要遵循任何顺序的束缚。

这些规律也适用于接下来的所有调用器方法。

### `Beige\Invoker\Invoker::new(string $className[, array $parameters])`: object:

[](#beigeinvokerinvokernewstring-classname-array-parameters-object)

实例化一个 php 类，并注入依赖项到类的构造方法。 参数：

- `$className`: 类的名称
- `$parameters`: 如果构造方法除了依赖参数外，还有额外的参数，则可以通过这个参数以数组的形式传入。i.e. 如果参数名称为 `$foo` 则 `$parameters` 为 `['foo' => 'bar']`.

返回值：

- 返回 `$className` 的实例

```
use Beige\Invoker\Invoker;

class Invokeable
{
    public function __construct(Test $test, $foo)
    {
        ...
    }
    ...
}

$instance = $invoker->new(Invokeable::class, ['foo' => 'bar']);
```

上例与 `Invoker::call` 非常相似，只是由函数调用变成了对类的实例化操作。我们调用 `Invoker::new` 方法，从容器中取出 Test 类型的实例，并绑定到构造方法的 `$test` 参数上，并用第二个参数注入了自定义参数 `$foo`，最终得到 `Invokeable` 的实例。

### `Beige\Invoker\Invoker::callMethod(object $instance, string $method[, array $parameters])`: mixed:

[](#beigeinvokerinvokercallmethodobject-instance-string-method-array-parameters-mixed)

调用一个对象的方法，并注入依赖项到这个方法中。

参数：

- `$instance`: 需要调用的方法所在的对象
- `$method`: 需要调用的方法名称
- `$parameters`: 如果需要调用的方法除了依赖参数外，还有额外的参数，则可以通过这个参数以数组的形式传入。i.e. 如果参数名称为 `$foo` 则 `$parameters` 为 `['foo' => 'bar']`.

返回值： 返回方法的返回值

```
$instance = new Invokeable();

$invoker->callMethod($instance, 'someMethod', ['foo' => 'bar']);
```

与 `Invoker::new` 很相似，`Invoker::callMethod` 也会读取类型声明注入参数。

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Total

3

Last Release

2445d ago

Major Versions

1.0.1 → 2.0.02019-09-04

### Community

Maintainers

![](https://www.gravatar.com/avatar/9f4cfe2ff9c9a5fe469dd2701b7c22132e5b5a9e053a32c54d7ae90cfe565ef3?d=identicon)[Alex Layton](/maintainers/Alex%20Layton)

---

Top Contributors

[![alex-1900](https://avatars.githubusercontent.com/u/49949411?v=4)](https://github.com/alex-1900 "alex-1900 (6 commits)")

---

Tags

dependencyinjectioninvokerdiiocservice

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/beige-invoker/health.svg)

```
[![Health](https://phpackages.com/badges/beige-invoker/health.svg)](https://phpackages.com/packages/beige-invoker)
```

###  Alternatives

[league/container

A fast and intuitive dependency injection container.

86387.8M343](/packages/league-container)[php-di/invoker

Generic and extensible callable invoker

26857.8M56](/packages/php-di-invoker)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2857.5k2](/packages/capsule-di)[marcelog/ding

PHP Dependency Injection based on Spring(tm), with Aspect Oriented Programming, MVC

1202.1k](/packages/marcelog-ding)[miladrahimi/phpcontainer

Dependency injection (IoC) container for PHP projects

1322.7k2](/packages/miladrahimi-phpcontainer)[infocyph/intermix

A Collection of useful PHP class functions.

136.4k1](/packages/infocyph-intermix)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
