robert430404 / rc-container
这是一个简单的PHP依赖注入容器。
1.2.0
2017-04-04 17:42 UTC
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2024-09-14 20:30:33 UTC
README
这是什么?
这是RC容器。这是一个简单的依赖注入容器,允许您注册服务、参数和工厂,然后检索/注销它们。
为什么编写这个?
我这样做是为了锻炼我的大脑,并全面了解PHP空间中DI-容器的工作原理。而不是只是阅读它并假设我知道它在做什么,我写这个来巩固我的知识。
安装包
要安装和使用此包,请使用composer安装
composer require robert430404/rc-container
它是如何工作的?
容器依赖于composer来自动加载类。然后您创建一个新的Container()对象实例,并开始将服务/参数/工厂分配给该实例。
<?php use RcContainer\Container; require 'vendor/autoload.php'; $container = new Container(); $container->registerParameter('test-parameter', function () { return 'this-is-the-test-param'; }); $container->registerService('test-service', function () { return new stdClass(); }); $container->registerFactory('test-factory', function () { return new stdClass(); });
您还可以使用此方法在一次调用中分配多个服务、参数或工厂
<?php use RcContainer\Container; require 'vendor/autoload.php'; $container = new Container(); $container->registerServices([ 'test-service-1' => function () { return new stdClass(); }, 'test-service-2' => function () { return new stdClass(); } ]); $container->registerParameters([ 'test-parameter-1' => function () { return 'parameter variable'; }, 'test-parameter-2' => function () { return 'second parameter variable'; } ]); $container->registerFactories([ 'test-factory-1' => function () { return new stdClass(); }, 'test-factory-2' => function () { return new stdClass(); } ]);
或者您可以使用更简洁的语法,如下所示
<?php use RcContainer\Container; require 'vendor/autoload.php'; $container = new Container(); $container->registerServices([ 'test-service-1' => 'stdClass', 'test-service-2' => 'stdClass' ]); $container->registerParameters([ 'test-parameter-1' => 'parameter variable', 'test-parameter-2' => 'second parameter variable' ]); $container->registerFactories([ 'test-factory-1' => 'stdClass', 'test-factory-2' => 'stdClass' ]);
定义好服务/参数/工厂后,您就可以通过容器上的检索方法来获取已注册的服务/参数/工厂。
<?php $container->parameter('test-parameter'); // Returns your param $container->service('test-service'); // Returns the service (Same Instance) $container->factory('test-factory'); // Returns the factory's object (New Instance)
有哪些功能?
容器允许您将其绑定到服务、工厂和参数。这使得您有一个中心位置来访问依赖关系,并按需注入所需的内容。您还可以通过将容器传递到闭包中,通过容器注入互锁的依赖关系。
<?php use RcContainer\Container; use Vendor\SDKObject; // Made Up Namespace require 'vendor/autoload.php'; $container = new Container(); // Parameters $container->registerParameter('api-key', function () { return '000-000-000-000-0000'; }); // Services $container->registerService('the-api', function () use ($container) { $apiKey = $container->parameter('api-key'); // Retrieves Registered Key return new SDKObject($apiKey); // Made Up Object });
相同的方法可以用来传递服务
<?php use RcContainer\Container; use Vendor\SDKObject; // Made Up Namespace use Vendor\DataFactory; // Made Up Namespace require 'vendor/autoload.php'; $container = new Container(); // Parameters $container->registerParameter('api-key', function () { return '000-000-000-000-0000'; }); // Services $container->registerService('the-api', function () use ($container) { $apiKey = $container->parameter('api-key'); // Retrieves Registered Key return new SDKObject($apiKey); // Made Up Object }); // Factories $container->registerFactory('data-factory', function () use ($container) { $apiSdk = $container->service('the-api'); return new DataFactory($apiSdk); });
这为您提供了一个强大且易于使用的容器,应该适合大多数DI需求。