|
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\SPI\Variation\VariationHandler; |
|
12
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Persistence Cache layer for AliasGenerator. |
|
16
|
|
|
*/ |
|
17
|
|
|
class AliasGeneratorDecorator implements VariationHandler |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \eZ\Publish\SPI\Variation\VariationHandler |
|
21
|
|
|
*/ |
|
22
|
|
|
private $aliasGenerator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Psr\Cache\CacheItemPoolInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $cache; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param \eZ\Publish\SPI\Variation\VariationHandler $aliasGenerator |
|
31
|
|
|
* @param \Psr\Cache\CacheItemPoolInterface $cache |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(VariationHandler $aliasGenerator, CacheItemPoolInterface $cache) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->aliasGenerator = $aliasGenerator; |
|
36
|
|
|
$this->cache = $cache; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Field $field |
|
41
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
|
42
|
|
|
* @param string $variationName |
|
43
|
|
|
* @param array $parameters |
|
44
|
|
|
* |
|
45
|
|
|
* @return \eZ\Publish\SPI\Variation\Values\Variation |
|
46
|
|
|
* |
|
47
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getVariation(Field $field, VersionInfo $versionInfo, $variationName, array $parameters = []) |
|
50
|
|
|
{ |
|
51
|
|
|
$item = $this->cache->getItem($this->getCacheKey($field, $versionInfo, $variationName)); |
|
52
|
|
|
$image = $item->get(); |
|
53
|
|
|
if (!$item->isHit()) { |
|
54
|
|
|
$image = $this->aliasGenerator->getVariation($field, $versionInfo, $variationName, $parameters); |
|
55
|
|
|
$item->set($image); |
|
56
|
|
|
$this->cache->save($item); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $image; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Field $field |
|
64
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
|
65
|
|
|
* @param string $variationName |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
private function getCacheKey(Field $field, VersionInfo $versionInfo, $variationName) |
|
70
|
|
|
{ |
|
71
|
|
|
return sprintf( |
|
72
|
|
|
'ez-image-variation-%d-%d-%s-%s', |
|
73
|
|
|
$versionInfo->getContentInfo()->id, |
|
74
|
|
|
$versionInfo->id, |
|
75
|
|
|
$field->id, |
|
76
|
|
|
$variationName |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|