assodepicche/php-di-container
Composer 安装命令:
composer require assodepicche/php-di-container
包简介
A PHP Dependency Injection Container
README 文档
README
According to DotNetTricks, a DI Container is a framework to create dependencies and inject them automatically when required. It automatically creates objects based on the request and injects them when required. DI Container helps us to manage dependencies within the application in a simple and easy way.
The DI container creates an object of the defined class and also injects all the required dependencies as an object a constructor, a property, or a method that is triggered at runtime and disposes itself at the appropriate time. This process is completed so that we don't have to create and manage objects manually all the time.
These repository contains a DI container for PHP projects.
Table of Contents
The Interface
The Dependency Injection Container interface defines four methods: autowire, get, set and singleton. All these methods expect a class or interface name as a string, but only the last two methods also expect a callable, which represents a definition of the class or interface.
<?php declare(strict_types=1); namespace Container\Adapter; interface DependencyInjectionContainer { public function autowire(string $className): object; /** * @template TClassName * @param class-string<TClassName> $className * @return TClassName */ public function get(string $className): object; public function has(string $className): bool; public function set(string $className, callable $definition): self; public function singleton(string $className, callable $definition): self; }
The Implementation
The implementation of the "DependencyInjectionContainer" interface is the "DependencyContainer" class and uses the PHP reflection API to manage the instantiation of registered classes.
<?php declare(strict_types=1); namespace Container\Infrastructure; use Container\Adapter\DependencyInjectionContainer; use ReflectionClass; use ReflectionParameter; final class DependencyContainer implements DependencyInjectionContainer { private array $definitions = []; private array $singletons = []; public function autowire(string $className): object { $reflectionClass = new ReflectionClass($className); $constructorParameters = array_map( fn (ReflectionParameter $parameter) => $this->get($parameter->getType()->getName()), $reflectionClass->getConstructor()?->getParameters() ?? [] ); return new $className(...$constructorParameters); } public function get(string $className): object { if ($instance = $this->singletons[$className] ?? null) { return $instance; } $definition = $this->definitions[$className] ?? $this->autowire(...); return $definition($className); } public function has(string $className): bool { return isset($this->definitions[$className]) || isset($this->singletons[$className]); } public function set(string $className, callable $definition): self { $this->definitions[$className] = $definition; return $this; } public function singleton(string $className, callable $definition): self { $this->definitions[$className] = function () use ($className, $definition) { $this->singletons[$className] = $definition($this); return $this->singletons[$className]; }; return $this; } }
Installation
First, make sure you have PHP installed (8.2 version or higher) and then clone this repository or install the library via composer.
- Installation via Git
git clone git@github.com:AssoDePicche/php-di-container.git
- Installation via Composer
composer require assodepicche/php-di-container
Getting Started
Instantiate the DependencyContainer class
<?php use Container\Infrastructure\DependencyContainer; $container = new DependencyContainer;
Use the set method to set the definition of a class
$container->set(Foo::class, fn () => new Foo);
Call the defined class with the get method whenever you want
$object = $container->get(Foo::class);
Use the has method to find out if an interface has been defined in the container
var_dump($container->has(Foo::class)); // true
Obs: to make a class a singleton, use the singleton method
$container->singleton(Singleton::class, fn () => new SingletonClass);
Contributing
To contribute to this project follow these steps.
Get in Touch
Samuel do Prado Rodrigues (AssoDePicche) - samuelprado730@gmail.com
assodepicche/php-di-container 适用场景与选型建议
assodepicche/php-di-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 assodepicche/php-di-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 assodepicche/php-di-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unlicense
- 更新时间: 2023-07-23