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\Bundle\EzPublishCoreBundle\Imagine\Cache; |
8
|
|
|
|
9
|
|
|
use eZ\Publish\API\Repository\Values\Content\Field; |
10
|
|
|
use eZ\Publish\API\Repository\Values\Content\VersionInfo; |
11
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess; |
12
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware; |
13
|
|
|
use eZ\Publish\SPI\Variation\VariationHandler; |
14
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
15
|
|
|
use Symfony\Component\Routing\RequestContext; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Persistence Cache layer for AliasGenerator. |
19
|
|
|
*/ |
20
|
|
|
class AliasGeneratorDecorator implements VariationHandler, SiteAccessAware |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \eZ\Publish\SPI\Variation\VariationHandler |
24
|
|
|
*/ |
25
|
|
|
private $aliasGenerator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Psr\Cache\CacheItemPoolInterface |
29
|
|
|
*/ |
30
|
|
|
private $cache; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \eZ\Publish\Core\MVC\Symfony\SiteAccess |
34
|
|
|
*/ |
35
|
|
|
private $siteAccess; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Symfony\Component\Routing\RequestContext |
39
|
|
|
*/ |
40
|
|
|
private $requestContext; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param \eZ\Publish\SPI\Variation\VariationHandler $aliasGenerator |
44
|
|
|
* @param \Psr\Cache\CacheItemPoolInterface $cache |
45
|
|
|
* @param \Symfony\Component\Routing\RequestContext $requestContext |
46
|
|
|
*/ |
47
|
|
|
public function __construct(VariationHandler $aliasGenerator, CacheItemPoolInterface $cache, RequestContext $requestContext) |
48
|
|
|
{ |
49
|
|
|
$this->aliasGenerator = $aliasGenerator; |
50
|
|
|
$this->cache = $cache; |
51
|
|
|
$this->requestContext = $requestContext; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Field $field |
56
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
57
|
|
|
* @param string $variationName |
58
|
|
|
* @param array $parameters |
59
|
|
|
* |
60
|
|
|
* @return \eZ\Publish\SPI\Variation\Values\Variation |
61
|
|
|
* |
62
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
public function getVariation(Field $field, VersionInfo $versionInfo, $variationName, array $parameters = []) |
65
|
|
|
{ |
66
|
|
|
$item = $this->cache->getItem($this->getCacheKey($field, $versionInfo, $variationName)); |
67
|
|
|
$image = $item->get(); |
68
|
|
|
if (!$item->isHit()) { |
69
|
|
|
$image = $this->aliasGenerator->getVariation($field, $versionInfo, $variationName, $parameters); |
70
|
|
|
$item->set($image); |
71
|
|
|
$this->cache->save($item); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $image; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \eZ\Publish\Core\MVC\Symfony\SiteAccess $siteAccess |
79
|
|
|
*/ |
80
|
|
|
public function setSiteAccess(SiteAccess $siteAccess = null) |
81
|
|
|
{ |
82
|
|
|
$this->siteAccess = $siteAccess; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Field $field |
87
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
88
|
|
|
* @param string $variationName |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
private function getCacheKey(Field $field, VersionInfo $versionInfo, $variationName) |
93
|
|
|
{ |
94
|
|
|
return sprintf( |
95
|
|
|
'ez-image-variation-%s-%s-%s-%d-%d-%d-%s-%s', |
96
|
|
|
$this->siteAccess ? $this->siteAccess->name : 'default', |
97
|
|
|
$this->requestContext->getScheme(), |
98
|
|
|
$this->requestContext->getHost(), |
99
|
|
|
$this->requestContext->getScheme() === 'https' ? $this->requestContext->getHttpsPort() : $this->requestContext->getHttpPort(), |
100
|
|
|
$versionInfo->getContentInfo()->id, |
101
|
|
|
$versionInfo->id, |
102
|
|
|
$field->id, |
103
|
|
|
$variationName |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|