|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BrightNucleus Chainmail Component. |
|
4
|
|
|
* |
|
5
|
|
|
* @package BrightNucleus/Chainmail |
|
6
|
|
|
* @author Alain Schlesser <[email protected]> |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @link http://www.brightnucleus.com/ |
|
9
|
|
|
* @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BrightNucleus\ChainMail\Template; |
|
13
|
|
|
|
|
14
|
|
|
use BrightNucleus\Chainmail\Exception\FailedToInitialiseTemplateException; |
|
15
|
|
|
use BrightNucleus\ChainMail\Support\Factory; |
|
16
|
|
|
use BrightNucleus\ChainMail\TemplateInterface; |
|
17
|
|
|
use BrightNucleus\Config\ConfigInterface; |
|
18
|
|
|
use BrightNucleus\View\ViewBuilder; |
|
19
|
|
|
use RuntimeException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Abstract Class AbstractTemplate. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
* |
|
26
|
|
|
* @package BrightNucleus\ChainMail\Template |
|
27
|
|
|
* @author Alain Schlesser <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class AbstractTemplate implements TemplateInterface |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Configuration Settings. |
|
34
|
|
|
* |
|
35
|
|
|
* @since 1.0.0 |
|
36
|
|
|
* |
|
37
|
|
|
* @var ConfigInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $config; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Name of the template. |
|
43
|
|
|
* |
|
44
|
|
|
* @since 1.0.0 |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $templateName; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* ViewBuilder to create template and section views. |
|
52
|
|
|
* |
|
53
|
|
|
* @since 1.0.0 |
|
54
|
|
|
* |
|
55
|
|
|
* @var ViewBuilder |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $viewBuilder; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Instantiate a AbstractTemplate object. |
|
61
|
|
|
* |
|
62
|
|
|
* @since 1.0.0 |
|
63
|
|
|
* |
|
64
|
|
|
* @param ConfigInterface $config Configuration settings. |
|
65
|
|
|
* @param array $arguments Arguments that are passed through the constructor. |
|
66
|
|
|
* Contained elements: string $template |
|
67
|
|
|
* |
|
68
|
|
|
* @throws RuntimeException |
|
69
|
|
|
*/ |
|
70
|
16 |
|
public function __construct($config, array $arguments) |
|
71
|
|
|
{ |
|
72
|
16 |
|
$this->config = $config; |
|
73
|
16 |
|
list($template, $this->viewBuilder) = $arguments; |
|
74
|
16 |
|
$this->setTemplateName($template); |
|
75
|
16 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the name of the Template. |
|
79
|
|
|
* |
|
80
|
|
|
* @since 1.0.0 |
|
81
|
|
|
* |
|
82
|
|
|
* @return string Name of the template. |
|
83
|
|
|
*/ |
|
84
|
8 |
|
public function getTemplateName() |
|
85
|
|
|
{ |
|
86
|
8 |
|
return $this->templateName; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Set the name of the Template. |
|
91
|
|
|
* |
|
92
|
|
|
* @since 1.0.0 |
|
93
|
|
|
* |
|
94
|
|
|
* @param string|null $template Optional. Name of the template. |
|
95
|
|
|
* |
|
96
|
|
|
* @throws FailedToInitialiseTemplateException If no template name was passed. |
|
97
|
|
|
* @throws FailedToInitialiseTemplateException If an unknown template name was passed. |
|
98
|
|
|
*/ |
|
99
|
16 |
|
protected function setTemplateName($template = null) |
|
100
|
|
|
{ |
|
101
|
16 |
|
if (null === $template) { |
|
102
|
|
|
throw new FailedToInitialiseTemplateException('Initialised template without passing it a template name.'); |
|
103
|
|
|
} |
|
104
|
16 |
|
if (! array_key_exists($template, $this->config['templates'])) { |
|
105
|
|
|
throw new FailedToInitialiseTemplateException('Initialised template with an unknown template name.'); |
|
106
|
|
|
} |
|
107
|
16 |
|
$this->templateName = $template; |
|
108
|
16 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get an array of Sections that are used by this template. |
|
112
|
|
|
* |
|
113
|
|
|
* @since 1.0.0 |
|
114
|
|
|
* |
|
115
|
|
|
* @return array Sections that are used by this template. |
|
116
|
|
|
*/ |
|
117
|
8 |
|
public function getUsedSections() |
|
118
|
|
|
{ |
|
119
|
8 |
|
return $this->config['templates'][$this->getTemplateName()]['sections']; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Render the template for a given context. |
|
124
|
|
|
* |
|
125
|
|
|
* @since 1.0.0 |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $context The context in which to render the template. |
|
128
|
|
|
* |
|
129
|
|
|
* @return string The rendered content. |
|
130
|
|
|
*/ |
|
131
|
8 |
|
public function render(array $context) |
|
132
|
|
|
{ |
|
133
|
|
|
|
|
134
|
8 |
|
$viewName = $this->getViewName($context); |
|
135
|
8 |
|
$view = $this->viewBuilder->create($viewName); |
|
136
|
|
|
|
|
137
|
8 |
|
$sanitizerType = $this->config['formats'][$context['format']]['sanitizer']; |
|
138
|
8 |
|
$sanitizerFactory = new Factory($this->config, 'sanitizers'); |
|
139
|
8 |
|
$sanitizer = $sanitizerFactory->create($sanitizerType); |
|
140
|
|
|
|
|
141
|
8 |
|
$output = $view->render($context); |
|
142
|
|
|
|
|
143
|
8 |
|
return $sanitizer->sanitize($output, $context); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the name of the View to use for rendering. |
|
148
|
|
|
* |
|
149
|
|
|
* @since 1.0.0 |
|
150
|
|
|
* |
|
151
|
|
|
* @param array $context Context in which to get the view name. |
|
152
|
|
|
* |
|
153
|
|
|
* @return string Name of the view. |
|
154
|
|
|
*/ |
|
155
|
8 |
|
protected function getViewName(array $context) |
|
156
|
|
|
{ |
|
157
|
8 |
|
return $this->config['templates'][$this->getTemplateName()]['view_name'] |
|
158
|
8 |
|
. '.' . $context['format']; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|