phpixie / di
PHPixie 的依赖注入容器
3.2.1
2018-02-16 00:20 UTC
Requires (Dev)
- phpixie/test: ~3.0
README
PHPixie 依赖注入库
PHPixie DI 是一个简单、快速、严格且灵活的依赖注入容器,旨在使您的代码更加解耦,并允许更流畅的开发。它还提供了一个可选的静态接口,这是大多数 DI 容器通常缺少的。
使用方法
要开始使用 DI,请扩展基本容器,并通过 configure()
方法指定您的配置
class Container extends \PHPixie\DI\Container\Root { public function configure() { // Simple value stored by key $this->value('apiToken', '1234567890'); // Dynamic method definition $this->callback('addFive', function($a, $b) { return $a + $b; }); // This method will be called only once, // which is perfect for service definitions $this->build('twitterService', function() { return new TwitterService($this->apiToken()); // or return new TwitterService($this->get('apiToken')); }); // Or you can also use a shortcut. // Note that the parameters with '@' in their name // will be replaced by corresponding value in the container $this->instance('twitterService', TwitterService::class, ['@apiToken']); // Nested group $this->group('user', function() { $this->instance('repository', UserRepository::class, ['@twitterService']); }); } } // initialize the container $container = new Container(); // Getting by key $container->get('apiToken'); $container->apiToken(); // Static methods are only allowed // after the container has been constructed Container::apiToken(); Container::get('apiToken'); // Dynamic method call // (also works via static methods) $container->add(6, 7); // 13 $container->call('add', [6, 7]); $callable = $container->get('add'); $callable(6, 7); // Accessing nested definitions $container->get('user.repository'); $userGroup = $container->user(); $userGroup->repository(); Container::user()->repository(); // etc...
您还可以使用容器来访问容器服务中的深层方法,例如,如果 TwitterService
类包含一个 getTweets
方法,您可以像这样调用它
// $container->twitterService()->getTweets(); $container->get('twitterService.getTweets'); // $container->twitterService()->getTweets()->first()->delete(true); $container->call('twitterService.getTweets.first.delete', [true]); // The above also works using static methods Container::call('twitterService.getTweets.first.delete', [true]);
配置方法,如 value
和 callback
被定义为 protected
,因此它们只能从类内部访问。这确保了容器在初始化后是不可变的,并确保所有配置都包含在一个地方。当然,您仍然可以通过将它们重写为 public
来允许这种行为。
在 IDE 中使用类型提示
由于值是动态定义的,因此当使用容器时,您不会为您的服务获得类型提示。要获得该功能,您可以使用 @method
注解
/** * @method TwitterService twitterService() * @method static TwitterService twitterService() */ class Container { //... }
与 PHPixie 一起使用
所有 PHPixie 组件都使用 Factory 类来构建依赖项,但默认捆绑包附带一个基本容器,该容器已包含一些有用的配置。这也使此组件完全可选。
首先创建您的容器类
namespace Project\App; // Note that we are extnding a different class this time class Container extends \PHPixie\DefaultBundle\Container { public function configure() { //....your own definitions parent::configure(); // don't forget this call } }
并在构建器中注册它
namespace Project\App; class Builder extends \PHPixie\DefaultBundle\Builder { protected function buildContainer() { return new Container($this); } }
构建器将在定义容器后自动初始化它,因此如果您愿意,可以立即使用静态方法。以下是一些使用示例
$container = $builder->container(); $container->get('components.orm'); $query = $container->call('components.orm.query', ['user']); $builder = Container::builder(); $frameworkBuilder = Container::frameworkBuilder();