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:
<?php
namespace Nethgui\Renderer;
class TemplateRenderer extends AbstractRenderer implements \Nethgui\Utility\PhpConsumerInterface, \Nethgui\Module\ModuleAttributesInterface, \Nethgui\Component\DependencyConsumer
{
private $templateResolver;
protected $phpWrapper;
protected $httpResponse;
public function __construct(\Nethgui\View\ViewInterface $view, $templateResolver, $contentType, $charset)
{
if ( ! is_callable($templateResolver)) {
throw new \InvalidArgumentException(sprintf('%s: $templateResolver must be a valid callback function.', get_class($this)), 1322238847);
}
parent::__construct($view);
$this->templateResolver = $templateResolver;
$this->phpWrapper = new \Nethgui\Utility\PhpWrapper();
$this->contentType = $contentType;
$this->charset = $charset;
}
public function spawnRenderer(\Nethgui\View\ViewInterface $view)
{
$o = new self($view, $this->getTemplateResolver(), $this->getContentType(), $this->getCharset());
$o->httpResponse = $this->httpResponse;
return $o;
}
protected function getTemplateResolver()
{
return $this->templateResolver;
}
public function render()
{
return $this->renderView($this->getTemplate(), array('view' => $this, 'T' => $this->getTranslateClosure(), 'response' => $this->httpResponse));
}
private function getTranslateClosure()
{
$view = $this->view;
$T = function($string, $args = array()) use ($view) {
return $view->translate($string, $args);
};
return $T;
}
protected function renderView($template, $viewState)
{
if ($template === FALSE) {
$viewOutput = '';
} elseif (is_callable($template)) {
$viewOutput = (string) call_user_func_array($template, $viewState);
} elseif (is_string($template)) {
$absoluteViewPath = call_user_func($this->getTemplateResolver(), $template);
if ( ! $absoluteViewPath) {
$this->getLog()->warning("Unable to load `{$template}`.");
return '';
}
ob_start();
$this->phpWrapper->phpInclude($absoluteViewPath, $viewState);
$viewOutput = ob_get_contents();
ob_end_clean();
} else {
throw new \UnexpectedValueException(sprintf("%s: wrong template type `%s`.", get_class($this), is_object($template) ? get_class($template) : gettype($template)), 1324479415);
}
return $viewOutput;
}
public function setPhpWrapper(\Nethgui\Utility\PhpWrapper $object)
{
$this->phpWrapper = $object;
}
public function getCategory()
{
return $this->translate($this->getModule()->getAttributesProvider()->getCategory());
}
public function getDescription()
{
return $this->translate($this->getModule()->getAttributesProvider()->getDescription());
}
public function getLanguageCatalog()
{
return $this->getModule()->getAttributesProvider()->getLanguageCatalog();
}
public function getMenuPosition()
{
return $this->getModule()->getAttributesProvider()->getMenuPosition();
}
public function getTags()
{
return $this->translate($this->getModule()->getAttributesProvider()->getTags());
}
public function getTitle()
{
return $this->translate($this->getModule()->getAttributesProvider()->getTitle());
}
public function getCharset()
{
return $this->charset;
}
public function getContentType()
{
return $this->contentType;
}
public function getDependencySetters()
{
$myHttpResponse = &$this->httpResponse;
return array(
'HttpResponse' => function (\Nethgui\Utility\HttpResponse $o) use (&$myHttpResponse) {
$myHttpResponse = $o;
}
);
}
}