hatem-azzouzi/z-di
Composer 安装命令:
composer require hatem-azzouzi/z-di
包简介
Easy minimalist dependenncy injection package
README 文档
README
Contents of this file
Introduction
Z-DI is intended for educational purposes for beginners looking to learn more skills and master PHP. Very lightweight and minimalist DI (Dependency Injection) package with lazy loading and recursive configuration capabilities.
Requirements
There is no requirement to install this package other than PHP. PHP >=7.2.0 is recommended. PHP 8.3 is highly recommended.
Installation
Use the dependency manager composer to install Z-DI.
composer require hatem-azzouzi/z-di
Usage
Dependency Injection (DI)
In software engineering, DI is a programming technique to inject a resource or a service (dependency) into a client on which it depends on to function. Client does not have to create nor to instantiate the resource or know how it is created.
Below is an example of a configuration file to create entry points for 2 services and a client. In this example, we inject the Email service into the Client. You can also try the Text service. Both services implement the same interface IMessage.
<?php // /conf/config.injection.php /* @var $this \ZDI\Inject */ return array( \ZDI\tests\injection\Email::class => $this->create(\ZDI\tests\injection\Email::class, 'email'), \ZDI\tests\injection\Text::class => $this->create(\ZDI\tests\injection\Text::class, 'text'), \ZDI\tests\injection\Client::class => $this->create(\ZDI\tests\injection\Client::class, 'client') ->inject(\ZDI\tests\injection\Email::class), );
To test the injection, run tests/injection/index.php.
<?php echo "\n"; echo "bootstrapping..\n"; /* @var $inject \ZDI\Inject */ $inject = require_once __DIR__ . '/../../conf/bootstrap.php'; echo "\n"; echo "get Client definition\n"; /* @var $client \ZDI\tests\injection\Client */ $client = \ZDI\Inject::getDefinition(\ZDI\tests\injection\Client::class); echo "\n"; echo "send function call\n"; $client->send("a test message"); echo "\n";
$ cd tests/injection $ php index.php config=injection bootstrapping.. get Client definition send function call ZDI\tests\injection\Email instantiated: email ZDI\tests\injection\Client instantiated: client ZDI\tests\injection\Email email sent: a test message
Lazy loading
Lazy loading a resource is intended to differ loading or instantiating a resource until it is needed. This technique is for (web) applications where performance, high availability, acceleration and initialization times to be kept at a minimum are critical.
Below is an example of a configuration file to create an entry point for ClassA injection and its constructor parameters.
<?php // /conf/config.lazy.php /* @var $this \ZDI\Inject */ return array( ZDI\tests\ignition\ClassA::class => $this->create(ZDI\tests\ignition\ClassA::class, 'hello'), );
Run tests/lazy/index.php.
<?php echo "\n"; echo "bootstrapping..\n"; /* @var $inject \ZDI\Inject */ $inject = require_once __DIR__ . '/../../conf/bootstrap.php'; echo "\n"; echo "get ClassA definition\n"; /* @var $classA ZDI\tests\ignition\ClassA */ $classA = \ZDI\Inject::getDefinition(ZDI\tests\ignition\ClassA::class); echo "\n"; echo "foo function call\n"; $classA->foo("bar"); echo "\n";
$ cd tests/ignition $ php index.php config=lazy bootstrapping.. get ClassA definition foo function call ZDI\tests\ignition\ClassA instantiated: hello ZDI\tests\ignition\ClassA function called: bar
The ClassA is instantiated after making the first foo() function call. As long as foo() is not called, the ClassA will never load.
Eager loading
Alternatively, eager loading is to load the resource as soon as the bootstrap is started. In our example above, instead of just referencing the resource, it will be instantiated using instance() function as shown below.
<?php // /conf/config.lazy.php /* @var $this \ZDI\Inject */ return array( ZDI\tests\lazy\ClassA::class => $this->instance(ZDI\tests\lazy\ClassA::class, 'hello'), );
$ cd tests/ignition $ php index.php config=eager bootstrapping.. ZDI\tests\ignition\ClassA instantiated: hello get ClassA definition foo function call ZDI\tests\ignition\ClassA function called: bar
The ClassA is instantiated before making the first foo() function call.
NOTE: In our injection example above we have used lazy loading, if we rather would like to use eager loading, we need to inject the instantiated service when instantiating the client.
<?php /* @var $this \ZDI\Inject */ return array( \ZDI\tests\injection\Email::class => $this->instance(\ZDI\tests\injection\Email::class, 'email'), \ZDI\tests\injection\Text::class => $this->instance(\ZDI\tests\injection\Text::class, 'text'), \ZDI\tests\injection\Client::class => $this->instance( \ZDI\tests\injection\Client::class, $this->instance(\ZDI\tests\injection\Email::class, 'text'), 'client' ), );
Recursion
Injection can be replaced recursively based on the environment variable such as staging or production, the hostname or any other specific configuration. The first element in the array is the default injections configuration file.
<?php namespace ZDI\conf; require_once __DIR__ . '/../vendor/autoload.php'; parse_str($argv[1], $arg); $config = $arg['config'] ?? ''; return (new \ZDI\Inject(\ZDI\Magic::class)) ->setDefinitions( [ __DIR__ . '/config.php', __DIR__ . "/config.$config.php", __DIR__ . '/config.' . strtolower(gethostname()) . '.php' ] );
To test the recursion..
Default configuration
[
ZDI\tests\ignition\ClassA::class => $this->create(ZDI\tests\ignition\ClassA::class, 'foo', 'bar'),
]
$ cd tests/ignition $ php index.php bootstrapping.. get ClassA definition foo function call ZDI\tests\ignition\ClassA instantiated: foo ZDI\tests\ignition\ClassA function called: bar
Dev configuration
[
ZDI\tests\ignition\ClassA::class => $this->create(ZDI\tests\ignition\ClassB::class, 'foo', 'bar'),
]
$ cd tests/ignition $ php index.php config=dev bootstrapping.. get ClassA definition foo function call ZDI\tests\ignition\ClassB instantiated: foo ZDI\tests\ignition\ClassB function called: bar
Production configuration
[
ZDI\tests\ignition\ClassA::class => $this->create(ZDI\tests\ignition\ClassC::class, 'foo 1', 'bar 2'),
]
$ cd tests/ignition $ php index.php config=prod bootstrapping.. get ClassA definition foo function call ZDI\tests\ignition\ClassC instantiated: foo 1 ZDI\tests\ignition\ClassC function called: bar
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Maintainers
License
hatem-azzouzi/z-di 适用场景与选型建议
hatem-azzouzi/z-di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「di」 「proxy」 「ioc」 「lazy loading」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hatem-azzouzi/z-di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hatem-azzouzi/z-di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hatem-azzouzi/z-di 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lazy loading for middleware and request handlers
A fast and intuitive dependency injection container.
Dependency injection container for the Monolith framework.
Http Microframework for Hack
Get working public proxy list for free by PubProxy (No API key required).
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-18