webiik/container
Composer 安装命令:
composer require webiik/container
包简介
The Container adds handy methods to most common Pimple functions and then adds automatic injection of dependencies from container to class constructor.
README 文档
README
Container
The Container adds handy methods to most common Pimple functions and then adds automatic injection of dependencies from container to class constructor.
Installation
composer require webiik/container
Example
$container = new \Webiik\Container\Container(); $container->addService('\Webiik\Arr\Arr', function () { return new \Webiik\Arr\Arr(); }); $array = $container->get('\Webiik\Arr\Arr');
Adding
addService
addService(string $name, callable $factory): void
addService() ads service factory to container. It returns always same instance of service.
$container->addService('\Webiik\Arr\Arr', function () { return new \Webiik\Arr\Arr(); });
If you need to access container inside $factory:
$container->addService('Service', function ($container) { // $container - Container });
addServiceFactory
addServiceFactory(string $name, callable $factory): void
addServiceFactory() ads service factory to container. It returns always new instance of service.
$container->addService('\Webiik\Arr\Arr', function () { return new \Webiik\Arr\Arr(); });
addParam
addParam(string $name, $val): void
addParam() ads parameter to container.
$container->addParam('foo', 'bar');
addFunction
addFunction(string $name, callable $function): void
addFunction() ads function to container.
$container->addFunction('myFn', function ($a, $b) { return $a * $b; });
Check
isIn
isIn(string $name): bool
isIn() checks if service, parameter or function is stored in container.
$container->isIn('\Webiik\Arr\Arr');
Getting
get
get(string $name)
get() returns service, parameter or function from container.
$array = $container->get('\Webiik\Arr\Arr');
Dependency Injection
Container provides automatic dependency injection from Container to class controller using the method injectTo(string $className): array. However it requires to follow these naming conventions:
Inject Service by Class Name
- Add service with same name as full name of underlying class:
$container->addService('\Webiik\Arr\Arr', function () { return new \Webiik\Arr\Arr(); });
- Use full class name as type parameter in controller in your class:
public function __construct(\Webiik\Arr\Arr $array) { $this->array = $array; }
Container will search for service with name
\Webiik\Arr\Arr. - Inject dependencies to class:
$myClass = new MyClass(...$container->injectTo('MyClass'));
Inject Service by Service Name
- Add service with name matching the following regex
ws[A-Z]:$container->addService('wsArray', function () { return new \Webiik\Arr\Arr(); });
- Add class name alias to your class:
use Webiik\Arr\Arr as wsArray;
- Add doc block with parameter type to controller of your class:
/** * @param wsArray $array */ public function __construct(wsArray $array) { $this->array = $array; }
Container will search for service with name
wsArray. - Inject dependencies from container to your class:
$myClass = new MyClass(...$container->injectTo('MyClass'));
Inject Function or Parameter
- Add parameter with any name:
$container->addFunction('myFnName', function() { echo 'Hello!'; });
- Use parameter name in constructor in your class:
public function __construct($myFnName) { $myFnName(); // Hello }
Container will search for parameter with name
myParamName. - Inject dependencies from container to your class:
$myClass = new MyClass(...$container->injectTo('MyClass'));
Inject Container Itself
- Use full Container class name as type parameter in constructor in your class:
public function __construct(\Webiik\Container\Container $container) { $this->container = $container; }
- Inject dependencies from container to your class:
$myClass = new MyClass(...$container->injectTo('MyClass'));
Resources
统计信息
- 总下载量: 70
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-04