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\Cache\Warmer; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\Cache; |
15
|
|
|
use Sylius\Bundle\ThemeBundle\Locator\ResourceNotFoundException; |
16
|
|
|
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface; |
17
|
|
|
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface; |
18
|
|
|
use Sylius\Bundle\ThemeBundle\Templating\Locator\TemplateLocatorInterface; |
19
|
|
|
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
21
|
|
|
use Symfony\Component\Templating\TemplateReferenceInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Kamil Kokot <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class TemplatePathsCacheWarmer implements CacheWarmerInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var TemplateFinderInterface |
30
|
|
|
*/ |
31
|
|
|
private $templateFinder; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TemplateLocatorInterface |
35
|
|
|
*/ |
36
|
|
|
private $templateLocator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ThemeRepositoryInterface |
40
|
|
|
*/ |
41
|
|
|
private $themeRepository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Cache |
45
|
|
|
*/ |
46
|
|
|
private $cache; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param TemplateFinderInterface $templateFinder |
50
|
|
|
* @param TemplateLocatorInterface $templateLocator |
51
|
|
|
* @param ThemeRepositoryInterface $themeRepository |
52
|
|
|
* @param Cache $cache |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
TemplateFinderInterface $templateFinder, |
56
|
|
|
TemplateLocatorInterface $templateLocator, |
57
|
|
|
ThemeRepositoryInterface $themeRepository, |
58
|
|
|
Cache $cache |
59
|
|
|
) { |
60
|
|
|
$this->templateFinder = $templateFinder; |
61
|
|
|
$this->templateLocator = $templateLocator; |
62
|
|
|
$this->themeRepository = $themeRepository; |
63
|
|
|
$this->cache = $cache; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function warmUp($cacheDir) |
70
|
|
|
{ |
71
|
|
|
$templates = $this->templateFinder->findAllTemplates(); |
72
|
|
|
|
73
|
|
|
/** @var TemplateReferenceInterface $template */ |
74
|
|
|
foreach ($templates as $template) { |
75
|
|
|
$this->warmUpTemplate($template); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function isOptional() |
83
|
|
|
{ |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param TemplateReferenceInterface $template |
89
|
|
|
*/ |
90
|
|
|
private function warmUpTemplate(TemplateReferenceInterface $template) |
91
|
|
|
{ |
92
|
|
|
/** @var ThemeInterface $theme */ |
93
|
|
|
foreach ($this->themeRepository->findAll() as $theme) { |
94
|
|
|
$this->warmUpThemeTemplate($template, $theme); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param TemplateReferenceInterface $template |
100
|
|
|
* @param ThemeInterface $theme |
101
|
|
|
*/ |
102
|
|
|
private function warmUpThemeTemplate(TemplateReferenceInterface $template, ThemeInterface $theme) |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
|
|
$location = $this->templateLocator->locateTemplate($template, $theme); |
106
|
|
|
} catch (ResourceNotFoundException $exception) { |
107
|
|
|
$location = null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->cache->save($this->getCacheKey($template, $theme), $location); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param TemplateReferenceInterface $template |
115
|
|
|
* @param ThemeInterface $theme |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
private function getCacheKey(TemplateReferenceInterface $template, ThemeInterface $theme) |
120
|
|
|
{ |
121
|
|
|
return $template->getLogicalName() . '|' . $theme->getSlug(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|