-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
AbstractInjector.php
75 lines (61 loc) · 1.84 KB
/
AbstractInjector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* @see https://github.com/zendframework/zend-di for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-di/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Di\CodeGenerator;
use Psr\Container\ContainerInterface;
use Zend\Di\DefaultContainer;
use Zend\Di\InjectorInterface;
use function is_string;
/**
* Abstract class for code generated dependency injectors
*/
abstract class AbstractInjector implements InjectorInterface
{
/**
* @var string|FactoryInterface[]
*/
protected $factories = [];
/**
* @var ContainerInterface
*/
private $container;
/**
* @var InjectorInterface
*/
private $injector;
/**
* {@inheritDoc}
*/
public function __construct(InjectorInterface $injector, ContainerInterface $container = null)
{
$this->injector = $injector;
$this->container = $container ?: new DefaultContainer($this);
$this->loadFactoryList();
}
/**
* Init factory list
*/
abstract protected function loadFactoryList() : void;
private function getFactory($type) : FactoryInterface
{
if (is_string($this->factories[$type])) {
$factory = $this->factories[$type];
$this->factories[$type] = new $factory();
}
return $this->factories[$type];
}
public function canCreate(string $name) : bool
{
return (isset($this->factories[$name]) || $this->injector->canCreate($name));
}
public function create(string $name, array $options = [])
{
if (isset($this->factories[$name])) {
return $this->getFactory($name)->create($this->container, $options);
}
return $this->injector->create($name, $options);
}
}