1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ThemeBundle\Templating\Locator; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\Cache; |
15
|
|
|
use Sylius\Bundle\ThemeBundle\Locator\ResourceNotFoundException; |
16
|
|
|
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface; |
17
|
|
|
use Symfony\Component\Templating\TemplateReferenceInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Kamil Kokot <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class CachedTemplateLocator implements TemplateLocatorInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var TemplateLocatorInterface |
26
|
|
|
*/ |
27
|
|
|
private $decoratedTemplateLocator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Cache |
31
|
|
|
*/ |
32
|
|
|
private $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param TemplateLocatorInterface $decoratedTemplateLocator |
36
|
|
|
* @param Cache $cache |
37
|
|
|
*/ |
38
|
|
|
public function __construct(TemplateLocatorInterface $decoratedTemplateLocator, Cache $cache) |
39
|
|
|
{ |
40
|
|
|
$this->decoratedTemplateLocator = $decoratedTemplateLocator; |
41
|
|
|
$this->cache = $cache; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function locateTemplate(TemplateReferenceInterface $template, ThemeInterface $theme) |
48
|
|
|
{ |
49
|
|
|
$cacheKey = $this->getCacheKey($template, $theme); |
50
|
|
|
if ($this->cache->contains($cacheKey)) { |
51
|
|
|
$location = $this->cache->fetch($cacheKey); |
52
|
|
|
|
53
|
|
|
if (null === $location) { |
54
|
|
|
throw new ResourceNotFoundException($template->getPath(), $theme); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $location; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->decoratedTemplateLocator->locateTemplate($template, $theme); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param TemplateReferenceInterface $template |
65
|
|
|
* @param ThemeInterface $theme |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
private function getCacheKey(TemplateReferenceInterface $template, ThemeInterface $theme) |
70
|
|
|
{ |
71
|
|
|
return $template->getLogicalName() . '|' . $theme->getSlug(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|