@@ -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 $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 $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 | } |