| 1 | <?php |
||
| 9 | final class DependencyProvider implements DependencyInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Provider dependency |
||
| 13 | * |
||
| 14 | * @var Dependency |
||
| 15 | */ |
||
| 16 | private $dependency; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var bool |
||
| 20 | */ |
||
| 21 | private $isSingleton = false; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var mixed |
||
| 25 | */ |
||
| 26 | private $instance; |
||
| 27 | |||
| 28 | 11 | public function __construct(Dependency $dependency) |
|
| 29 | { |
||
| 30 | 11 | $this->dependency = $dependency; |
|
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function register(array &$container, Bind $bind) |
||
| 37 | { |
||
| 38 | $container[(string) $bind] = $bind->getBound(); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | 6 | public function inject(Container $container) |
|
| 45 | { |
||
| 46 | 5 | if ($this->isSingleton && $this->instance) { |
|
| 47 | 1 | return $this->instance; |
|
| 48 | } |
||
| 49 | $this->instance = $this->dependency->inject($container)->get(); |
||
| 50 | |||
| 51 | 6 | return $this->instance; |
|
| 52 | 5 | } |
|
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | 1 | public function setScope($scope) |
|
| 58 | { |
||
| 59 | 1 | if ($scope === Scope::SINGLETON) { |
|
| 60 | 1 | $this->isSingleton = true; |
|
| 61 | } |
||
| 62 | 1 | } |
|
| 63 | |||
| 64 | 1 | public function __sleep() |
|
| 68 | } |
||
| 69 |