gixx/worstpractice-dependency-injection
Composer 安装命令:
composer require gixx/worstpractice-dependency-injection
包简介
A simple dependency injection container
README 文档
README
DIY Dependency Injection
The complete source code for the series of articles DIY Dependency Injection Container part 1, 2 and 3.
Purpose
The only purpose is practicing:
- PHP 7.4, 8.0, 8.1 and 8.2 features
- keep coding standards
- write clean code
- write strict-typed code
- Unit testing
Although, I believe it works like any other DIC, I don't recommend to use it in production. Unfortunately the code quality checks on the Scrutinizer CI side don't yet support PHP 8.2.
Installation
To add this package to your project, just get it via composer:
composer require gixx/worstpractice-dependency-injection
Usage
To use it, you will need only a configuration as in the example:
namespace MyNamespace; use WorstPractice\Component\DependencyInjection\ConfigParser\ArrayParser; use WorstPractice\Component\DependencyInjection\ServiceLibrary; use WorstPractice\Component\DependencyInjection\Container; $config = [ 'ServiceAlias' => [ 'class' => \Namespace\To\MyClass::class, 'arguments' => [ \Namespace\To\OtherClassInterface::class, 'literalParameter' => 1234, 'otherLiteralParameter' => false ], 'shared' => false ], \Namespace\To\OtherClassInterface::class => [ 'class' => \Namespace\To\OtherClass::class, 'shared' => true ], \DateTimeZone::class => [ 'arguments' => [ 'param' => 'Europe/Berlin' ], 'shared' => true ], \DateTime::class => [ 'calls' => [ ['setTimezone', [\DateTimeZone::class]] ], 'shared' => true ], 'Auth' => [ // empty, will be determined later ], 'OtherServiceAlias' => [ 'inherits' => 'ServiceAlias', 'calls' => [ ['someMethod', ['parameter1' => 4543, 'parameter2' => [0, 1, 2], \DateTime::class]] ], 'shared' => true ], 'LoginController' => [ 'class' => \Namespace\To\Controller\Login:class, 'arguments' => [ 'Auth', 'OtherServiceAlias' ] ] ]; $container = new Container(new ServiceLibrary(new ArrayParser()), $config); $authService = $_ENV['environment'] === 'dev' ? new \Namespace\To\DebugAuthService() : new \Namepace\To\Strict\AuthenticationService(); $isShared = true; $container->set('Auth', $authService, $isShared); $controller = $container->get('LoginController');
This DIC will instantiate any class only when it is requested or being referenced by a requested class. It also supports adding instance into the DIC. It comes in handy when the script needs an instance based on some calculation.
Testing
The package contains a simple Docker setup to be able to run tests. For this you need only run the following:
docker-compose up -d
docker exec -it worstpractice-dependency-injection-php-fpm php composer.phar install
docker exec -it worstpractice-dependency-injection-php-fpm php composer.phar check
The following tests will run:
- PHP lint
- PHP Mess Detector
- PHP Unit
- PHPStan (max level, no ignored Errors)
The following tests form the PHP 7.4 version are now ignored, since they don't yet have PHP 8.2 support:
- PHP-CS-Fixer
- PHP Code Sniffer
gixx/worstpractice-dependency-injection 适用场景与选型建议
gixx/worstpractice-dependency-injection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 gixx/worstpractice-dependency-injection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gixx/worstpractice-dependency-injection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-21

