翻译 · 2015年10月30日 0

Class Phalcon\Di

[highlight]由于官方部分没有翻译,看起来很不方便,所以我花了几个小时做出这个中文版(类方法部分有英文对照),非专业翻译,部分翻译或有偏差,如有发现欢迎指正 :)[/highlight]

implements Phalcon\DiInterface, ArrayAccess

Phalcon\Di 是一个实现了服务的依赖 注入/业务定位(Service Location) 的组件,并且它本身也是一个这样的容器。由于Phalcon 高度解耦,Phalcon\Di是框架中必不可少的组件。开发者同样可以使用这个组件来注入依赖关系,并且管理程序中使用的不同的类的全局实例。这个组件基本上实现了 控制反转 模式。对象使用setter 或构造函数 不接收它的依赖项,而请求服务的依赖注入。这减少整体的复杂性,因此组件内只有一种方式来获取必要的依赖项。此外,这种模式增加了代码的可预测性,因此使得它不易出错。

<?php

 $di = new \Phalcon\Di();

 //Using a string definition
 $di->set("request", "Phalcon\Http\Request", true);

 //Using an anonymous function
 $di->set("request", function(){

  return new \Phalcon\Http\Request();

 }, true);

 $request = $di->getRequest();

方法

public __construct ()

Phalcon\Di 的构造函数 | Phalcon\Di constructor

public setInternalEventsManager (unknown $eventsManager)

设置内部事件管理器 | Sets the internal event manager

public getInternalEventsManager ()

返回内部事件管理器 | Returns the internal event manager

public set (unknown $name, unknown $definition, [unknown $shared])

注册一个服务在服务容器中 | Registers a service in the services container

public setShared (unknown $name, unknown $definition)

注册一个“长期共享”服务在服务容器中 | Registers an “always shared” service in the services container

public remove (unknown $name)

删除服务容器中的一个服务,它会同时删除服务创建的任何共享过的实例。 | Removes a service in the services container It also removes any shared instance created for the service

public attempt (unknown $name, unknown $definition, [unknown $shared])

尝试在服务容器中去注册一个服务 ,仅在之前没有注册过相同名字的时候才会注册成功。 | Attempts to register a service in the services container Only is successful if a service hasn”t been registered previously with the same name

public setRaw (unknown $name, unknown $rawDefinition)

设置服务使用原始定义(Falcon\Di\Service) | Sets a service using a raw Phalcon\Di\Service definition

public getRaw (unknown $name)

无返回(上面设置的)一个没有解析的原始服务数据 |Returns a service definition without resolving

public getService (unknown $name)

返回一个 Phalcon\Di\Service 实例 |Returns a Phalcon\Di\Service instance

public get (unknown $name, [unknown $parameters])

基于配置 解析服务 | Resolves the service based on its configuration

public mixed getShared (string $name, [array $parameters])

解析一个服务,解析过的服务已存储在 DI 里,这个服务后来的请求将会返回相同的实例。 | Resolves a service, the resolved service is stored in the DI, subsequent requests for this service will return the same instance

public has (unknown $name)

检查DI是否包含所述服务 | Check whether the DI contains a service by a name

public wasFreshInstance ()

检查最后一个服务是否已存在或者通过 getShared 获取生成了新的实例 | Check whether the last service obtained via getShared produced a fresh instance or an existing one

public getServices ()

返回DI中已注册的服务 | Return the services registered in the DI

public offsetExists (unknown $name)

检查是否使用数组语法注册过 | Check if a service is registered using the array syntax

public boolean offsetSet (string $name, mixed $definition)

允许使用数组语法注册共享服务 | Allows to register a shared service using the array syntax

<?php

$di["request"] = new \Phalcon\Http\Request();

public mixed offsetGet (string $name)

允许使用数组语法来获得一个共享服务 | Allows to obtain a shared service using the array syntax

<?php

var_dump($di["request"]);

public offsetUnset (unknown $name)

使用数组语法从服务容器中移除一个服务 | Removes a service from the services container using the array syntax

public mixed __call (string $method, [array $arguments])

使用 setters/getters 设置或获取服务的 魔术方法 | Magic method to get or set services using setters/getters

public static setDefault (unknown $dependencyInjector)

设置一个默认依赖注入容器来获取到静态方法 | Set a default dependency injection container to be obtained into static methods

public static getDefault ()

返回最后一个创建的DI | Return the lastest DI created

public static reset ()

重置内部默认的DI | Resets the internal default DI