ride/lib-dependency

Ride框架的依赖注入库

1.2.0 2024-06-26 08:11 UTC

README

PHP Ride框架的依赖注入库。

此模块可以创建对象并使用动态参数注入调用回调。

维基百科上了解更多关于依赖注入模式的信息。

库中包含的内容

依赖

Dependency类用于定义您的类实例。您可以选择哪些接口类实现,以便依赖注入器知道何时使用此实例。定义方法调用,以便按需构建您的实例,准备就绪。

当您有多个类的实例时,您可以设置依赖项的id以指定每个实例。

依赖项可以标记以检索指定接口的依赖项子集。

除了实际构建对象外,依赖项也可以定义为通过工厂构建。

DependencyInjector

DependencyInjector是这个库的代理。它有不同的获取器来检索单个或多个实例。依赖项通过接口请求,可选的id,或者对于多个实例,标记。

当请求的接口或实例依赖项在容器中未定义时,会尝试自动构建实例。

DependencyContainer

DependencyContainer正如其名,是一个依赖项容器。所有您的定义都保存在这里,以便依赖注入器作为其来源使用。

DependencyArgumentParser

当为依赖项定义方法调用时,您可以向这些调用传递参数。您有不同的参数类型。此库默认定义以下类型:nullscalararraydependencycall

通过实现DependencyArgumentParser接口,您可以创建自己的参数类型。Ride将添加parameterroute类型,与ride/appride/web模块一起使用。

代码示例

查看此代码示例,了解此库的潜力

<?php

use ride\library\dependency\Dependency;
use ride\library\dependency\DependencyCall;
use ride\library\dependency\DependencyCallArgument;
use ride\library\dependency\DependencyContainer;
use ride\library\dependency\DependencyInjector;

// Your dependencies are stored in a dependency container. For the sake of
// explaining this library, let's initialize it manually. This should be done
// through configuration to get real benefit of this library
$dependencyContainer = new DependencyContainer();

// most generic definition of a dependency is a class, however this definition 
// is obsulete since the dependency injector attempts to create undefined 
// dependencies as well
$dependency = new Dependency('some\Class');

// give your dependency an id to retrieve a specific instance of an interface
$dependency = new Dependency('some\Class', 'id1');

// some\Class implements some interfaces
$dependency->addInterface('some\Interface');
$dependency->addInterface('some\OtherInterface');

// now add it to the container
$dependencyContainer->addDependency($dependency);

// lets create another another, this time with a constructor and some action

// define the constructor call
$argument = new DependencyCallArgument('name', 'dependency', array(
    'interface' => 'some\Interface', 
    'id' => 'id1',
));
$call = new DependencyCall('__construct');
$call->addArgument($argument);

// define the dependency and add some calls
$dependency = new Dependency('another\Class', 'id2');
$dependency->addCall($call);
$dependency->addCall(new DependencyCall('doSomething'));
$dependency->addInterface('some\Interface');

// add it to the container
$dependencyContainer->addDependency($dependency);

// define a factory for a dependency
$constructCall = new DependencyConstructCall('some\Factory', 'methodOnFactory');

$dependency = new Dependency($constructCall, 'id');
$dependency->addCall(new DependencyCall('doSomething'));
$dependency->addInterface('some\Interface');

$dependencyContainer->addDependency($dependency);

// Your dependency container gets filled up with this kind of definitions.
// Once setup, you are ready to get your instances.

// First we need to create the dependency injector itself.
$dependencyInjector = new DependencyInjector($dependencyContainer);

// Let's get an instance, the thing you are most likely to do...
$instance = $dependencyInjector->get('some\Interface'); // another\Class since it's last defined
$instance = $dependencyInjector->get('some\Interface', 'id1'); // some\Class
try {
    $instance = $dependencyInjector->get('third\Class');
    // your instance if the third\Class can be created with the available dependencies
} catch (Exception $e) {
    // when the dependency is an interface,
    // or when it's a class and it's not constructable or when some required arguments could
    // not be injected.
}

// You can also invoke callbacks where not provided arguments are injected if possible
$callback = 'function';
$callback = array('some\Static', 'call');
$callback = array(new some\Class(), 'call');
$arguments = array('name' => $value); // arguments you know/want, the rest will be injected
$returnValue = $dependencyInjector->invoke($callback, $arguments);

安装

您可以使用Composer安装此库。

composer require ride/lib-dependency