mcustiel / php-simple-di
内存使用低、性能高的最小化库,用于管理依赖注入。
Requires
- php: >=5.5
Requires (Dev)
- phing/phing: >=2.0
- phpmd/phpmd: @stable
- phpunit/phpunit: >=4.3.0
- sebastian/phpcpd: >=1.4.3
- squizlabs/php_codesniffer: >=2.1.0
This package is auto-updated.
Last update: 2024-09-19 09:32:42 UTC
README
这是什么?
php-simple-di(Php 简单依赖注入)是一个库,它提供了一个最小化的依赖容器,能够通过名称识别提供依赖项的单一或原型版本。php-simple-di 提供了一个单一类,您可以在其中注册您的依赖项,通过名称识别它们,然后您可以检索它们。此库仅按需创建实例(它不会实例化不需要请求执行的依赖项),因此您只处理和保留内存中您正在使用的内容。
安装
Composer
{ "require": { // ... "mcustiel/php-simple-di": "^1.2.1" } }
如何使用它?
注册
在您的引导文件(或某些启动脚本)中,您必须定义您的类可能需要的所有可能的依赖项。
use Mcustiel\DependencyInjection\DependencyInjectionService; $dependencyInjectionService = new DependencyInjectionService(); // ... $dbConfig = loadDbConfig(); $cacheConfig = loadCacheConfig(); $dependencyInjectionService->register('dbConnection', function() use ($dbConfig) { return new DatabaseConnection($dbConfig); }); $dependencyInjectionService->register('cache', function() use ($cacheConfig) { return new CacheManager($cacheConfig); });
获取依赖项
然后您可以通过引用它们的标识符来检索实例。
$cacheManager = $dependencyInjectionService->get('cache');
实例
php-simple-di 默认创建 "单一实例",这意味着每次您请求依赖项时,它都会返回相同的实例。如果需要改变这种行为,您可以定义每次您向 php-simple-di 请求依赖项时,它都会返回一个新实例。这种行为是通过 register 方法中的布尔参数改变的。
单一实例行为
$dependencyInjectionService->add('dbConnection', function() use ($dbConfig) { return new DatabaseConnection($dbConfig); }); // or also you can make it explicit: $dependencyInjectionService->register('cache', function() use ($cacheConfig) { return new CacheManager($cacheConfig); }, true); $instance1 = $dependencyInjectionService->get('cache'); $instance2 = $dependencyInjectionService->get('cache'); // $instance1 and $instance2 reference the same object
原型行为
$dependencyInjectionService->register('dbConnection', function() use ($dbConfig) { return new DatabaseConnection($dbConfig); }, false); $instance1 = $dependencyInjectionService->get('cache'); $instance2 = $dependencyInjectionService->get('cache'); // $instance1 and $instance2 reference different objects
注意
为了简化前面的示例,我已显示配置,但 php-simple-di 完全能够将此功能封装在回调中,甚至可以调用自身。
$dependencyInjectionService->add('config-loader', function() { return new SomeConfigLoaderService(); }); $dependencyInjectionService->add('config', function() { $injector = new DependencyInjectionService(); $configLoader = $injector->get('config-loader'); return $configLoader->load(); }); $dependencyInjectionService->add('dbConnection', function() { $injector = new DependencyInjectionService(); $dbConfig = $injector->get('config'); return new DatabaseConnection($dbConfig); });
关于 Singleton 模式有很多讨论,人们将其称为反模式,因为它很难测试。无论如何,php-simple-di 提供了一个作为单一类的容器,以允许只有一个实例参与执行。您应该考虑良好的实践,并避免通过单一实例使用此类,而是在引导文件中定义它,并将容器实例作为参数传递给您的应用程序分发器,并且始终作为参数传递(将其作为依赖项注入)。然后,请记住正确使用它,不要将容器作为依赖项传递,而是使用它来获取依赖项并将它们传递给您的服务。
// Do this: $dbConnection = $dependencyInjectionService->get('dbConnection'); $personDao = new PersonDao($dbConnection); // Pass the proper dependency // Instead of doing this: $personDao = new PersonDao($dependencyInjectionService); // This works but is heretic and makes a little kitten cry.