|
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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\ThemeBundle\Templating\Locator; |
|
15
|
|
|
|
|
16
|
|
|
use Sylius\Bundle\ThemeBundle\Context\EmptyThemeContext; |
|
17
|
|
|
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface; |
|
18
|
|
|
use Sylius\Bundle\ThemeBundle\HierarchyProvider\NoopThemeHierarchyProvider; |
|
19
|
|
|
use Sylius\Bundle\ThemeBundle\HierarchyProvider\ThemeHierarchyProviderInterface; |
|
20
|
|
|
use Sylius\Bundle\ThemeBundle\Locator\ResourceNotFoundException; |
|
21
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
|
22
|
|
|
use Symfony\Component\Templating\TemplateReferenceInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
* |
|
27
|
|
|
* @author Kamil Kokot <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
final class TemplateFileLocator implements FileLocatorInterface, \Serializable |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var FileLocatorInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $decoratedFileLocator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var ThemeContextInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $themeContext; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ThemeHierarchyProviderInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $themeHierarchyProvider; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var TemplateLocatorInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $templateLocator; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param FileLocatorInterface $decoratedFileLocator |
|
53
|
|
|
* @param ThemeContextInterface $themeContext |
|
54
|
|
|
* @param ThemeHierarchyProviderInterface $themeHierarchyProvider |
|
55
|
|
|
* @param TemplateLocatorInterface $templateLocator |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct( |
|
58
|
|
|
FileLocatorInterface $decoratedFileLocator, |
|
59
|
|
|
ThemeContextInterface $themeContext, |
|
60
|
|
|
ThemeHierarchyProviderInterface $themeHierarchyProvider, |
|
61
|
|
|
TemplateLocatorInterface $templateLocator |
|
62
|
|
|
) { |
|
63
|
|
|
$this->decoratedFileLocator = $decoratedFileLocator; |
|
64
|
|
|
$this->themeContext = $themeContext; |
|
65
|
|
|
$this->themeHierarchyProvider = $themeHierarchyProvider; |
|
66
|
|
|
$this->templateLocator = $templateLocator; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function locate($template, $currentPath = null, $first = true): string |
|
73
|
|
|
{ |
|
74
|
|
|
if (!$template instanceof TemplateReferenceInterface) { |
|
75
|
|
|
throw new \InvalidArgumentException('The template must be an instance of TemplateReferenceInterface.'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$theme = $this->themeContext->getTheme(); |
|
79
|
|
|
$themes = $theme !== null ? $this->themeHierarchyProvider->getThemeHierarchy($theme) : []; |
|
80
|
|
|
foreach ($themes as $theme) { |
|
81
|
|
|
try { |
|
82
|
|
|
return $this->templateLocator->locateTemplate($template, $theme); |
|
83
|
|
|
} catch (ResourceNotFoundException $exception) { |
|
84
|
|
|
// Ignore if resource cannot be found in given theme. |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->decoratedFileLocator->locate($template, $currentPath, $first); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function serialize(): string |
|
95
|
|
|
{ |
|
96
|
|
|
return serialize($this->decoratedFileLocator); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function unserialize($serialized): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->decoratedFileLocator = unserialize($serialized); |
|
105
|
|
|
|
|
106
|
|
|
$this->themeContext = new EmptyThemeContext(); |
|
107
|
|
|
$this->themeHierarchyProvider = new NoopThemeHierarchyProvider(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|