exts/canister
Composer 安装命令:
composer require exts/canister
包简介
PHP 7 dependency autowiring psr-11 container
README 文档
README
Canister is a psr-11 auto-wiring container for PHP 7. Will add examples later.
Notes
- All classes that aren't in the container are automatically resolved if they exist
- All resolved classes are shared by default
- Reflector uses a simple array cache by default, but can be overridden by using a PSR-16 simple cache interface when passed to the constructor of a new reflector instance
- Container is an implementation of
ArrayAccessso you can do pretty much what you would normally do usingArrayAccess - You can define/override default values for class instances when the code tries to resolve it from the container.
- The get method checks in this order when calling the
getmethod from the container: container, factory callables, shared callables, then attempts to resolve any classes if they exists and attempt to grab them from the reflector's cache first then resolve it automatically. If all else fails it'll returnNULL
Examples
Below are simple examples of it's usage.
Basic instantiation
use Canister; $container = new Canister;
Storing basic data into the container
$container['my-key-value'] = 'accessed anywhere'; echo $container->get('my-key-value'); // or $container['my-key-value'];
Auto-Resolving a class
class Example { public function foo() { return 'bar'; } } $example = $container->get(Example::class); echo $example->foo(); // -> 'bar'
Creating an alias to a class
$container->alias(ExampleInterface::class, Example::class); $example = $container->get(ExampleInterface::class); echo $example->foo(); // -> 'bar'
This also works for automatically passing dependencies to classes
class Test implements TestInterface { public function example() { return 'example'; } } class TestExample { public function __construct(TestInterface $test) { $this->test = $test; } public function testing() { return $this->test->example(); } } $container->alias(TestInterface::class, Test::class); $test_example = $container->get(TestExample::class); echo $test_example->testing(); // -> 'example'
Define class as a factory
Everytime you call FactoryClass it'll be a new instance instead of being shared by default.
$container->factory(FactoryClass::class);
Define factory callable
Dependencies to callables are also auto resolved by default, so you can access the container directly because it's automatically passed to the container by default.
$container->factory(FactoryClass::class, function() { //... });
Define shared callable
Want to do the same thing instead store the value instead of creating new instances every call? Well replace factory with shared and you get the same functionality.
(Note: since auto resolution is shared by default, you cannot pass a class name by itself like you can with the factory method)
Defining default parameters for callables & classes
For callables it's as simple as
$container->share('example', function($foo, $bar) { return $foo + $bar; }); $container->define('example', [ 'foo' => val(5), 'bar' => val(21) ]); echo $container->get('example');
The first parameter is the class name or callable name that we're trying to define parameters for. The second parameter is an array used to match the parameters we want to define.
You don't have to order them in any order, just need to know the variable name and make that as the key.
Global Definition functions
There's two global definition functions inside the Canister namespace called val (or Canister\val()) and bag or (Canister\bag()).
The val function is used to tell the resolution method that we're using a raw value.
The bag function is used to tell the resolution method that we should check the container for this value.
Other Notes
- You can resolve php classes as well as define their value too.
$container = new Canister; $container->define(\PDO::class, [ 'dsn' => val('sqlite::memory:'), ]); $pdo = $container->get(\PDO::class); echo is_a($pdo, \PDO::class) ? 'true' : 'false'; // -> true
exts/canister 适用场景与选型建议
exts/canister 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 exts/canister 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 exts/canister 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 42
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2017-05-17