1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
<?php
namespace ICanBoogie\Render;
class Renderer
{
const OPTION_LAYOUT = 'layout';
const OPTION_PARTIAL = 'partial';
const OPTION_TEMPLATE = 'template';
const OPTION_CONTENT = 'content';
const OPTION_LOCALS = 'locals';
private $engines;
protected $template_resolver;
public function __construct(TemplateResolver $template_resolver, EngineCollection $engines)
{
$this->template_resolver = $template_resolver;
$this->engines = $engines;
}
public function resolve_template($name)
{
$template_pathname = $this->template_resolver->resolve($name, $this->engines->extensions, $tried);
if (!$template_pathname)
{
throw new TemplateNotFound("There is no template matching `$name`.", $tried ?: []);
}
return $template_pathname;
}
public function render($target_or_options, array $additional_options = [])
{
if (!$target_or_options && !$additional_options)
{
return null;
}
$options = $this->resolve_options($target_or_options, $additional_options);
$content = $options[self::OPTION_CONTENT];
$variables = $options[self::OPTION_LOCALS];
$template = $options[self::OPTION_PARTIAL];
if ($template)
{
$content = $this->render_partial($template, $content, $variables);
}
$template = $options[self::OPTION_TEMPLATE];
if ($template)
{
$content = $this->render_template($template, $content, $variables);
}
$template = $options[self::OPTION_LAYOUT];
if ($template)
{
$content = $this->render_layout($template, [ self::OPTION_CONTENT => $content ] + $variables);
}
return $content;
}
protected function render_partial($template, $content, $variables)
{
return $this->render_template(
$this->resolve_template_name($template)->as_partial,
$content,
$variables
);
}
protected function render_layout($template, array $variables)
{
return $this->render_template(
$this->resolve_template_name($template)->as_layout,
null,
$variables
);
}
protected function render_template($name, $content, $variables)
{
return $this->engines->render(
$this->resolve_template($name),
$content,
$variables
);
}
protected function resolve_template_name($content)
{
return TemplateName::from($content);
}
protected function resolve_options($target_or_options, array $additional_options = [])
{
$options = [];
if (is_object($target_or_options))
{
$additional_options[self::OPTION_CONTENT] = $target_or_options;
$additional_options[self::OPTION_LOCALS]['this'] = $target_or_options;
if (empty($additional_options[self::OPTION_PARTIAL]) && empty($additional_options[self::OPTION_TEMPLATE]))
{
$additional_options[self::OPTION_PARTIAL] = $this->resolve_template_name($target_or_options);
}
}
else if (is_array($target_or_options))
{
$options = $target_or_options;
}
else
{
throw new InvalidRenderTarget($target_or_options);
}
return $additional_options + $options + [
self::OPTION_CONTENT => null,
self::OPTION_PARTIAL => null,
self::OPTION_TEMPLATE => null,
self::OPTION_LAYOUT => null,
self::OPTION_LOCALS => []
];
}
}