cspray/annotated-container
Composer 安装命令:
composer require cspray/annotated-container
包简介
Create Dependency Injection containers configured with PHP8 Attributes.
README 文档
README
A Dependency Injection framework for creating an autowired, feature-rich, PSR-11 compatible Container using PHP 8 Attributes!
- Designate an interface as a Service and easily configure which concrete implementations to use
- Delegate service construction to a factory
- Inject scalar values, environment variables, and other services into your constructors and setters
- Automatically invoke methods after the service is constructed
- Use Profiles to easily use different services in different runtimes
- Create type-safe, highly flexible configuration objects
- Easily include third-party services that cannot be easily annotated
- Bring Your Own Container!
Quick Start
This quick start is intended to get you familiar with Annotated Container's core functionality and get a working Container created. First, a simple example showing how an interface can be aliased to a concrete Service. After that we'll show you how to get a Container to create the Service.
Code Example
<?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; } }
This example is built upon in the docs. Check out the tutorials for more examples of Annotated Container's functionality!
Bootstrapping Your Container
Annotated Container ships with a built-in CLI tool to easily create a configuration detailing how to build your Container and a corresponding Cspray\AnnotatedContainer\Bootstrap\Bootstrap implementation to create your Container using that configuration. It is highly recommended to use the provided tooling to create your Container.
The CLI tool offers extensive documentation detailing how to run commands and what options are available. If you're ever looking for more info run:
./vendor/bin/annotated-container help
The first step is to create the configuration. By default, the tooling will look at your composer.json to determine what directories to scan and create a directory that the ContainerDefinition can be cached in. Run the following command to complete this step.
./vendor/bin/annotated-container init
The configuration file will be created in the root of your project named "annotated-container.xml". The cache directory will also be created in the root of your project named ".annotated-container-cache". Check out the command's help documentation for available options, including how to customize these values.
Be sure to review the generated configuration! A "normal" Composer setup might result in a configuration that looks like the following. If there are any directories that should be scanned but aren't listed in <source></source> be sure to include them. Conversely, if there are directories included that shouldn't be scanned be sure to remove them.
<?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>
Now, bootstrap your Container in your app.
<?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
Installation
composer require cspray/annotated-container
Choose a Backing Container
AnnotatedContainer does not provide any of the actual Container functionality. We provide Attributes and definition objects that can determine how actual implementations are intended to be setup. AnnotatedContainer currently supports the following backing Containers:
rdlowrey/auryn
composer require rdlowrey/auryn
php-di/php-di
composer require php-di/php-di
illuminate/container
composer require illuminate/container
Documentation
This library is thoroughly documented in-repo under the /docs directory. The documentation is split into three parts; Tutorials, How Tos, and References.
Tutorials are where you'll want to start. It'll expand on the examples in the "Quick Start" and teach you how to do most of the things you'll want to do with the library. This documentation tends to split the difference between the amount of code and the amount of explanation.
How Tos are where you'll go to get step-by-step guides on how to achieve specific functionality. These documents tend to be more code and less explanation. We assume you've gotten an understanding of the library and have questions on how to do something beyond the "normal" use cases.
References are where you can get into the real internal, technical workings of the library. List of APIs and more technically-explicit documentation can be found here. References may be a lot of code, a lot of explanation, or a split between the two depending on the context.
Roadmap
The Roadmap can be found in the annotated-container Project page.
External Resources
Notable Libraries Using Annotated Container
- Annotated Console
- Labrador Async Event (3.0.0-beta2+)
Blog Posts
- Introducing Annotated Container - Part 1
- Introducing Annotated Container - Part 2
- Introducing Annotated Container - Part 3
- Annotated Container, Why Attributes?
- Autowiring Annotated Container
Demos
cspray/annotated-container 适用场景与选型建议
cspray/annotated-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.32k 次下载、GitHub Stars 达 42, 最近一次更新时间为 2022 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「container」 「attributes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cspray/annotated-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cspray/annotated-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cspray/annotated-container 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
Http Microframework for Hack
PHP Dependency Injection Container
统计信息
- 总下载量: 1.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 42
- 点击次数: 9
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-23