@@ -30,7 +30,7 @@ |
||
30 | 30 | * @param array $context The context in which to render the Renderable. |
31 | 31 | * @param string $content Optional. The content that the view should |
32 | 32 | * represent. |
33 | - * @return string|null Rendered output of the Renderable. |
|
33 | + * @return string Rendered output of the Renderable. |
|
34 | 34 | */ |
35 | 35 | public function render(array $context, $content = null) |
36 | 36 | { |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * PHPView |
|
4 | - * |
|
5 | - * @package brightnucleus/chainmail |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * PHPView |
|
4 | + * |
|
5 | + * @package brightnucleus/chainmail |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | namespace BrightNucleus\ChainMail\View; |
13 | 13 | |
@@ -22,23 +22,23 @@ discard block |
||
22 | 22 | class PHPView extends AbstractView |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Render the current Renderable for a given context. |
|
27 | - * |
|
28 | - * @since 1.0.0 |
|
29 | - * |
|
30 | - * @param array $context The context in which to render the Renderable. |
|
31 | - * @param string $content Optional. The content that the view should |
|
32 | - * represent. |
|
33 | - * @return string|null Rendered output of the Renderable. |
|
34 | - */ |
|
35 | - public function render(array $context, $content = null) |
|
36 | - { |
|
25 | + /** |
|
26 | + * Render the current Renderable for a given context. |
|
27 | + * |
|
28 | + * @since 1.0.0 |
|
29 | + * |
|
30 | + * @param array $context The context in which to render the Renderable. |
|
31 | + * @param string $content Optional. The content that the view should |
|
32 | + * represent. |
|
33 | + * @return string|null Rendered output of the Renderable. |
|
34 | + */ |
|
35 | + public function render(array $context, $content = null) |
|
36 | + { |
|
37 | 37 | |
38 | - ob_start(); |
|
38 | + ob_start(); |
|
39 | 39 | |
40 | - include $this->viewLocation . '.php'; |
|
40 | + include $this->viewLocation . '.php'; |
|
41 | 41 | |
42 | - return ob_get_clean(); |
|
43 | - } |
|
42 | + return ob_get_clean(); |
|
43 | + } |
|
44 | 44 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * ChainMail |
|
4 | - * |
|
5 | - * @package brightnucleus/chainmail |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * ChainMail |
|
4 | + * |
|
5 | + * @package brightnucleus/chainmail |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | namespace BrightNucleus\ChainMail; |
13 | 13 | |
@@ -27,112 +27,112 @@ discard block |
||
27 | 27 | class ChainMail |
28 | 28 | { |
29 | 29 | |
30 | - const DEFAULT_CONFIG = __DIR__ . '/../config/defaults.php'; |
|
31 | - |
|
32 | - /** |
|
33 | - * Configuration Settings. |
|
34 | - * |
|
35 | - * @since 1.0.0 |
|
36 | - * |
|
37 | - * @var ConfigInterface |
|
38 | - */ |
|
39 | - protected $config; |
|
40 | - |
|
41 | - /** |
|
42 | - * Instantiate a ChainMail object. |
|
43 | - * |
|
44 | - * @since 1.0.0 |
|
45 | - * |
|
46 | - * @param ConfigInterface|null $config Optional. Configuration settings. |
|
47 | - */ |
|
48 | - public function __construct(ConfigInterface $config = null) |
|
49 | - { |
|
50 | - |
|
51 | - $defaults = new Config(include(self::DEFAULT_CONFIG)); |
|
52 | - |
|
53 | - if ( ! $config) { |
|
54 | - $this->config = $defaults; |
|
55 | - |
|
56 | - return; |
|
57 | - } |
|
58 | - |
|
59 | - $this->config = new Config(array_merge( |
|
60 | - (array)$defaults, |
|
61 | - (array)$config) |
|
62 | - ); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Create a new mail object. |
|
67 | - * |
|
68 | - * @since 1.0.0 |
|
69 | - * |
|
70 | - * @param string|null $format Optional. Format to use. |
|
71 | - * @param string|TemplateInterface|null $template Optional. Template to be |
|
72 | - * used. |
|
73 | - * @return MailInterface |
|
74 | - * @throws RuntimeException |
|
75 | - */ |
|
76 | - public function createMail($format = null, $template = null) |
|
77 | - { |
|
78 | - $mail_factory = new Factory($this->config, 'mails'); |
|
79 | - $mail_class = $this->config->getKey('formats')[$format]['mail']; |
|
80 | - $mail = $mail_factory->create($mail_class); |
|
81 | - $mail->setTemplate($template); |
|
82 | - |
|
83 | - return $mail; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Render a specific section. |
|
88 | - * |
|
89 | - * @since 1.0.0 |
|
90 | - * |
|
91 | - * @param string $sectionType Type of section to render. |
|
92 | - * @param array $context The context in which to render the section. |
|
93 | - * @return string Rendered HTML. |
|
94 | - */ |
|
95 | - public static function renderSection($sectionType, $context) |
|
96 | - { |
|
97 | - /** @var SectionInterface $section */ |
|
98 | - $section = $context['sections'][$sectionType]; |
|
99 | - |
|
100 | - return $section->render($context); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Get an array of strings representing the sections that are used by the |
|
105 | - * template. |
|
106 | - * |
|
107 | - * @since 1.0.0 |
|
108 | - * |
|
109 | - * @param array $context The context in which to render the section. |
|
110 | - * @return array Array of strings with section types. |
|
111 | - */ |
|
112 | - public static function getUsedSections($context) |
|
113 | - { |
|
114 | - /** @var TemplateInterface $template */ |
|
115 | - $template = $context['template']; |
|
116 | - |
|
117 | - return $template->getUsedSections(); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Render a all used sections. |
|
122 | - * |
|
123 | - * @since 1.0.0 |
|
124 | - * |
|
125 | - * @param array $context The context in which to render the section. |
|
126 | - * @return string Rendered HTML. |
|
127 | - */ |
|
128 | - public static function renderSections($context) |
|
129 | - { |
|
130 | - $output = ''; |
|
131 | - |
|
132 | - foreach (self::getUsedSections($context) as $section) { |
|
133 | - $output .= self::renderSection($section, $context); |
|
134 | - } |
|
135 | - |
|
136 | - return $output; |
|
137 | - } |
|
30 | + const DEFAULT_CONFIG = __DIR__ . '/../config/defaults.php'; |
|
31 | + |
|
32 | + /** |
|
33 | + * Configuration Settings. |
|
34 | + * |
|
35 | + * @since 1.0.0 |
|
36 | + * |
|
37 | + * @var ConfigInterface |
|
38 | + */ |
|
39 | + protected $config; |
|
40 | + |
|
41 | + /** |
|
42 | + * Instantiate a ChainMail object. |
|
43 | + * |
|
44 | + * @since 1.0.0 |
|
45 | + * |
|
46 | + * @param ConfigInterface|null $config Optional. Configuration settings. |
|
47 | + */ |
|
48 | + public function __construct(ConfigInterface $config = null) |
|
49 | + { |
|
50 | + |
|
51 | + $defaults = new Config(include(self::DEFAULT_CONFIG)); |
|
52 | + |
|
53 | + if ( ! $config) { |
|
54 | + $this->config = $defaults; |
|
55 | + |
|
56 | + return; |
|
57 | + } |
|
58 | + |
|
59 | + $this->config = new Config(array_merge( |
|
60 | + (array)$defaults, |
|
61 | + (array)$config) |
|
62 | + ); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Create a new mail object. |
|
67 | + * |
|
68 | + * @since 1.0.0 |
|
69 | + * |
|
70 | + * @param string|null $format Optional. Format to use. |
|
71 | + * @param string|TemplateInterface|null $template Optional. Template to be |
|
72 | + * used. |
|
73 | + * @return MailInterface |
|
74 | + * @throws RuntimeException |
|
75 | + */ |
|
76 | + public function createMail($format = null, $template = null) |
|
77 | + { |
|
78 | + $mail_factory = new Factory($this->config, 'mails'); |
|
79 | + $mail_class = $this->config->getKey('formats')[$format]['mail']; |
|
80 | + $mail = $mail_factory->create($mail_class); |
|
81 | + $mail->setTemplate($template); |
|
82 | + |
|
83 | + return $mail; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Render a specific section. |
|
88 | + * |
|
89 | + * @since 1.0.0 |
|
90 | + * |
|
91 | + * @param string $sectionType Type of section to render. |
|
92 | + * @param array $context The context in which to render the section. |
|
93 | + * @return string Rendered HTML. |
|
94 | + */ |
|
95 | + public static function renderSection($sectionType, $context) |
|
96 | + { |
|
97 | + /** @var SectionInterface $section */ |
|
98 | + $section = $context['sections'][$sectionType]; |
|
99 | + |
|
100 | + return $section->render($context); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Get an array of strings representing the sections that are used by the |
|
105 | + * template. |
|
106 | + * |
|
107 | + * @since 1.0.0 |
|
108 | + * |
|
109 | + * @param array $context The context in which to render the section. |
|
110 | + * @return array Array of strings with section types. |
|
111 | + */ |
|
112 | + public static function getUsedSections($context) |
|
113 | + { |
|
114 | + /** @var TemplateInterface $template */ |
|
115 | + $template = $context['template']; |
|
116 | + |
|
117 | + return $template->getUsedSections(); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Render a all used sections. |
|
122 | + * |
|
123 | + * @since 1.0.0 |
|
124 | + * |
|
125 | + * @param array $context The context in which to render the section. |
|
126 | + * @return string Rendered HTML. |
|
127 | + */ |
|
128 | + public static function renderSections($context) |
|
129 | + { |
|
130 | + $output = ''; |
|
131 | + |
|
132 | + foreach (self::getUsedSections($context) as $section) { |
|
133 | + $output .= self::renderSection($section, $context); |
|
134 | + } |
|
135 | + |
|
136 | + return $output; |
|
137 | + } |
|
138 | 138 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * AbstractMail |
|
4 | - * |
|
5 | - * @package brightnucleus/chainmail |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * AbstractMail |
|
4 | + * |
|
5 | + * @package brightnucleus/chainmail |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | namespace BrightNucleus\ChainMail\Mail; |
13 | 13 | |
@@ -29,200 +29,200 @@ discard block |
||
29 | 29 | abstract class AbstractMail implements MailInterface |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * Configuration Settings. |
|
34 | - * |
|
35 | - * @since 1.0.0 |
|
36 | - * |
|
37 | - * @var ConfigInterface |
|
38 | - */ |
|
39 | - protected $config; |
|
40 | - |
|
41 | - /** |
|
42 | - * Template that is used for the email. |
|
43 | - * |
|
44 | - * @since 1.0.0 |
|
45 | - * |
|
46 | - * @var TemplateInterface |
|
47 | - */ |
|
48 | - protected $template; |
|
49 | - |
|
50 | - /** |
|
51 | - * Content for the different sections. |
|
52 | - * |
|
53 | - * @since 1.0.0 |
|
54 | - * |
|
55 | - * @var array |
|
56 | - */ |
|
57 | - protected $sectionContent = []; |
|
58 | - |
|
59 | - /** |
|
60 | - * Format of the mail. |
|
61 | - * |
|
62 | - * @since 1.0.0 |
|
63 | - * |
|
64 | - * @var string |
|
65 | - */ |
|
66 | - protected $format; |
|
67 | - |
|
68 | - /** |
|
69 | - * Instantiate an AbstractMail object. |
|
70 | - * |
|
71 | - * @since 1.0.0 |
|
72 | - * |
|
73 | - * @param ConfigInterface $config |
|
74 | - */ |
|
75 | - public function __construct(ConfigInterface $config) |
|
76 | - { |
|
77 | - $this->config = $config; |
|
78 | - $this->setFormat(); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Set the format of the mail. |
|
83 | - * |
|
84 | - * @since 1.0.0 |
|
85 | - * |
|
86 | - * @return string Format of the Mail. |
|
87 | - */ |
|
88 | - protected function getFormat() |
|
89 | - { |
|
90 | - return $this->format; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Set the format of the mail. |
|
95 | - * |
|
96 | - * @since 1.0.0 |
|
97 | - * |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - abstract protected function setFormat(); |
|
101 | - |
|
102 | - /** |
|
103 | - * Set the template to use for the renderer. |
|
104 | - * |
|
105 | - * @since 1.0.0 |
|
106 | - * |
|
107 | - * @param string|TemplateInterface $template Template to use for the |
|
108 | - * renderer. |
|
109 | - * @throws RuntimeException |
|
110 | - */ |
|
111 | - public function setTemplate($template) |
|
112 | - { |
|
113 | - if (is_string($template)) { |
|
114 | - $template = $this->createTemplate($template); |
|
115 | - } |
|
116 | - $this->template = $template; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Get the template to use for the renderer. |
|
121 | - * |
|
122 | - * @since 1.0.0 |
|
123 | - * |
|
124 | - * @return TemplateInterface Reference to the template that is used. |
|
125 | - * @throws RuntimeException |
|
126 | - */ |
|
127 | - public function getTemplate() |
|
128 | - { |
|
129 | - |
|
130 | - if ( ! $this->template) { |
|
131 | - $this->setDefaultTemplate(); |
|
132 | - } |
|
133 | - |
|
134 | - if (is_string($this->template)) { |
|
135 | - $this->template = $this->createTemplate($this->template); |
|
136 | - } |
|
137 | - |
|
138 | - return $this->template; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Set the template to the default template defined in the configuration. |
|
143 | - * |
|
144 | - * @since 1.0.0 |
|
145 | - * |
|
146 | - * @throws RuntimeException |
|
147 | - */ |
|
148 | - protected function setDefaultTemplate() |
|
149 | - { |
|
150 | - $defaultTemplate = $this->config->getKey('default_template'); |
|
151 | - $this->setTemplate($defaultTemplate); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Create an instance of a template. |
|
156 | - * |
|
157 | - * @since 1.0.0 |
|
158 | - * |
|
159 | - * @param string $template Template to instantiate. |
|
160 | - * @return TemplateInterface $template Newly created instance. |
|
161 | - * @throws RuntimeException |
|
162 | - */ |
|
163 | - protected function createTemplate($template) |
|
164 | - { |
|
165 | - $templateFactory = new Factory($this->config, 'templates'); |
|
166 | - |
|
167 | - return $templateFactory->create($template, [$template]); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Add a section to the Mail. |
|
172 | - * |
|
173 | - * @since 1.0.0 |
|
174 | - * |
|
175 | - * @param string $type Type of section to add. |
|
176 | - * @param string $content Content of the section. |
|
177 | - * @throws RuntimeException |
|
178 | - */ |
|
179 | - public function addSection($type, $content) |
|
180 | - { |
|
181 | - $this->sectionContent[$type] = $content; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Render the Mail for a given context. |
|
186 | - * |
|
187 | - * @since 1.0.0 |
|
188 | - * |
|
189 | - * @param array $context The context in which to render the email. |
|
190 | - * @return string Rendered output of the email |
|
191 | - * @throws RuntimeException |
|
192 | - */ |
|
193 | - public function render(array $context) |
|
194 | - { |
|
195 | - |
|
196 | - $template = $this->getTemplate(); |
|
197 | - |
|
198 | - $context['template'] = $template; |
|
199 | - |
|
200 | - $sections = $template->getUsedSections(); |
|
201 | - |
|
202 | - $sectionFactory = new Factory($this->config, 'sections'); |
|
203 | - foreach ($sections as $section) { |
|
204 | - $content = null; |
|
205 | - if (array_key_exists($section, $this->sectionContent)) { |
|
206 | - $content = $this->sectionContent[$section]; |
|
207 | - } |
|
208 | - $context['sections'][$section] = $sectionFactory->create($section, |
|
209 | - [$section, $content]); |
|
210 | - } |
|
211 | - |
|
212 | - $context['format'] = $this->getFormat(); |
|
213 | - |
|
214 | - $context = $this->setContext($context); |
|
215 | - |
|
216 | - return $template->render($context); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Set the context of the mail. |
|
221 | - * |
|
222 | - * @since 1.0.0 |
|
223 | - * |
|
224 | - * @param array $context Context to set/modify. |
|
225 | - * @return array Updated context. |
|
226 | - */ |
|
227 | - abstract protected function setContext(array $context); |
|
32 | + /** |
|
33 | + * Configuration Settings. |
|
34 | + * |
|
35 | + * @since 1.0.0 |
|
36 | + * |
|
37 | + * @var ConfigInterface |
|
38 | + */ |
|
39 | + protected $config; |
|
40 | + |
|
41 | + /** |
|
42 | + * Template that is used for the email. |
|
43 | + * |
|
44 | + * @since 1.0.0 |
|
45 | + * |
|
46 | + * @var TemplateInterface |
|
47 | + */ |
|
48 | + protected $template; |
|
49 | + |
|
50 | + /** |
|
51 | + * Content for the different sections. |
|
52 | + * |
|
53 | + * @since 1.0.0 |
|
54 | + * |
|
55 | + * @var array |
|
56 | + */ |
|
57 | + protected $sectionContent = []; |
|
58 | + |
|
59 | + /** |
|
60 | + * Format of the mail. |
|
61 | + * |
|
62 | + * @since 1.0.0 |
|
63 | + * |
|
64 | + * @var string |
|
65 | + */ |
|
66 | + protected $format; |
|
67 | + |
|
68 | + /** |
|
69 | + * Instantiate an AbstractMail object. |
|
70 | + * |
|
71 | + * @since 1.0.0 |
|
72 | + * |
|
73 | + * @param ConfigInterface $config |
|
74 | + */ |
|
75 | + public function __construct(ConfigInterface $config) |
|
76 | + { |
|
77 | + $this->config = $config; |
|
78 | + $this->setFormat(); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Set the format of the mail. |
|
83 | + * |
|
84 | + * @since 1.0.0 |
|
85 | + * |
|
86 | + * @return string Format of the Mail. |
|
87 | + */ |
|
88 | + protected function getFormat() |
|
89 | + { |
|
90 | + return $this->format; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Set the format of the mail. |
|
95 | + * |
|
96 | + * @since 1.0.0 |
|
97 | + * |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + abstract protected function setFormat(); |
|
101 | + |
|
102 | + /** |
|
103 | + * Set the template to use for the renderer. |
|
104 | + * |
|
105 | + * @since 1.0.0 |
|
106 | + * |
|
107 | + * @param string|TemplateInterface $template Template to use for the |
|
108 | + * renderer. |
|
109 | + * @throws RuntimeException |
|
110 | + */ |
|
111 | + public function setTemplate($template) |
|
112 | + { |
|
113 | + if (is_string($template)) { |
|
114 | + $template = $this->createTemplate($template); |
|
115 | + } |
|
116 | + $this->template = $template; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Get the template to use for the renderer. |
|
121 | + * |
|
122 | + * @since 1.0.0 |
|
123 | + * |
|
124 | + * @return TemplateInterface Reference to the template that is used. |
|
125 | + * @throws RuntimeException |
|
126 | + */ |
|
127 | + public function getTemplate() |
|
128 | + { |
|
129 | + |
|
130 | + if ( ! $this->template) { |
|
131 | + $this->setDefaultTemplate(); |
|
132 | + } |
|
133 | + |
|
134 | + if (is_string($this->template)) { |
|
135 | + $this->template = $this->createTemplate($this->template); |
|
136 | + } |
|
137 | + |
|
138 | + return $this->template; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Set the template to the default template defined in the configuration. |
|
143 | + * |
|
144 | + * @since 1.0.0 |
|
145 | + * |
|
146 | + * @throws RuntimeException |
|
147 | + */ |
|
148 | + protected function setDefaultTemplate() |
|
149 | + { |
|
150 | + $defaultTemplate = $this->config->getKey('default_template'); |
|
151 | + $this->setTemplate($defaultTemplate); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Create an instance of a template. |
|
156 | + * |
|
157 | + * @since 1.0.0 |
|
158 | + * |
|
159 | + * @param string $template Template to instantiate. |
|
160 | + * @return TemplateInterface $template Newly created instance. |
|
161 | + * @throws RuntimeException |
|
162 | + */ |
|
163 | + protected function createTemplate($template) |
|
164 | + { |
|
165 | + $templateFactory = new Factory($this->config, 'templates'); |
|
166 | + |
|
167 | + return $templateFactory->create($template, [$template]); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Add a section to the Mail. |
|
172 | + * |
|
173 | + * @since 1.0.0 |
|
174 | + * |
|
175 | + * @param string $type Type of section to add. |
|
176 | + * @param string $content Content of the section. |
|
177 | + * @throws RuntimeException |
|
178 | + */ |
|
179 | + public function addSection($type, $content) |
|
180 | + { |
|
181 | + $this->sectionContent[$type] = $content; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Render the Mail for a given context. |
|
186 | + * |
|
187 | + * @since 1.0.0 |
|
188 | + * |
|
189 | + * @param array $context The context in which to render the email. |
|
190 | + * @return string Rendered output of the email |
|
191 | + * @throws RuntimeException |
|
192 | + */ |
|
193 | + public function render(array $context) |
|
194 | + { |
|
195 | + |
|
196 | + $template = $this->getTemplate(); |
|
197 | + |
|
198 | + $context['template'] = $template; |
|
199 | + |
|
200 | + $sections = $template->getUsedSections(); |
|
201 | + |
|
202 | + $sectionFactory = new Factory($this->config, 'sections'); |
|
203 | + foreach ($sections as $section) { |
|
204 | + $content = null; |
|
205 | + if (array_key_exists($section, $this->sectionContent)) { |
|
206 | + $content = $this->sectionContent[$section]; |
|
207 | + } |
|
208 | + $context['sections'][$section] = $sectionFactory->create($section, |
|
209 | + [$section, $content]); |
|
210 | + } |
|
211 | + |
|
212 | + $context['format'] = $this->getFormat(); |
|
213 | + |
|
214 | + $context = $this->setContext($context); |
|
215 | + |
|
216 | + return $template->render($context); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Set the context of the mail. |
|
221 | + * |
|
222 | + * @since 1.0.0 |
|
223 | + * |
|
224 | + * @param array $context Context to set/modify. |
|
225 | + * @return array Updated context. |
|
226 | + */ |
|
227 | + abstract protected function setContext(array $context); |
|
228 | 228 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * MailInterface |
|
4 | - * |
|
5 | - * @package brightnucleus/chainmail |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * MailInterface |
|
4 | + * |
|
5 | + * @package brightnucleus/chainmail |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | namespace BrightNucleus\ChainMail; |
13 | 13 | |
@@ -24,34 +24,34 @@ discard block |
||
24 | 24 | interface MailInterface extends Renderable |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * Set the template that the email will use. |
|
29 | - * |
|
30 | - * @since 1.0.0 |
|
31 | - * |
|
32 | - * @param string|TemplateInterface $template Template to use. |
|
33 | - * @return void |
|
34 | - */ |
|
35 | - public function setTemplate($template); |
|
27 | + /** |
|
28 | + * Set the template that the email will use. |
|
29 | + * |
|
30 | + * @since 1.0.0 |
|
31 | + * |
|
32 | + * @param string|TemplateInterface $template Template to use. |
|
33 | + * @return void |
|
34 | + */ |
|
35 | + public function setTemplate($template); |
|
36 | 36 | |
37 | - /** |
|
38 | - * Get the instance of the template. |
|
39 | - * |
|
40 | - * @since 1.0.0 |
|
41 | - * |
|
42 | - * @return TemplateInterface |
|
43 | - */ |
|
44 | - public function getTemplate(); |
|
37 | + /** |
|
38 | + * Get the instance of the template. |
|
39 | + * |
|
40 | + * @since 1.0.0 |
|
41 | + * |
|
42 | + * @return TemplateInterface |
|
43 | + */ |
|
44 | + public function getTemplate(); |
|
45 | 45 | |
46 | - /** |
|
47 | - * Add a section to the Mail. |
|
48 | - * |
|
49 | - * @since 1.0.0 |
|
50 | - * |
|
51 | - * @param string $type Type of section to add. |
|
52 | - * @param string $content Content of the section. |
|
53 | - * @return void |
|
54 | - * @throws RuntimeException |
|
55 | - */ |
|
56 | - public function addSection($type, $content); |
|
46 | + /** |
|
47 | + * Add a section to the Mail. |
|
48 | + * |
|
49 | + * @since 1.0.0 |
|
50 | + * |
|
51 | + * @param string $type Type of section to add. |
|
52 | + * @param string $content Content of the section. |
|
53 | + * @return void |
|
54 | + * @throws RuntimeException |
|
55 | + */ |
|
56 | + public function addSection($type, $content); |
|
57 | 57 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * AbstractSection |
|
4 | - * |
|
5 | - * @package brightnucleus/chainmail |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * AbstractSection |
|
4 | + * |
|
5 | + * @package brightnucleus/chainmail |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | namespace BrightNucleus\ChainMail\Section; |
13 | 13 | |
@@ -27,156 +27,156 @@ discard block |
||
27 | 27 | abstract class AbstractSection implements SectionInterface |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Name of the Section. |
|
32 | - * |
|
33 | - * @since 1.0.0 |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $sectionName; |
|
38 | - |
|
39 | - /** |
|
40 | - * Configuration Settings. |
|
41 | - * |
|
42 | - * @since 1.0.0 |
|
43 | - * |
|
44 | - * @var ConfigInterface |
|
45 | - */ |
|
46 | - protected $config; |
|
47 | - |
|
48 | - /** |
|
49 | - * Content of the section. |
|
50 | - * |
|
51 | - * @since 1.0.0 |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - protected $content; |
|
56 | - |
|
57 | - /** |
|
58 | - * Instantiate a AbstractSection object. |
|
59 | - * |
|
60 | - * @since 1.0.0 |
|
61 | - * |
|
62 | - * @param ConfigInterface $config Configuration settings. |
|
63 | - * @param array $arguments Arguments that are passed through |
|
64 | - * the constructor. Contained |
|
65 | - * elements: string $section, string |
|
66 | - * $content |
|
67 | - * @throws RuntimeException |
|
68 | - */ |
|
69 | - public function __construct($config, $arguments) |
|
70 | - { |
|
71 | - $this->config = $config; |
|
72 | - list($section, $content) = $arguments; |
|
73 | - $this->setSectionName($section); |
|
74 | - $this->content = $content; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Set the name of the Section. |
|
79 | - * |
|
80 | - * @since 1.0.0 |
|
81 | - * |
|
82 | - * @param string|null $section Optional. Name of the section. |
|
83 | - * @throws RuntimeException |
|
84 | - */ |
|
85 | - protected function setSectionName($section = null) |
|
86 | - { |
|
87 | - if (null === $section) { |
|
88 | - throw new RuntimeException('Initialised section without passing it a section name.'); |
|
89 | - } |
|
90 | - if ( ! array_key_exists($section, $this->config['sections'])) { |
|
91 | - throw new RuntimeException('Initialised section with an unknown section name.'); |
|
92 | - } |
|
93 | - $this->sectionName = $section; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get the name of the Section. |
|
98 | - * |
|
99 | - * @since 1.0.0 |
|
100 | - * |
|
101 | - * @return string Name of the section. |
|
102 | - */ |
|
103 | - public function getSectionName() |
|
104 | - { |
|
105 | - return $this->sectionName; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Get the name of the View to use for rendering. |
|
110 | - * |
|
111 | - * @since 1.0.0 |
|
112 | - * |
|
113 | - * @return string Name of the view. |
|
114 | - */ |
|
115 | - protected function getViewName() |
|
116 | - { |
|
117 | - return $this->config['sections'][$this->getSectionName()]['view_name']; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Render the current Renderable for a given context. |
|
122 | - * |
|
123 | - * @since 1.0.0 |
|
124 | - * |
|
125 | - * @param array $context The context in which to render the Renderable. |
|
126 | - * @return string Rendered output of the Renderable. |
|
127 | - * @throws RuntimeException |
|
128 | - */ |
|
129 | - public function render(array $context) |
|
130 | - { |
|
131 | - |
|
132 | - $viewLocation = $this->getViewLocation($context); |
|
133 | - $viewType = $this->config['view_type']; |
|
134 | - |
|
135 | - $viewFactory = new Factory($this->config, 'view_types'); |
|
136 | - $view = $viewFactory->create($viewType, $viewLocation); |
|
137 | - |
|
138 | - $context['css_class'] = $this->getCSSClass(); |
|
139 | - |
|
140 | - return $view->render($context, $this->content); |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Get the location of the view that is used for rendering. |
|
145 | - * |
|
146 | - * @since 1.0.0 |
|
147 | - * |
|
148 | - * @param array $context Context for which to get the view location. |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - protected function getViewLocation(array $context) |
|
152 | - { |
|
153 | - return $this->getViewRoot() . '/sections/' . $context['format'] . '/' . $this->getViewName(); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Get the root location of the view that is used for rendering. |
|
158 | - * |
|
159 | - * @since 1.0.0 |
|
160 | - * |
|
161 | - * @return string |
|
162 | - */ |
|
163 | - protected function getViewRoot() |
|
164 | - { |
|
165 | - $viewRoots = $this->config['view_root_locations']; |
|
166 | - $viewRootKey = $this->config['sections'][$this->getSectionName()]['view_location']; |
|
167 | - |
|
168 | - return $viewRoots[$viewRootKey]; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Get the CSS class that is used for the section. |
|
173 | - * |
|
174 | - * @since 1.0.0 |
|
175 | - * |
|
176 | - * @return string |
|
177 | - */ |
|
178 | - protected function getCSSClass() |
|
179 | - { |
|
180 | - return $this->config['sections'][$this->getSectionName()]['css_class']; |
|
181 | - } |
|
30 | + /** |
|
31 | + * Name of the Section. |
|
32 | + * |
|
33 | + * @since 1.0.0 |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $sectionName; |
|
38 | + |
|
39 | + /** |
|
40 | + * Configuration Settings. |
|
41 | + * |
|
42 | + * @since 1.0.0 |
|
43 | + * |
|
44 | + * @var ConfigInterface |
|
45 | + */ |
|
46 | + protected $config; |
|
47 | + |
|
48 | + /** |
|
49 | + * Content of the section. |
|
50 | + * |
|
51 | + * @since 1.0.0 |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + protected $content; |
|
56 | + |
|
57 | + /** |
|
58 | + * Instantiate a AbstractSection object. |
|
59 | + * |
|
60 | + * @since 1.0.0 |
|
61 | + * |
|
62 | + * @param ConfigInterface $config Configuration settings. |
|
63 | + * @param array $arguments Arguments that are passed through |
|
64 | + * the constructor. Contained |
|
65 | + * elements: string $section, string |
|
66 | + * $content |
|
67 | + * @throws RuntimeException |
|
68 | + */ |
|
69 | + public function __construct($config, $arguments) |
|
70 | + { |
|
71 | + $this->config = $config; |
|
72 | + list($section, $content) = $arguments; |
|
73 | + $this->setSectionName($section); |
|
74 | + $this->content = $content; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Set the name of the Section. |
|
79 | + * |
|
80 | + * @since 1.0.0 |
|
81 | + * |
|
82 | + * @param string|null $section Optional. Name of the section. |
|
83 | + * @throws RuntimeException |
|
84 | + */ |
|
85 | + protected function setSectionName($section = null) |
|
86 | + { |
|
87 | + if (null === $section) { |
|
88 | + throw new RuntimeException('Initialised section without passing it a section name.'); |
|
89 | + } |
|
90 | + if ( ! array_key_exists($section, $this->config['sections'])) { |
|
91 | + throw new RuntimeException('Initialised section with an unknown section name.'); |
|
92 | + } |
|
93 | + $this->sectionName = $section; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get the name of the Section. |
|
98 | + * |
|
99 | + * @since 1.0.0 |
|
100 | + * |
|
101 | + * @return string Name of the section. |
|
102 | + */ |
|
103 | + public function getSectionName() |
|
104 | + { |
|
105 | + return $this->sectionName; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Get the name of the View to use for rendering. |
|
110 | + * |
|
111 | + * @since 1.0.0 |
|
112 | + * |
|
113 | + * @return string Name of the view. |
|
114 | + */ |
|
115 | + protected function getViewName() |
|
116 | + { |
|
117 | + return $this->config['sections'][$this->getSectionName()]['view_name']; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Render the current Renderable for a given context. |
|
122 | + * |
|
123 | + * @since 1.0.0 |
|
124 | + * |
|
125 | + * @param array $context The context in which to render the Renderable. |
|
126 | + * @return string Rendered output of the Renderable. |
|
127 | + * @throws RuntimeException |
|
128 | + */ |
|
129 | + public function render(array $context) |
|
130 | + { |
|
131 | + |
|
132 | + $viewLocation = $this->getViewLocation($context); |
|
133 | + $viewType = $this->config['view_type']; |
|
134 | + |
|
135 | + $viewFactory = new Factory($this->config, 'view_types'); |
|
136 | + $view = $viewFactory->create($viewType, $viewLocation); |
|
137 | + |
|
138 | + $context['css_class'] = $this->getCSSClass(); |
|
139 | + |
|
140 | + return $view->render($context, $this->content); |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Get the location of the view that is used for rendering. |
|
145 | + * |
|
146 | + * @since 1.0.0 |
|
147 | + * |
|
148 | + * @param array $context Context for which to get the view location. |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + protected function getViewLocation(array $context) |
|
152 | + { |
|
153 | + return $this->getViewRoot() . '/sections/' . $context['format'] . '/' . $this->getViewName(); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Get the root location of the view that is used for rendering. |
|
158 | + * |
|
159 | + * @since 1.0.0 |
|
160 | + * |
|
161 | + * @return string |
|
162 | + */ |
|
163 | + protected function getViewRoot() |
|
164 | + { |
|
165 | + $viewRoots = $this->config['view_root_locations']; |
|
166 | + $viewRootKey = $this->config['sections'][$this->getSectionName()]['view_location']; |
|
167 | + |
|
168 | + return $viewRoots[$viewRootKey]; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Get the CSS class that is used for the section. |
|
173 | + * |
|
174 | + * @since 1.0.0 |
|
175 | + * |
|
176 | + * @return string |
|
177 | + */ |
|
178 | + protected function getCSSClass() |
|
179 | + { |
|
180 | + return $this->config['sections'][$this->getSectionName()]['css_class']; |
|
181 | + } |
|
182 | 182 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * AbstractTemplate |
|
4 | - * |
|
5 | - * @package brightnucleus/chainmail |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * AbstractTemplate |
|
4 | + * |
|
5 | + * @package brightnucleus/chainmail |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | namespace BrightNucleus\ChainMail\Template; |
13 | 13 | |
@@ -27,149 +27,149 @@ discard block |
||
27 | 27 | abstract class AbstractTemplate implements TemplateInterface |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Configuration Settings. |
|
32 | - * |
|
33 | - * @since 1.0.0 |
|
34 | - * |
|
35 | - * @var ConfigInterface |
|
36 | - */ |
|
37 | - protected $config; |
|
38 | - |
|
39 | - /** |
|
40 | - * Name of the template. |
|
41 | - * |
|
42 | - * @since 1.0.0 |
|
43 | - * |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $templateName; |
|
47 | - |
|
48 | - /** |
|
49 | - * Instantiate a AbstractTemplate object. |
|
50 | - * |
|
51 | - * @since 1.0.0 |
|
52 | - * |
|
53 | - * @param ConfigInterface $config Configuration settings. |
|
54 | - * @param array $arguments Arguments that are passed through |
|
55 | - * the constructor. Contained |
|
56 | - * elements: string $template |
|
57 | - * @throws RuntimeException |
|
58 | - */ |
|
59 | - public function __construct($config, $arguments) |
|
60 | - { |
|
61 | - $this->config = $config; |
|
62 | - list($template) = $arguments; |
|
63 | - $this->setTemplateName($template); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Get the name of the Template. |
|
68 | - * |
|
69 | - * @since 1.0.0 |
|
70 | - * |
|
71 | - * @return string Name of the template. |
|
72 | - */ |
|
73 | - public function getTemplateName() |
|
74 | - { |
|
75 | - return $this->templateName; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Set the name of the Template. |
|
80 | - * |
|
81 | - * @since 1.0.0 |
|
82 | - * |
|
83 | - * @param string|null $template Optional. Name of the template. |
|
84 | - * @throws RuntimeException |
|
85 | - */ |
|
86 | - protected function setTemplateName($template = null) |
|
87 | - { |
|
88 | - if (null === $template) { |
|
89 | - throw new RuntimeException('Initialised template without passing it a template name.'); |
|
90 | - } |
|
91 | - if ( ! array_key_exists($template, $this->config['templates'])) { |
|
92 | - throw new RuntimeException('Initialised template with an unknown template name.'); |
|
93 | - } |
|
94 | - $this->templateName = $template; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Get the name of the View to use for rendering. |
|
99 | - * |
|
100 | - * @since 1.0.0 |
|
101 | - * |
|
102 | - * @return string Name of the view. |
|
103 | - */ |
|
104 | - protected function getViewName() |
|
105 | - { |
|
106 | - return $this->config['templates'][$this->getTemplateName()]['view_name']; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Get an array of Sections that are used by this template. |
|
111 | - * |
|
112 | - * @since 1.0.0 |
|
113 | - * |
|
114 | - * @return array Sections that are used by this template. |
|
115 | - */ |
|
116 | - public function getUsedSections() |
|
117 | - { |
|
118 | - return $this->config['templates'][$this->getTemplateName()]['sections']; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Render the template for a given context. |
|
123 | - * |
|
124 | - * @since 1.0.0 |
|
125 | - * |
|
126 | - * @param array $context The context in which to render the template. |
|
127 | - * @return string The rendered content. |
|
128 | - * @throws RuntimeException |
|
129 | - */ |
|
130 | - public function render(array $context) |
|
131 | - { |
|
132 | - |
|
133 | - $viewLocation = $this->getViewLocation($context); |
|
134 | - $viewType = $this->config['view_type']; |
|
135 | - |
|
136 | - $viewFactory = new Factory($this->config, 'view_types'); |
|
137 | - $view = $viewFactory->create($viewType, $viewLocation); |
|
138 | - |
|
139 | - $sanitizerType = $this->config['formats'][$context['format']]['sanitizer']; |
|
140 | - $sanitizerFactory = new Factory($this->config, 'sanitizers'); |
|
141 | - $sanitizer = $sanitizerFactory->create($sanitizerType); |
|
142 | - |
|
143 | - $output = $view->render($context); |
|
144 | - |
|
145 | - return $sanitizer->sanitize($output, $context); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Get the location of the view that is used for rendering. |
|
150 | - * |
|
151 | - * @since 1.0.0 |
|
152 | - * |
|
153 | - * @param array $context Context for which to get the view location. |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - protected function getViewLocation(array $context) |
|
157 | - { |
|
158 | - return $this->getViewRoot() . '/templates/' . $context['format'] . '/' . $this->getViewName(); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Get the root location of the view that is used for rendering. |
|
163 | - * |
|
164 | - * @since 1.0.0 |
|
165 | - * |
|
166 | - * @return string |
|
167 | - */ |
|
168 | - protected function getViewRoot() |
|
169 | - { |
|
170 | - $viewRoots = $this->config['view_root_locations']; |
|
171 | - $viewRootKey = $this->config['templates'][$this->getTemplateName()]['view_location']; |
|
172 | - |
|
173 | - return $viewRoots[$viewRootKey]; |
|
174 | - } |
|
30 | + /** |
|
31 | + * Configuration Settings. |
|
32 | + * |
|
33 | + * @since 1.0.0 |
|
34 | + * |
|
35 | + * @var ConfigInterface |
|
36 | + */ |
|
37 | + protected $config; |
|
38 | + |
|
39 | + /** |
|
40 | + * Name of the template. |
|
41 | + * |
|
42 | + * @since 1.0.0 |
|
43 | + * |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $templateName; |
|
47 | + |
|
48 | + /** |
|
49 | + * Instantiate a AbstractTemplate object. |
|
50 | + * |
|
51 | + * @since 1.0.0 |
|
52 | + * |
|
53 | + * @param ConfigInterface $config Configuration settings. |
|
54 | + * @param array $arguments Arguments that are passed through |
|
55 | + * the constructor. Contained |
|
56 | + * elements: string $template |
|
57 | + * @throws RuntimeException |
|
58 | + */ |
|
59 | + public function __construct($config, $arguments) |
|
60 | + { |
|
61 | + $this->config = $config; |
|
62 | + list($template) = $arguments; |
|
63 | + $this->setTemplateName($template); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Get the name of the Template. |
|
68 | + * |
|
69 | + * @since 1.0.0 |
|
70 | + * |
|
71 | + * @return string Name of the template. |
|
72 | + */ |
|
73 | + public function getTemplateName() |
|
74 | + { |
|
75 | + return $this->templateName; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Set the name of the Template. |
|
80 | + * |
|
81 | + * @since 1.0.0 |
|
82 | + * |
|
83 | + * @param string|null $template Optional. Name of the template. |
|
84 | + * @throws RuntimeException |
|
85 | + */ |
|
86 | + protected function setTemplateName($template = null) |
|
87 | + { |
|
88 | + if (null === $template) { |
|
89 | + throw new RuntimeException('Initialised template without passing it a template name.'); |
|
90 | + } |
|
91 | + if ( ! array_key_exists($template, $this->config['templates'])) { |
|
92 | + throw new RuntimeException('Initialised template with an unknown template name.'); |
|
93 | + } |
|
94 | + $this->templateName = $template; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Get the name of the View to use for rendering. |
|
99 | + * |
|
100 | + * @since 1.0.0 |
|
101 | + * |
|
102 | + * @return string Name of the view. |
|
103 | + */ |
|
104 | + protected function getViewName() |
|
105 | + { |
|
106 | + return $this->config['templates'][$this->getTemplateName()]['view_name']; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Get an array of Sections that are used by this template. |
|
111 | + * |
|
112 | + * @since 1.0.0 |
|
113 | + * |
|
114 | + * @return array Sections that are used by this template. |
|
115 | + */ |
|
116 | + public function getUsedSections() |
|
117 | + { |
|
118 | + return $this->config['templates'][$this->getTemplateName()]['sections']; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Render the template for a given context. |
|
123 | + * |
|
124 | + * @since 1.0.0 |
|
125 | + * |
|
126 | + * @param array $context The context in which to render the template. |
|
127 | + * @return string The rendered content. |
|
128 | + * @throws RuntimeException |
|
129 | + */ |
|
130 | + public function render(array $context) |
|
131 | + { |
|
132 | + |
|
133 | + $viewLocation = $this->getViewLocation($context); |
|
134 | + $viewType = $this->config['view_type']; |
|
135 | + |
|
136 | + $viewFactory = new Factory($this->config, 'view_types'); |
|
137 | + $view = $viewFactory->create($viewType, $viewLocation); |
|
138 | + |
|
139 | + $sanitizerType = $this->config['formats'][$context['format']]['sanitizer']; |
|
140 | + $sanitizerFactory = new Factory($this->config, 'sanitizers'); |
|
141 | + $sanitizer = $sanitizerFactory->create($sanitizerType); |
|
142 | + |
|
143 | + $output = $view->render($context); |
|
144 | + |
|
145 | + return $sanitizer->sanitize($output, $context); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Get the location of the view that is used for rendering. |
|
150 | + * |
|
151 | + * @since 1.0.0 |
|
152 | + * |
|
153 | + * @param array $context Context for which to get the view location. |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + protected function getViewLocation(array $context) |
|
157 | + { |
|
158 | + return $this->getViewRoot() . '/templates/' . $context['format'] . '/' . $this->getViewName(); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Get the root location of the view that is used for rendering. |
|
163 | + * |
|
164 | + * @since 1.0.0 |
|
165 | + * |
|
166 | + * @return string |
|
167 | + */ |
|
168 | + protected function getViewRoot() |
|
169 | + { |
|
170 | + $viewRoots = $this->config['view_root_locations']; |
|
171 | + $viewRootKey = $this->config['templates'][$this->getTemplateName()]['view_location']; |
|
172 | + |
|
173 | + return $viewRoots[$viewRootKey]; |
|
174 | + } |
|
175 | 175 | } |