anax / di
Anax DI模块,依赖注入(PHP-FIG PSR-11)。
Requires
- php: ^7.2
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-09-16 23:52:57 UTC
README
Anax DI服务容器,用于框架服务的依赖注入,通过创建和懒加载服务。
容器实现符合PHP-FIG PSR-11:容器接口
目录
您也可以在线阅读此文档。
安装
您可以使用composer从anax/di
在Packagist上安装模块。
composer require anax/di
开发
要作为开发者工作,您需要克隆仓库并通过make安装本地环境。然后您可以运行单元测试。
make install
make test
基本使用
这是容器的基本使用方法。
首先创建容器。
// Create it $di = new \Anax\DI\DI(); // Check its a PSR-11 interface assert($di instanceof \Psr\Container\ContainerInterface);
将服务添加到容器中。
// Add a service $di->set("response", "\Anax\Response\Response"); // Add a shared service $di->setShared("view", "\Anax\View\ViewContainer");
检查服务是否已加载。
// Check if service is loaded if ($di->has("view")) { ; // the service is loaded }
获取并使用服务。
// Get and use a service $response = $di->get("response"); $response->addBody($body)->send(); // Same, without storing in a variable $di->get("response")->addBody($body)->send();
共享服务
共享服务总是返回相同的对象。对象仅实例化一次,然后存储以供后续使用。共享服务只有一个实例。服务在所有用户之间共享。
// Add a shared service $di->setShared("view", "\Anax\View\ViewContainer"); // Get two instances of the shared service $view1 = $di->get("view"); $view2 = $di->get("view"); assert($view1 === $view2);
非共享服务每次从容器获取时都会返回一个新的实例。
// Add a service $di->set("response", "\Anax\Response\Response"); // Get two instances of the service $response1 = $di->get("response"); $response2 = $di->get("response"); assert($response1 !== $response2);
懒加载
添加到容器的服务在访问之前不会激活。它们是懒加载的,以确保仅在需要时才激活。
您可以看到加载到容器中的服务以及激活的服务。
// Add services $di->set("response", "\Anax\Response\Response"); $di->setShared("view", "\Anax\View\ViewContainer"); // Get one service $response = $di->get("response"); // Check what services are loaded implode(",", $di->getServices()); // response,view // Check what services are active implode(",", $di->getActiveServices()); // response
在Anax中使用
In Anax,所有服务都在文件htdocs/index.php
的引导阶段加载。它看起来像这样。
// Add all framework services to $di $di = new Anax\DI\DIFactoryConfig(); $di->loadServices(ANAX_INSTALL_PATH . "/config/di");
变量$di
是框架中的唯一全局变量。主要依赖项作为全局变量,可以在视图模板文件和视图助手文件中访问。
Anax服务的初始化
所有框架服务都是从目录ANAX_INSTALL_PATH . "/config/di"
中的文件加载的。每个文件包含一个服务。一个文件可以包含多个服务。
这是服务"request"的服务配置文件示例。
/** * Configuration file for request service. */ return [ // Services to add to the container. "services" => [ "request" => [ "shared" => true, "callback" => function () { $obj = new \Anax\Request\Request(); $obj->init(); return $obj; } ], ], ];
服务可以包含一个回调,在加载时启动服务。回调应返回启动的对象。
当服务不需要额外初始化时,可以通过其类名定义服务。
"callback" => "\Anax\Request\Request",
初始化服务的一种常见方法是注入$di
到服务中。这是一个例子。
"response" => [ "shared" => true, //"callback" => "\Anax\Response\Response", "callback" => function () { $obj = new \Anax\Response\ResponseUtility(); $obj->setDI($this); return $obj; } ],
Anax服务的配置
在初始化阶段,一些服务可能需要读取额外的配置信息。它们可以通过使用anax/configuration
来实现,如下所示。
首先,让我们看看"configuration"服务。
"configuration" => [ "shared" => true, "callback" => function () { $config = new \Anax\Configure\Configuration(); $dirs = require ANAX_INSTALL_PATH . "/config/configuration.php"; $config->setBaseDirectories($dirs); return $config; } ],
这是一个从框架指定的目录中读取配置文件的服务。它可以这样使用,在创建依赖于它们自己的配置文件的其它服务时。
"session" => [ "active" => defined("ANAX_WITH_SESSION") && ANAX_WITH_SESSION, // true|false "shared" => true, "callback" => function () { $session = new \Anax\Session\Session(); // Load the configuration files $cfg = $this->get("configuration"); $config = $cfg->load("session"); // Set various settings in the $session // ... return $session; } ],
配置设置"active"表示服务在加载时是否应该被激活,或者是否应该懒加载。
在回调中,首先从$di中检索服务"configuration",然后使用它来读取名为"session"的配置文件。
实际的配置从配置文件 "session" 中返回,然后可以用来配置和设置对象,在将其作为框架服务返回之前。
配置文件通常按照服务名存储在 ANAX_INSTALL_PATH . "/config/servicename.php"
。它应该返回一些内容,如下所示。
/** * Config-file for sessions. */ return [ // Session name "name" => preg_replace("/[^a-z\d]/i", "", __DIR__), ];
依赖
通过 psr/container
使用 psr11。
许可
本软件携带 MIT 许可证。有关详细信息,请参阅 LICENSE.txt。
.
..: Copyright (c) 2013 - 2018 Mikael Roos, mos@dbwebb.se