|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace eZ\Publish\Core\Persistence\Cache; |
|
8
|
|
|
|
|
9
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException; |
|
10
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
|
11
|
|
|
use eZ\Publish\SPI\Persistence\Content\UrlWildcard; |
|
12
|
|
|
use eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler as UrlWildcardHandlerInterface; |
|
13
|
|
|
|
|
14
|
|
|
class UrlWildcardHandler extends AbstractHandler implements UrlWildcardHandlerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Constant used for storing not found results for lookup(). |
|
18
|
|
|
*/ |
|
19
|
|
|
private const NOT_FOUND = 0; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @see \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler::create |
|
23
|
|
|
*/ |
|
24
|
|
|
public function create($sourceUrl, $destinationUrl, $forward = false) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->logger->logCall( |
|
27
|
|
|
__METHOD__, |
|
28
|
|
|
[ |
|
29
|
|
|
'sourceUrl' => $sourceUrl, |
|
30
|
|
|
'destinationUrl' => $destinationUrl, |
|
31
|
|
|
'forward' => $forward, |
|
32
|
|
|
] |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
$urlWildcard = $this->persistenceHandler->urlWildcardHandler()->create($sourceUrl, $destinationUrl, $forward); |
|
36
|
|
|
|
|
37
|
|
|
$this->cache->invalidateTags(['urlWildcard-notFound']); |
|
38
|
|
|
|
|
39
|
|
|
return $urlWildcard; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @see \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler::remove |
|
44
|
|
|
*/ |
|
45
|
|
|
public function remove($id) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->logger->logCall(__METHOD__, ['id' => $id]); |
|
48
|
|
|
|
|
49
|
|
|
$this->persistenceHandler->urlWildcardHandler()->remove($id); |
|
50
|
|
|
|
|
51
|
|
|
$this->cache->invalidateTags(['ez-urlWildcard-id-' . $id]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @see \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler::load |
|
56
|
|
|
*/ |
|
57
|
|
|
public function load($id) |
|
58
|
|
|
{ |
|
59
|
|
|
$cacheItem = $this->cache->getItem('ez-urlWildcard-id-' . $id); |
|
60
|
|
|
|
|
61
|
|
|
if ($cacheItem->isHit()) { |
|
62
|
|
|
$this->logger->logCacheHit(['id' => $id]); |
|
63
|
|
|
|
|
64
|
|
|
return $cacheItem->get(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->logger->logCacheMiss(['id' => $id]); |
|
68
|
|
|
|
|
69
|
|
|
$urlWildcard = $this->persistenceHandler->urlWildcardHandler()->load($id); |
|
70
|
|
|
|
|
71
|
|
|
$cacheItem->set($urlWildcard); |
|
72
|
|
|
$cacheItem->tag($this->getCacheTags([$urlWildcard])); |
|
73
|
|
|
$this->cache->save($cacheItem); |
|
74
|
|
|
|
|
75
|
|
|
return $urlWildcard; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @see \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler::loadAll |
|
80
|
|
|
*/ |
|
81
|
|
|
public function loadAll($offset = 0, $limit = -1) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->logger->logCall(__METHOD__, ['offset' => $offset, 'limit' => $limit]); |
|
84
|
|
|
|
|
85
|
|
|
return $this->persistenceHandler->urlWildcardHandler()->loadAll($offset, $limit); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @see \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler::lookup |
|
90
|
|
|
*/ |
|
91
|
|
|
public function translate(string $sourceUrl): UrlWildcard |
|
92
|
|
|
{ |
|
93
|
|
|
$cacheItem = $this->cache->getItem('ez-urlWildcard-source-' . $this->escapeForCacheKey($sourceUrl)); |
|
94
|
|
|
|
|
95
|
|
|
if ($cacheItem->isHit()) { |
|
96
|
|
|
$this->logger->logCacheHit(['url' => $sourceUrl]); |
|
97
|
|
|
|
|
98
|
|
|
if (($return = $cacheItem->get()) === self::NOT_FOUND) { |
|
99
|
|
|
throw new NotFoundException('UrlWildcard', $sourceUrl); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $return; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->logger->logCacheMiss(['url' => $sourceUrl]); |
|
106
|
|
|
|
|
107
|
|
|
try { |
|
108
|
|
|
$urlWildcard = $this->persistenceHandler->urlWildcardHandler()->translate($sourceUrl); |
|
109
|
|
|
} catch (APINotFoundException $e) { |
|
110
|
|
|
$cacheItem->set(self::NOT_FOUND) |
|
111
|
|
|
->expiresAfter(30) |
|
112
|
|
|
->tag(['urlWildcard-notFound']); |
|
113
|
|
|
$this->cache->save($cacheItem); |
|
114
|
|
|
throw new NotFoundException('UrlWildcard', $sourceUrl, $e); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$cacheItem->set($urlWildcard); |
|
118
|
|
|
$cacheItem->tag($this->getCacheTags([$urlWildcard])); |
|
119
|
|
|
$this->cache->save($cacheItem); |
|
120
|
|
|
|
|
121
|
|
|
return $urlWildcard; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @see \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler::exactSourceUrlExists() |
|
126
|
|
|
*/ |
|
127
|
|
|
public function exactSourceUrlExists(string $sourceUrl): bool |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->persistenceHandler->urlWildcardHandler()->exactSourceUrlExists($sourceUrl); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\UrlWildcard[] $urlWildcards |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
private function getCacheTags(array $urlWildcards): array |
|
137
|
|
|
{ |
|
138
|
|
|
$tags = []; |
|
139
|
|
|
|
|
140
|
|
|
foreach ($urlWildcards as $urlWildcard) { |
|
141
|
|
|
$tags[] = 'ez-urlWildcard-id-' . $urlWildcard->id; |
|
142
|
|
|
$tags[] = 'ez-urlWildcard-source-' . $this->escapeForCacheKey($urlWildcard->sourceUrl); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $tags; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|