cspray/annotated-container

创建使用 PHP8 属性配置的依赖注入容器。

v2.3.0 2024-05-22 11:29 UTC

README

Unit Tests

一个用于创建自动装配、功能丰富的、兼容PSR-11容器的依赖注入框架,使用 PHP 8 属性!

  • 指定一个接口作为服务,并轻松配置要使用哪个具体实现
  • 将服务构造委托给工厂
  • 将标量值、环境变量和其他服务注入到构造函数和设置器中
  • 在服务构造后自动调用方法
  • 使用配置文件轻松在不同运行时使用不同的服务
  • 创建类型安全、高度灵活的配置对象
  • 轻松包含难以注解的第三方服务
  • 使用自己的容器!

快速入门

本快速入门旨在让您熟悉 Annotated Container 的核心功能,并创建一个可工作的容器。首先,一个简单的示例,展示如何将接口别名到具体的服务。之后,我们将向您展示如何让容器创建服务。

代码示例

<?php declare(strict_types=1);

// interfaces and classes in __DIR__ . '/src'

use Cspray\AnnotatedContainer\Attribute\Service;
use Cspray\AnnotatedContainer\Attribute\InjectEnv;

#[Service]
interface BlobStorage {

    public function store(string $identifier, string $contents) : void;
    
    public function retrieve(string $identifier) : ?string;

}

#[Service]
class FilesystemStorage implements BlobStorage {
    
    public function store(string $identifier, string $contents) : void {
        file_put_contents($identifier, $contents);
    }
    
    public function retrieve(string $identifier) : ?string {
        return file_get_contents($identifier) ?? null;
    }

}

本示例基于文档中的示例。查看教程以获取 Annotated Container 功能的更多示例!

启动您的容器

Annotated Container 随附一个内置的 CLI 工具,可以轻松创建一个配置文件,详细说明如何构建您的容器,以及一个相应的 Cspray\AnnotatedContainer\Bootstrap\Bootstrap 实现来使用该配置创建容器。强烈建议使用提供的工具创建容器。

CLI 工具提供了详细的文档,说明了如何运行命令和可用的选项。如果您需要更多信息,请运行: ./vendor/bin/annotated-container help

第一步是创建配置文件。默认情况下,工具将查看您的 composer.json 以确定要扫描的目录,并在项目中创建一个可以缓存 ContainerDefinition 的目录。运行以下命令以完成此步骤。

./vendor/bin/annotated-container init

配置文件将创建在项目根目录下,命名为 "annotated-container.xml"。缓存目录也将创建在项目根目录下,命名为 ".annotated-container-cache"。查看命令的帮助文档以获取可用的选项,包括如何自定义这些值。

请务必检查生成的配置!一个“正常”的 Composer 设置可能会生成以下配置。如果应扫描的任何目录没有在 <source></source> 中列出,请确保包括它们。相反,如果包括应 扫描的目录,请确保删除它们。

<?xml version="1.0" encoding="UTF-8" ?>
<annotatedContainer xmlns="https://annotated-container.cspray.io/schema/annotated-container.xsd">
  <scanDirectories>
    <source>
      <dir>src</dir>
      <dir>tests</dir>
    </source>
  </scanDirectories>
  <cacheDir>.annotated-container-cache</cacheDir>
</annotatedContainer>

现在,在您的应用程序中启动您的容器。

<?php declare(strict_types=1);

// app bootstrap in __DIR__ . '/app.php'
require __DIR__ . '/vendor/autoload.php';

use Cspray\AnnotatedContainer\Bootstrap\Bootstrap;

// Include other active profiles in this list
// If the only active profile is default you can call this method without any arguments
$profiles = ['default'];
$container = (new Bootstrap())->bootstrapContainer($profiles);

$storage = $container->get(BlobStorage::class);     // instanceof FilesystemStorage

安装

composer require cspray/annotated-container

选择支持容器

AnnotatedContainer 不提供任何实际的容器功能。我们提供属性和定义对象,可以确定实际实现应该如何设置。AnnotatedContainer 当前支持以下支持容器

rdlowrey/auryn

composer require rdlowrey/auryn

php-di/php-di

composer require php-di/php-di

illuminate/container

composer require illuminate/container

文档

此库在代码库中的 /docs 目录中得到充分文档记录。文档分为三个部分;教程、如何做和参考。

教程 是您应该开始的地方。它将在“快速入门”中的示例上进行扩展,并教您如何使用库完成大多数事情。这种文档通常在代码量和解释量之间取得平衡。

操作指南 是您获取如何实现特定功能分步指南的地方。这些文档通常包含更多代码和较少的解释。我们假设您已经了解了库,并希望了解如何超出“正常”用例进行操作。

参考 是您可以深入了解库的实际内部、技术运作的地方。可以在这里找到API列表和更多技术性明确的文档。根据上下文,参考可能包含大量代码、大量解释,或者两者兼而有之。

路线图

路线图可以在 标注容器项目页面 中找到。

外部资源

使用标注容器的知名库

博客文章

演示