|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\CMS\Theme; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use TheCodingMachine\CMS\Block\BlockRendererInterface; |
|
8
|
|
|
use TheCodingMachine\CMS\RenderableInterface; |
|
9
|
|
|
use TheCodingMachine\CMS\Theme\Extensions\ThemeExtension; |
|
10
|
|
|
|
|
11
|
|
|
class TwigThemeFactory implements ThemeFactoryInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Twig_Environment |
|
15
|
|
|
*/ |
|
16
|
|
|
private $twig; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var BlockRendererInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $blockRenderer; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $themesPath; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $themesUrl; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(\Twig_Environment $twig, BlockRendererInterface $blockRenderer, string $themesPath, string $themesUrl) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->twig = $twig; |
|
33
|
|
|
$this->blockRenderer = $blockRenderer; |
|
34
|
|
|
$this->themesPath = rtrim($themesPath, '/').'/'; |
|
35
|
|
|
$this->themesUrl = rtrim($themesUrl, '/').'/'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Creates a theme object based on the descriptor object passed in parameter. |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \TheCodingMachine\CMS\Theme\CannotHandleThemeDescriptorExceptionInterface Throws an exception if the factory cannot handle this descriptor. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function createTheme(ThemeDescriptorInterface $descriptor): RenderableInterface |
|
44
|
|
|
{ |
|
45
|
|
|
if (!$descriptor instanceof TwigThemeDescriptor) { |
|
46
|
|
|
throw CannotHandleThemeDescriptorException::cannotHandleDescriptorClass(get_class($descriptor)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Let's configure / customize the Twig environment! |
|
50
|
|
|
$config = $descriptor->getConfig(); |
|
51
|
|
|
if (isset($config['theme'])) { |
|
52
|
|
|
$twig = clone $this->twig; |
|
53
|
|
|
$twig->setLoader(new \Twig_Loader_Filesystem($this->themesPath.ltrim($config['theme'], '/'))); |
|
54
|
|
|
$themeUrl = $this->themesUrl.ltrim($config['theme'], '/'); |
|
55
|
|
|
} else { |
|
56
|
|
|
$twig = $this->twig; |
|
57
|
|
|
$themeUrl = null; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return new TwigTheme($twig, $descriptor->getTemplate(), $this->blockRenderer, $themeUrl); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns true if this factory can handle the descriptor passed in parameter. False otherwise. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function canCreateTheme(ThemeDescriptorInterface $descriptor): bool |
|
67
|
|
|
{ |
|
68
|
|
|
return $descriptor instanceof TwigThemeDescriptor; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|