grifart/nette-scoped-di
Composer 安装命令:
composer require grifart/nette-scoped-di
包简介
Multiple DI containers support for Nette DI
README 文档
README
Split your app dependency injection container into well-defined scopes and enforce relationships between them.
On the edge between monolith and microservices
This approach stays in the middle of monolith and microservices.
From architectonical perspective it is equavalent to microservices approach. There is clear boundary and interface between model and app.
Technically you save tons of hours of maintainace cost of managing microservices. Because there is still one app, but with many independent modules. They have clear boundaries and explicitly defined dependencies. And when you break this boundary, you get an exception.
So if needed you can easily turn already defined modules into microservices later. Only part that is missing here is a serialization & networking stack. This can be easily solved by adding RPC layer between modules that are extracted into microservices.
More resources:
- 🎥 Jan Kuchar: Why you need to have more then one DI (english)
- 🎥 Live stream of Jan Tvrdik's talk (Czech only)
Example
You have a model where you want to expose access only to a few facades. So you export them from the model container into the app container, and the app then can only access those exported services. This means that app can no longer ignore ACL rules or directly access a database.
Registering the model into a user DI container
For the model container, you must create a DI extension to register all the services exported from the model to the user's application (UI/API/CLI/...) part of the app.
<?php declare(strict_types=1); namespace App\Infrastructure; use Mangoweb\NetteDIScope\ScopeExtension; use Nette\Configurator; final class ModelExtension extends ScopeExtension { public static function getTagName(): string { // Every service marketed with an `exported` tag will be available in every *user* DI container return 'exported'; } protected function createInnerConfigurator(): Configurator // @phpstan-ignore-line { $configurator = parent::createInnerConfigurator(); $configurator->addConfig(__DIR__ . '/../container.neon'); // the configration entrypoint of the *model* container $configurator->addConfig(__DIR__ . '/../../config/config.local.neon'); // parameters that are needed for the *model* container $configurator->addStaticParameters([ // provide the %modelDir% parameter so the model is self-aware of where it lays 'modelDir' => __DIR__ . '/../' ]); return $configurator; } }
In the user DI container, register this newly created extension:
# the app-container.neon # import public services from model-level container # see Model/**/*.neon files for more info extensions: model: Ivy\Infrastructure\ModelExtension
Exporting a service from the model container
# model-container.neon services: - factory: Ivy\Survey\SurveyFacade( # dependencies... ) tags: [exported] # This makes it available for the outer container
Requiring a dependency of the model container
When the model container needs a dependency (EmailSender), it can require it by defining the service torso.
namespace App\Model\Infrastructure\Email; interface EmailSender { function send(TheMessage $message): void; }
Then tell the DI that the outer container MUST provide the EmailSender.
# model-container.neon services: - type: App\Model\Infrastructure\Email\EmailSender factory: @outerContainer::getByType(Ivy\Infrastructure\Email\EmailSender)
The outer container then registers the service, and you are done.
services: - MyEmailSender # implements App\Model\Infrastructure\Email\EmailSender
Danger zone
Sometimes, you need to break the pattern of being insulated from the model. See danger zone.
Contributing
This repository is fork of mangoweb-backend/nette-di-scope.
Primary development place is gitlab.grifart.cz. master is automatically replicated back to github:grifart/nette-di-scope.
Submitting contribution into fork
Internal contributions
Use GitLab and open merge request, target: master. After merge it will be automatically propagated to GitHub.
External contributions
External developers: Use GitHub, open pull request targeting our master master.
Internal reviewer and maintainer of pull request:
- fetch changes to local repo
- then merge into local
master - push local master into
gitlab/master(this will be automatically propagated to GitHub) - remove branches that are not needed anymore
Recommended git remotes setup
git remote add origin https://gitlab.grifart.cz/grifart/nette-di-scope.git git remote add github-grifart https://github.com/grifart/nette-di-scope.git git remote add github-mangoweb https://github.com/mangoweb-backend/nette-di-scope.git
grifart/nette-scoped-di 适用场景与选型建议
grifart/nette-scoped-di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.14k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 grifart/nette-scoped-di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 grifart/nette-scoped-di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-04