1 | <?php |
||
2 | |||
3 | namespace Bankiru\Api\Doctrine; |
||
4 | |||
5 | use Bankiru\Api\Doctrine\Cache\CacheConfiguration; |
||
6 | use Bankiru\Api\Doctrine\Cache\CacheConfigurationInterface; |
||
7 | use Bankiru\Api\Doctrine\Type\TypeRegistryInterface; |
||
8 | use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
||
9 | use Psr\Cache\CacheItemPoolInterface; |
||
10 | use Psr\Log\LoggerInterface; |
||
11 | use Psr\Log\NullLogger; |
||
12 | |||
13 | class Configuration |
||
14 | { |
||
15 | /** @var EntityMetadataFactory */ |
||
16 | private $metadataFactory; |
||
17 | /** @var MappingDriver */ |
||
18 | private $driver; |
||
19 | /** @var ClientRegistryInterface */ |
||
20 | private $clientRegistry; |
||
21 | /** @var ApiFactoryRegistryInterface */ |
||
22 | private $factoryRegistry; |
||
23 | /** @var string */ |
||
24 | private $proxyDir; |
||
25 | /** @var string */ |
||
26 | private $proxyNamespace; |
||
27 | /** @var bool */ |
||
28 | private $autogenerateProxies = true; |
||
29 | /** @var TypeRegistryInterface */ |
||
30 | private $typeRegistry; |
||
31 | /** @var array */ |
||
32 | private $cacheConfiguration = []; |
||
33 | /** @var CacheItemPoolInterface */ |
||
34 | private $apiCache; |
||
35 | /** @var LoggerInterface */ |
||
36 | private $apiCacheLogger; |
||
37 | /** @var CacheConfigurationInterface[] */ |
||
38 | private $cacheConfigurationCache = []; |
||
39 | |||
40 | /** |
||
41 | * Configuration constructor. |
||
42 | */ |
||
43 | public function __construct() |
||
44 | { |
||
45 | $this->apiCacheLogger = new NullLogger(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @return ClientRegistryInterface |
||
50 | */ |
||
51 | public function getClientRegistry() |
||
52 | { |
||
53 | return $this->clientRegistry; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param ClientRegistryInterface $clientRegistry |
||
58 | */ |
||
59 | public function setClientRegistry($clientRegistry) |
||
60 | { |
||
61 | $this->clientRegistry = $clientRegistry; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return ApiFactoryRegistryInterface |
||
66 | */ |
||
67 | public function getFactoryRegistry() |
||
68 | { |
||
69 | return $this->factoryRegistry; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param ApiFactoryRegistryInterface $factoryRegistry |
||
74 | */ |
||
75 | public function setFactoryRegistry(ApiFactoryRegistryInterface $factoryRegistry) |
||
76 | { |
||
77 | $this->factoryRegistry = $factoryRegistry; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return LoggerInterface |
||
82 | */ |
||
83 | public function getApiCacheLogger() |
||
84 | { |
||
85 | return $this->apiCacheLogger; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param LoggerInterface $apiCacheLogger |
||
90 | */ |
||
91 | public function setApiCacheLogger(LoggerInterface $apiCacheLogger = null) |
||
92 | { |
||
93 | $this->apiCacheLogger = $apiCacheLogger ?: new NullLogger(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return CacheItemPoolInterface|null |
||
98 | */ |
||
99 | public function getApiCache() |
||
100 | { |
||
101 | return $this->apiCache; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param CacheItemPoolInterface|null $apiCache |
||
106 | */ |
||
107 | public function setApiCache(CacheItemPoolInterface $apiCache = null) |
||
108 | { |
||
109 | $this->apiCache = $apiCache; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Returns class cache configuration. |
||
114 | * |
||
115 | * Checks for parent classes recursively |
||
116 | * |
||
117 | * @param $class |
||
118 | * |
||
119 | * @return CacheConfigurationInterface |
||
120 | */ |
||
121 | public function getCacheConfiguration($class) |
||
122 | { |
||
123 | if ($this->getMetadataFactory()->isTransient($class)) { |
||
124 | return CacheConfiguration::disabled(); |
||
125 | } |
||
126 | |||
127 | if (array_key_exists($class, $this->cacheConfigurationCache)) { |
||
128 | return $this->cacheConfigurationCache[$class]; |
||
129 | } |
||
130 | |||
131 | if ($this->hasCacheConfigurationFor($class)) { |
||
132 | return $this->cacheConfigurationCache[$class] = |
||
133 | CacheConfiguration::create($this->cacheConfiguration[$class]); |
||
134 | } |
||
135 | |||
136 | $metadata = $this->getMetadataFactory()->getMetadataFor($class); |
||
137 | $parent = $metadata->getReflectionClass()->getParentClass(); |
||
138 | while ($parent) { |
||
139 | if ($this->hasCacheConfigurationFor($parent->getName())) { |
||
140 | return $this->cacheConfigurationCache[$class] = $this->getCacheConfiguration($parent->getName()); |
||
141 | } |
||
142 | |||
143 | $parent = $parent->getParentClass(); |
||
144 | } |
||
145 | |||
146 | return $this->cacheConfigurationCache[$class] = CacheConfiguration::disabled(); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param string $class |
||
151 | * @param array $options |
||
152 | */ |
||
153 | public function setCacheConfiguration($class, array $options = null) |
||
154 | { |
||
155 | $this->cacheConfiguration[$class] = $options; |
||
156 | |||
157 | if (null === $this->cacheConfiguration[$class]) { |
||
158 | unset($this->cacheConfiguration[$class]); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | public function hasCacheConfigurationFor($class) |
||
163 | { |
||
164 | return array_key_exists($class, $this->cacheConfiguration); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * |
||
169 | * |
||
170 | * @param string $class |
||
171 | * @param CacheConfigurationInterface $configuration |
||
172 | */ |
||
173 | public function setCacheConfigurationInstance($class, CacheConfigurationInterface $configuration) |
||
174 | { |
||
175 | $this->cacheConfigurationCache[$class] = $configuration; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return TypeRegistryInterface |
||
180 | */ |
||
181 | public function getTypeRegistry() |
||
182 | { |
||
183 | return $this->typeRegistry; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param TypeRegistryInterface $typeRegistry |
||
188 | */ |
||
189 | public function setTypeRegistry(TypeRegistryInterface $typeRegistry) |
||
190 | { |
||
191 | $this->typeRegistry = $typeRegistry; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getProxyDir() |
||
198 | { |
||
199 | return $this->proxyDir; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param string $proxyDir |
||
204 | */ |
||
205 | public function setProxyDir($proxyDir) |
||
206 | { |
||
207 | $this->proxyDir = $proxyDir; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @return string |
||
212 | */ |
||
213 | public function getProxyNamespace() |
||
214 | { |
||
215 | return $this->proxyNamespace; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param string $proxyNamespace |
||
220 | */ |
||
221 | public function setProxyNamespace($proxyNamespace) |
||
222 | { |
||
223 | $this->proxyNamespace = $proxyNamespace; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return boolean |
||
228 | */ |
||
229 | public function isAutogenerateProxies() |
||
230 | { |
||
231 | return $this->autogenerateProxies; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param boolean $autogenerateProxies |
||
236 | */ |
||
237 | public function setAutogenerateProxies($autogenerateProxies) |
||
238 | { |
||
239 | $this->autogenerateProxies = $autogenerateProxies; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @return MappingDriver |
||
244 | */ |
||
245 | public function getDriver() |
||
246 | { |
||
247 | return $this->driver; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param MappingDriver $driver |
||
252 | */ |
||
253 | public function setDriver($driver) |
||
254 | { |
||
255 | $this->driver = $driver; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @return EntityMetadataFactory |
||
260 | */ |
||
261 | public function getMetadataFactory() |
||
262 | { |
||
263 | return $this->metadataFactory; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param string $metadataFactory |
||
268 | */ |
||
269 | public function setMetadataFactory($metadataFactory) |
||
270 | { |
||
271 | $this->metadataFactory = $metadataFactory; |
||
272 | } |
||
273 | } |
||
274 |