infuse-di/infuse

符合PSR-11规范的依赖注入库

1.0.0 2024-06-27 02:54 UTC

This package is auto-updated.

Last update: 2024-09-27 13:33:33 UTC


README

Infuse

CI Packagist PHP License

Infuse 🍃

最小化的PSR-11实现。

特性

  • ✔️ 自动装配(由反射驱动)
  • ✔️ 简单的API(仅3个方法)
  • ✔️ 容器可以从定义构建
  • ✔️ 单例模式
  • ✔️ 检测循环依赖
  • ⏳ 可用于生产的编译
  • ⏳ 支持PHPStan泛型的容器绑定

文档

Infuse具有一个非常简单的API,只有三个方法

/**
 * Finds an entry of the container by its identifier and returns it.
 *
 * @param string $id identifier of the entry to look for
 *
 * @return mixed entry
 *
 * @throws NotFoundExceptionInterface  no entry was found for the provided identifier
 * @throws ContainerExceptionInterface error while retrieving the entry
 */
public function get(string $id): mixed;

/**
 * Returns true if the container can return an entry for the given identifier.
 * Returns false otherwise.
 *
 * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
 * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
 *
 * @param string $id identifier of the entry to look for
 */
public function has(string $id): bool;

/**
 * @param \Closure(Container): mixed $definition
 *
 * @throws ContainerExceptionInterface if provided id is not unique
 */
public function bind(string $id, \Closure $definition): void;

示例

use Infuse\Container;

$container = new Container();

// Container::bind tells the Container how to build a Bar object
$container->bind(Bar::class, function () {
    return new Bar();
});

// Container::has checks if there's a binding with the provided id
$container->has(Bar::class); // true

// the callable parameter receives the Container itself as argument
$container->bind(Foo::class, function (Container $c) {
    $bar = $c->get(Bar::class);
    return new Foo($bar):
});

// Container::get retrieves an instance from the Container, the bound callable will be called at this moment
$foo = $container->get(Foo::class);
$isFoo = $foo instanceof Foo; // true

// Every instance is a singleton, you'll always receive the same reference
$sameFoo = $container->get(Foo::class);
$sameInstance = $foo === $sameFoo; // true

// This will throw a ContainerException, ids must be unique
$container->bind(Bar::class, function (Container $c) {
    return new Bar();
});

// This will throw a NotFoundException, this id has not been bound
$container->get(Zee::class);

// You can bind basically anything
$container->bind('some_array', fn () => ['hello' => 'world']);
$container->bind('some_scalar', fn () => 42);

您还可以从定义数组创建一个可用的容器

// definitions.php
<?php

use Infuse\Container;

// should be shaped as array<string, callable>

return [
    GeoLocationService::class => function (Container $c) {
        $config = $c->get('config');
        return new GeoLocationService($config['GEOLOCATION_API_KEY']);
    },
    'config' => function () {
        return [
            'GEOLOCATION_API_KEY' => $_ENV['GEOLOCATION_API_KEY'],
        ];
    },
];
// something.php
use Infuse\ContainerFactory;

$definitions = require __DIR__ . '/definitions.php';
$container = ContainerFactory::FromDefinitions($definitions);
$container->has('config'); // true
$container->has(GeoLocationService::class); // true

安装

推荐通过 Composer 安装Infuse。

composer require infuse-di/infuse

测试

要执行测试套件,您需要安装所有开发依赖。

git clone https://github.com/davisenra/infuse
composer install
composer test

许可证

本项目采用MIT许可证。更多信息请参阅 许可证文件