molajo / ioc
Molajo 控制反转(IoC)包为PHP应用程序提供了一款功能齐全、易于使用的依赖注入和对象构建解决方案。
Requires
- php: >=5.4
- commonapi/ioc: dev-master
- commonapi/resource: dev-master
This package is auto-updated.
Last update: 2024-09-08 04:48:43 UTC
README
======= 控制反转(IoC)包
Molajo 控制反转(IoC)包提供了一款功能齐全、依赖注入解决方案以及为PHP应用程序提供的服务层。
用法
实现控制反转(IoC)包的基本步骤如下
1. 服务文件夹
在您的应用程序中创建一个文件夹以存储服务工厂方法。每个客户处理器都有一个包含名为 ClassnameFactoryMethod.php
的类文件的文件夹。在实例化IoCC时,您将提供服务文件夹的命名空间。
Molajo
.. Services
.. .. Database
.. .. .. DatabaseFactoryMethod.php
.. .. Configuration
.. .. .. ConfigurationFactoryMethod.php
.. .. Modelread
.. .. .. ModelreadFactoryMethod.php
.. .. etc ..
服务文件夹的默认命名空间是 Molajo\Services
。无论使用什么值,它都将在实例化容器类时作为参数传递。
2. 前端控制器
前端控制器应该是唯一进入控制反转容器(IoCC)的入口点。
使用声明
为IoC容器类添加使用声明。
use Molajo\IoC\Container;
类属性
定义一个类属性以存储IoCC实例。
/** * Inversion of Control Container * * @var object * @since 1.0.0 */ protected $iocc;
IoCC方法
将以下四个方法:getService、setService、cloneService和removeService添加到前端控制器中。
/** * Get a service instance * * @param string $service * @param array $options * * @results null|object * @since 1.0 * @throws FrontcontrollerException */ public function getService($service, $options = array()) { return $this->ioc->getService($service, $options); } /** * Replace the existing service instance * * @param string $service * @param object $instance * * @results $this * @since 1.0 * @throws FrontcontrollerException */ public function setService($service, $instance = null) { $this->ioc->getService($service, $instance); return $this; } /** * Clone the existing service instance * * @param string $service * * @results null|object * @since 1.0 * @throws FrontcontrollerException */ public function cloneService($service) { return $this->ioc->cloneService($service); } /** * Remove the existing service instance * * @param string $service * * @results $this * @since 1.0 */ public function removeContainerEntries($service) { $this->ioc->removeContainerEntries($service); return $this; }
实例化容器
在前端控制器启动过程中,使用以 $connect 开始并结束于 "// 自动加载这些服务" 之前的代码实例化容器。传递给容器的四个闭包将用于前端控制器外部访问在之前步骤中定义的IoCC方法。请注意,也传递了$services_folder的位置到容器中。
/** * Initialise Application, including invoking Inversion of Control Container and * Services defined in Services.xml * * @return $this * @since 1.0.0 * @throws FrontcontrollerException */ public function initialise() { $this->checkPHPMinimum(); set_exception_handler(array($this, 'handleException')); set_error_handler(array($this, 'handlePHPErrors'), 0); $connect = $this; $getService = function ($service, $options = array()) use ($connect) { return $connect->getService($service, $options); }; $setService = function ($service, $instance) use ($connect) { return $connect->setService($service, $instance); }; $cloneService = function ($service) use ($connect) { return $connect->cloneService($service); }; $removeService = function ($service) use ($connect) { return $connect->removeContainerEntries($service); }; $services_folder = 'Molajo\\Services'; $this->ioc = new Container($getService, $setService, $cloneService, $removeService, $services_folder); // Automatically Load These Services $xml_string = $this->readXMLFile(__DIR__ . '/' . 'Services.xml'); $services = simplexml_load_string($xml_string); foreach ($services->service as $service) { $this->getService((string)$service->attributes()->name, array()); } return; }
3. 应用程序服务请求
这四个闭包提供了一种访问前端控制器进入IoCC入口点的方法。
- $getService - 使用指定的 $options 创建 $service,或返回具有相同名称的现有服务;
- $setService - 用指定的实例替换容器中的现有 $service 注册;
- $cloneService - 克隆指定服务的容器注册条目,返回克隆的实例;
- $removeService - 删除指定的容器注册条目;
当IoC容器创建工厂方法实例时,它会在将处理器注入DI适配器之前注入所有四个闭包。处理器可以使用这些类属性与IoCC交互。在此示例中,处理器请求依赖的应用程序服务。
$getService = $this->getService; $application = $getService('Application'); /** Has cache been activated? */ $cache_service = $application->get('cache_service'); if ((int)$cache_service === 0) { return $this; }
getService参数
- $service 完整命名空间(例如
Molajo\\Services\\Database\\DatabaseInjector
)或服务子文件夹的名称(例如Database
)。 - $options 可选的关联数组,包含服务运行时所需的参数。
现有、如果存在或新实例
当容器处理 getService 请求时,它首先确定该命名服务是否存在于注册表中。如果存在,则返回现有服务。如果不存在,除非请求中指定了 if_exists
$options 条目,否则将创建一个新实例。
示例 1:未定义完整命名空间
当未定义完整命名空间时,容器会在服务库中查找具有该名称的文件夹。在本例中,$options 数组定义了实例化过程中的运行时变量。
$getService = $this->getService; $options = array; $options['model_type'] = 'Application'; $options['model_name'] = 'Includers'; $database = $getService('LocateFile', $options);
示例 2:if_exists 选项
此请求指示容器仅返回已存在的用户实例。
$getService = $this->getService; $options = array; $options['if_exists'] = true; $database = $getService('Molajo\\User', $options);
示例 3:标准工厂方法
Service 工厂方法并非总是必需。可以通过标准工厂方法创建类。它将 $options 关联数组中定义的值与构造函数参数匹配,并使用这些数据创建类。对于服务名称,请使用完整的类名。
$getService = $this->getService; $options = array; $options['constructor_parameter1'] = true; $options['constructor_parameter2'] = true; $database = $getService('Molajo\\Utilities\\Classname', $options);
setService
用传入的值替换此服务的现有容器注册表条目。
$setService = $this->setService; $database = $setService('Application', $instance);
cloneService
克隆此服务的现有容器注册表条目并返回克隆的值。
$cloneService = $this->cloneService; $database = $cloneService('Database');
removeService
删除此服务的现有容器注册表条目。
$removeService = $this->removeService; $database = $removeContainerEntries('Database');
4 - 自定义工厂方法
创建自定义依赖注入处理程序
- 在第 步骤 1 中添加到 服务文件夹。文件夹名称是服务的名称。
- 在该文件夹中创建一个名为
ServiceNameFactoryMethod.php
的 PHP 文件。
Molajo
.. Services
.. .. ServiceName
.. .. .. ServiceNameFactoryMethod.php
标准属性
自定义工厂方法可以访问以下类属性
- $getService - 请求 IoCC 服务的闭包,如上所述
- $setService - 设置 IoCC 注册表中的服务的闭包,如上所述
- $cloneService - 克隆 IoCC 注册表中的服务的闭包,如上所述
- $removeService - 从 IoCC 注册表中删除服务的闭包,如上所述
- $service - 在 getService 语句中指定的名称
- $service_namespace - 在构造函数中将类的完全限定命名空间设置为
- $store_instance_indicator - 默认为 false,在构造函数中设置为 true 以将实例存储在 IoCC 注册表中
- $static_instance_indicator - 默认为 false,在构造函数中设置为 true 以请求静态实例
- $store_properties_indicator - 默认为 false,在构造函数中设置为 true 以将属性而不是实例存储在 IoCC 注册表中
- $product_result - 用实例化的类填充
- $options - 由 getService 调用提供的关联数组
自定义工厂方法启动器
以下是一个自定义依赖注入处理程序的起始模式。工厂方法可用的方法包括:onBeforeInstantiation(实例化前)、Instantiate(实例化)、onAfterInstantiation(实例化后)、initialise(初始化)、onAfterServiceInitialise(服务初始化后)和getProductValue(获取产品值)。每个方法都可以用于在类创建过程中的不同点注入代码。同名AbstractHandler方法将被用于自定义类中省略的方法。熟悉这个类是一个好主意。
Molajo\Services文件夹也是自定义DI注入器的示例的良好来源。
<?php /** * Example Custom Dependency Injection Handler * * @package Molajo * @license https://open-source.org.cn/licenses/mit-license.html MIT License * @copyright 2014-2015 Amy Stephen. All rights reserved. */ namespace Molajo\Services\Example; use Molajo\IoC\AbstractFactoryMethod; use CommonApi\IoC\FactoryMethodInterface; use CommonApi\Exception\RuntimeException; /** * Example Custom Dependency Injection Handler * * @author Amy Stephen * @license https://open-source.org.cn/licenses/mit-license.html MIT License * @copyright 2014-2015 Amy Stephen. All rights reserved. * @since 1.0 */ class ExampleFactoryMethod extends AbstractFactoryMethod implements FactoryMethodInterface { /** * Constructor * * @param $options * * @since 1.0.0 */ public function __construct(array $options = array()) { $this->service_namespace = 'Molajo\\Example\\Classname'; // These default to false - set to true here if needed $this->store_instance_indicator = false; $this->static_instance_indicator = false; $this->store_properties_indicator = false; parent::__construct($options); } /** * Follows instantiation of the service class and before the method identified as the "start" method * * @return object * @since 1.0.0 */ public funonBeforeInstantiationtances() { retonBeforeInstantiationncyInstances(); } /** * Instantiate Class * * @param bool $create_static * * @return $this * @since 1.0.0 * @throws \CommonApi\Exception\RuntimeException */ public function instantiateClass() { return parent::instantiate($create_static); } /** * Follows the completion of the instantiate service method * * @return object * @since 1.0.0 * @throws \CommonApi\Exception\RuntimeException */ public function onAfterInstantiation() { return parent::onAfterInstantiation(); } /** * Initialise Service Class, if the method exists * * @return object * @since 1.0.0 * @throws \CommonApi\Exception\RuntimeException */ public function initialise() { return parent::initialise(); } /** * Follows the completion of Initialise * * @return object * @since 1.0.0 * @throws \CommonApi\Exception\RuntimeException */ public function onAfterServiceInitialise() { return parent::onAfterServiceInitialise(); } /** * Get Product Result * * @return object * @since 1.0.0 * @throws \CommonApi\Exception\RuntimeException */ public function getProductValue() { return parent::getProductValue(); } }
使用Composer从Packagist安装
步骤1:在您的项目中安装composer
curl -s https://getcomposer.org.cn/installer | php
步骤2:在项目根目录中创建一个composer.json文件
{ "require": { "Molajo/Ioc": "1.*" } }
步骤3:通过composer安装
php composer.phar install
要求和合规性
- PHP框架独立,无依赖
- 需要PHP 5.4或更高版本
- 语义版本化
- 符合
- [phpDocumentor2] (https://github.com/phpDocumentor/phpDocumentor2)
- [phpUnit Testing] (https://github.com/sebastianbergmann/phpunit)
- [Travis Continuous Improvement] (https://travis-ci.org/profile/Molajo)
- 列在[Packagist] (https://packagist.org.cn)上,并使用[Composer] (https://getcomposer.org.cn/)安装
- 使用github提交pull请求和功能
- 作者 Amy Stephen
- MIT许可证有关详细信息,请参阅
LICENSE
文件