sasa-b/container
Composer 安装命令:
composer require sasa-b/container
包简介
Lightweight dependency injection container with laravel like autowiring, interface/abstract class binding and contextual binding. PSR-11 compliant.
README 文档
README
Lightweight dependency injection container with laravel like autowiring, interface/abstract class binding and contextual binding. PSR-11 compliant.
To use the library clone the repo or just use composer to install it.
composer require sasa-b/container
Usage examples
require '../vendor/autoload.php'; $container = new Foundation\Container\Container();`
Binding/Registering services
A service (bound value) can be a string representation of a class, an anonymous function which returns an object instance, or an object instance itself. If you bind an object instance itself, that service will essentially act as a singleton because the same instance will always be returned.
$container->bind(Foo\Bar::class, Foo\Bar::class); $container->bind(Foo\Bar::class, function ($container) { return new \Foo\Bar($container['Foo\Baz']); }); $container->bind('Foo\Bar', FooBar::class); $container->bind(Foo\Bar::class, new \Foo\Bar());
Singletons
$container->bind('Foo\Bar', FooBar::class)->share(); //or $container->bind('Foo\Bar', FooBar::class); $container->share(Foo\Bar::class); $container->bind(Foo\Baz::class, Foo\Baz::class)->mapTo('baz'); $container->share('baz');
Mapping services to keys
For convenience you can map your services to keys and there are multiple ways of doing it.
- With mapTo method via method chaining
$container->bind(Foo\Bar::class, Foo\Bar::class)->mapTo('foo');
- With the key method
$container->key('foo', Foo\Bar::class);
- Directly with the bind statement
$container->bind('foo', function ($container) { return new Foo\Bar(); });
When you directly bind services to a key, they will only be accessible by that key and those services can't be used for autowiring (injecting by type hinting), unless the service you are binding/registering is a string representaton of a class, in that case the service will automatically be mapped to the given key.
In case of mapTo and key bound/registered services are accessible by both the key and their class name, and because of that they can be used for autowiring.
Binding/Registering multiple services
For registering multiple services at once you can use the register method or it's alias bindMany.
$container->register([ Foundation\Request\Http::class => Foundation\Request\Http::class, Foundation\Sessions\SessionManager::class => \Foundation\Sessions\SessionManager::class, Foundation\Request\Cookie::class => Foundation\Request\Cookie::class, 'date' => \DateTime::class, Foundation\Core\Database::class => Foundation\Core\Database::class, Foundation\Database\QueryBuilder::class => (function ($c) { return new \Foundation\Database\PDOQuery($c['db']); }) ])->mapTo(['request', 'session', 'cookie', 'db']); $container->register([ Foundation\Request\Http::class => Foundation\Request\Http::class, Foundation\Sessions\SessionManager::class => \Foundation\Sessions\SessionManager::class, Foundation\Request\Cookie::class => Foundation\Request\Cookie::class ]); $container->keys([ 'request' => Foundation\Request\Http::class, 'session' => Foundation\Sessions\SessionManager::class, 'cookie' => Foundation\Request\Cookie::class ]);
Registering multiple singletons
/* Only those specified will be registered as singletons */ $container->register([ Foundation\Request\Http::class => Foundation\Request\Http::class, Foundation\Sessions\SessionManager::class => \Foundation\Sessions\SessionManager::class, Foundation\Request\Cookie::class => Foundation\Request\Cookie::class, 'date' => \DateTime::class, Foundation\Core\Database::class => Foundation\Core\Database::class, Foundation\Database\QueryBuilder::class => (function ($c) { return new \Foundation\Database\PDOQuery($c['db']); }) ])->share(['session', 'db', Foundation\Core\Database::class]); /* All will be registered as singletons */ $container->register([ Foundation\Request\Http::class => Foundation\Request\Http::class, Foundation\Sessions\SessionManager::class => \Foundation\Sessions\SessionManager::class, Foundation\Request\Cookie::class => Foundation\Request\Cookie::class, 'date' => \DateTime::class, Foundation\Core\Database::class => Foundation\Core\Database::class, Foundation\Database\QueryBuilder::class => (function ($c) { return new \Foundation\Database\PDOQuery($c['db']); }) ])->share(); // or $container->register([ Foundation\Request\Http::class => Foundation\Request\Http::class, Foundation\Sessions\SessionManager::class => \Foundation\Sessions\SessionManager::class, Foundation\Request\Cookie::class => Foundation\Request\Cookie::class, 'date' => \DateTime::class, Foundation\Core\Database::class => Foundation\Core\Database::class, Foundation\Database\QueryBuilder::class => (function ($c) { return new \Foundation\Database\PDOQuery($c['db']); }) ], true);
Abstract binding
You can bind/register services to interfaces and abstract classes as well. These abstract bindings are convenient when used with autowiring, because you can type hint with abstractions (abstract classes and interfaces) and not concretions (implementations).
$container->bind(Foo\BarInterface::class, Foo\Bar::class); $container->bind(Foo\AbstractBaz::class, Foo\Baz::class);
Contextual binding
When you want to bind/register multiple different services to a same interface or abstract class you need to provide context otherwise one will override the other. Context is the third parameter to the bind method, and it can be a string representation of a class or a key mapped to a class, and that class needs to be the one in whose constructor or method the interface/abstract class is used as a typehint.
// This will result in an override and `Foo\Baz::class` will always be returned for Foo\Bar::interface $container->bind(Foo\BarInterface::class, Foo\Bar::class); $container->bind(Foo\BarInterface::class, Foo\Baz::class); $container->bind(Foo\BarInterface::class, Foo\Bar::class); $container->bind(Foo\BarInterface::class, Foo\Baz::class, 'FooController');
Retrieveing services
$service = $container['Foo\Bar']; $service = $container[Foo\Bar::class]; $service = $container['foo']; // Only works for services mapped to keys $service = $container->foo(); $service = $container->foo; // Retrieves a new instance or a singleton if a service has been registered as a singleton $service = $container->get('foo'); // Always retrieves a singleton $service = $container->shared('foo');
sasa-b/container 适用场景与选型建议
sasa-b/container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 10 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sasa-b/container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sasa-b/container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-07