1 | <?php |
||
14 | class DiAbstractFactory implements AbstractFactoryInterface |
||
15 | { |
||
16 | use ServiceTrait; |
||
17 | |||
18 | /** @var array */ |
||
19 | protected $config; |
||
20 | |||
21 | /** @var ChangeSet|mixed */ |
||
22 | protected $changeSet; |
||
23 | |||
24 | /** @var ExtractorInterface */ |
||
25 | protected $extractor; |
||
26 | |||
27 | /** @var InstantiatorInterface */ |
||
28 | protected $instantiator; |
||
29 | |||
30 | /** @var StorageInterface|AbstractAdapter */ |
||
31 | protected $cache; |
||
32 | |||
33 | 11 | public function __construct(InstantiatorInterface $instantiator = null) |
|
37 | |||
38 | /** |
||
39 | * Determine if we can create a service with name |
||
40 | * |
||
41 | * @param ServiceLocatorInterface $serviceLocator |
||
42 | * @param string $name |
||
43 | * @param string $requestedName |
||
44 | * @return bool |
||
45 | */ |
||
46 | 4 | public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
|
47 | { |
||
48 | 4 | if ($this->shouldBeAvoided($serviceLocator, $name)) { |
|
49 | 1 | return false; |
|
50 | } |
||
51 | |||
52 | 3 | $this->initializeCache($serviceLocator); |
|
53 | 3 | $this->setChangeSet($this->cache->getItem($this->getHash($name))); |
|
54 | |||
55 | 3 | if ($this->getChangeSet() instanceof ChangeSet) { |
|
56 | // Positive result available via cache |
||
57 | // Because we don't allow not annotated results to be set there |
||
58 | 1 | return true; |
|
59 | } |
||
60 | |||
61 | // Result is not available via cache |
||
62 | // Calculate the result first |
||
63 | 2 | $this->initializeExtractor($serviceLocator); |
|
64 | 2 | $this->setChangeSet($this->extractor->getChangeSet($requestedName)); |
|
65 | |||
66 | 2 | if ($this->getChangeSet()->isAnnotated()) { |
|
67 | // Service is annotated to cache results |
||
68 | 1 | $this->cache->setItem($this->getHash($name), $this->getChangeSet()); |
|
69 | 1 | return true; |
|
70 | } |
||
71 | |||
72 | // Service is not annotated |
||
73 | // Cache false for it |
||
74 | 1 | $this->cache->setItem($this->getHash($name), false); |
|
75 | 1 | return false; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Create service with name |
||
80 | * |
||
81 | * @param ServiceLocatorInterface $serviceLocator |
||
82 | * @param string $name |
||
83 | * @param string $requestedName |
||
84 | * @return object |
||
85 | */ |
||
86 | 1 | public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
|
90 | |||
91 | /** |
||
92 | * @param ServiceLocatorInterface $serviceLocator |
||
93 | * @param string $name |
||
94 | * @return bool |
||
95 | */ |
||
96 | 4 | protected function shouldBeAvoided(ServiceLocatorInterface $serviceLocator, $name) |
|
97 | { |
||
98 | 4 | if (! $this->config) { |
|
99 | 4 | $this->config = $serviceLocator->get('config')['mxdimodule']['avoid_service']; |
|
100 | 4 | } |
|
101 | |||
102 | 4 | return isset($this->config[$name]) && $this->config[$name]; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param ServiceLocatorInterface $serviceLocator |
||
107 | */ |
||
108 | 2 | protected function initializeExtractor(ServiceLocatorInterface $serviceLocator) |
|
109 | { |
||
110 | 2 | if (!$this->extractor) { |
|
111 | $this->extractor = $serviceLocator->get('mxdiModule\Extractor'); |
||
112 | } |
||
113 | 2 | } |
|
114 | |||
115 | /** |
||
116 | * @param ServiceLocatorInterface $serviceLocator |
||
117 | */ |
||
118 | 3 | protected function initializeCache(ServiceLocatorInterface $serviceLocator) |
|
119 | { |
||
120 | 3 | if (!$this->cache) { |
|
121 | 3 | $this->cache = $serviceLocator->get('mxdiModule\Cache'); |
|
122 | 3 | } |
|
123 | 3 | } |
|
124 | |||
125 | /** |
||
126 | * @return mixed|ChangeSet |
||
127 | */ |
||
128 | 4 | public function getChangeSet() |
|
132 | |||
133 | /** |
||
134 | * @param mixed|ChangeSet $changeSet |
||
135 | */ |
||
136 | 4 | public function setChangeSet($changeSet) |
|
140 | |||
141 | /** |
||
142 | * @param ExtractorInterface $extractor |
||
143 | */ |
||
144 | 5 | public function setExtractor(ExtractorInterface $extractor) |
|
148 | } |
||
149 |