clippy/container
Composer 安装命令:
composer require clippy/container
包简介
Dependency-injection container inspired by Pimple and PHP-DI Invoker
README 文档
README
This is a derivative of pimple with a few notable changes.
Function parameter injection
In standard Pimple, the service factories all receive Container $c as input, e.g.
$c['descendent'] = function($c) { return $c['parent']->generateDescendent()->withWhizBang(); };
In Clippy's container:
$c['descendent'] = function(MyParent $parent) { return $parent->generateDescendent()->withWhizBang(); };
This allows the consumers of services to (progressively) use type-hinting.
Service methods
A "service method" is a function which supports both service-injection and runtime data-passing. For example:
$c['getPassword()'] = function ($domain, SymfonyStyle $io) { if (getenv('PASSWORD')) return getenv('PASSWORD'); return $io->askHidden("What is the password for <comment>$domain</comment>?"); } $c['app']->main('', function($getPassword) { $pass = $getPassword('example.com'); });
The first parameter to getPassword is given at runtime ($getPassword('example.com')); the second parameter ($io)
is injected automatically.
The service-method is denoted by including () in the declaration. Compare:
// Standard service $c['foo'] = function($injectedService, ...) { ... } // Service method $c['foo()'] = function($inputtedData, ... , $injectedService, ...) { ... } };
Autowired objects / anonymous service classes
The following allows for using injection with improvised service classes.
$c['basicService'] = 'something'; $c['newService'] = $c->autowiredObject(new class() { protected $basicService; public function double() { return $this->basicService . $this->basicService; } });
Properties (eg $basicService) will be pre-populated with the corresponding services.
In the default strict mode, unmatched properties will produce exceptions. This can be disabled, e.g.
$c['newService'] = $c->autowiredObject(['strict' => FALSE], new class() { ..});
Similarly, you may define a regular service function and use autowiring as part of the logic, e.g.
$c['basicService'] = 'something'; $c['newService'] = function() use ($c) { return $c->autowire(new MyClass()); };
Sigils
In standard Pimple, you may define alternative handling for a callback by using a wrapper method. Clippy supports wrappers as well as a sigil notation.
// Run a function every time one reads `$c['theJoneses]`, with mix of inputs and services $c['getPassword'] = $c->method(function ($domain, SymfonyStyle $io) { ... }); $c['getPassword()'] = function ($domain, SymfonyStyle $io) { ... }; $c['getPassword']('localhost'); $c['getPassword']('example.com'); // Create a new instance every time one reads `$c['theJonses']`: $c['theJoneses'] = $c->factory(function() { return new CoolThing(microtime(1)); }); $c['theJoneses++'] = function() { return new CoolThing(microtime(1)); }; print_r($c['theJoneses']); print_r($c['theJoneses']);
clippy/container 适用场景与选型建议
clippy/container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 95.95k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 clippy/container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 clippy/container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 95.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-08