1 | <?php |
||
25 | final class QueryRepository implements QueryRepositoryInterface |
||
26 | { |
||
27 | /** @var ResourceStorageInterface */ |
||
28 | private $storage; |
||
29 | |||
30 | /** @var Reader */ |
||
31 | private $reader; |
||
32 | |||
33 | /** @var Expiry */ |
||
34 | private $expiry; |
||
35 | |||
36 | /** @var EtagSetterInterface */ |
||
37 | 29 | private $setEtag; |
|
38 | |||
39 | public function __construct( |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | * |
||
54 | 27 | * @throws ReflectionException |
|
55 | */ |
||
56 | 27 | public function put(ResourceObject $ro) |
|
57 | 27 | { |
|
58 | 27 | $ro->toString(); |
|
59 | 27 | $httpCache = $this->getHttpCacheAnnotation($ro); |
|
60 | 27 | $cacheable = $this->getCacheableAnnotation($ro); |
|
61 | 26 | ($this->setEtag)($ro, null, $httpCache); |
|
62 | 26 | $lifeTime = $this->getExpiryTime($ro, $cacheable); |
|
63 | if (isset($ro->headers['ETag'])) { |
||
64 | 26 | $this->storage->updateEtag($ro, $lifeTime); |
|
65 | 26 | } |
|
66 | 2 | ||
67 | $this->setMaxAge($ro, $lifeTime); |
||
68 | if ($cacheable instanceof Cacheable && $cacheable->type === 'view') { |
||
69 | 24 | return $this->saveViewCache($ro, $lifeTime); |
|
70 | } |
||
71 | |||
72 | return $this->storage->saveValue($ro, $lifeTime); |
||
73 | } |
||
74 | |||
75 | 25 | /** |
|
76 | * {@inheritdoc} |
||
77 | 25 | */ |
|
78 | 25 | public function get(AbstractUri $uri) |
|
79 | 23 | { |
|
80 | $data = $this->storage->get($uri); |
||
81 | 15 | ||
82 | 15 | if ($data === false) { |
|
83 | return false; |
||
84 | 15 | } |
|
85 | |||
86 | $age = time() - strtotime($data[2]['Last-Modified']); |
||
87 | $data[2]['Age'] = $age; |
||
88 | |||
89 | return $data; |
||
90 | 12 | } |
|
91 | |||
92 | 12 | /** |
|
93 | * {@inheritdoc} |
||
94 | 12 | */ |
|
95 | public function purge(AbstractUri $uri) |
||
101 | |||
102 | 27 | /** |
|
103 | 27 | * @throws ReflectionException |
|
104 | 27 | */ |
|
105 | private function getHttpCacheAnnotation(ResourceObject $ro): ?HttpCache |
||
109 | |||
110 | /** |
||
111 | * @return ?Cacheable |
||
112 | * |
||
113 | * @throws ReflectionException |
||
114 | */ |
||
115 | 27 | private function getCacheableAnnotation(ResourceObject $ro): ?Cacheable |
|
119 | 27 | ||
120 | private function getExpiryTime(ResourceObject $ro, ?Cacheable $cacheable = null): int |
||
132 | |||
133 | private function getExpiryAtSec(ResourceObject $ro, Cacheable $cacheable): int |
||
146 | 2 | ||
147 | /** |
||
148 | * @return void |
||
149 | 22 | */ |
|
150 | private function setMaxAge(ResourceObject $ro, int $age) |
||
175 | |||
176 | 3 | private function saveViewCache(ResourceObject $ro, int $lifeTime): bool |
|
184 | } |
||
185 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: