|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace WebHemi\Adapter\Renderer\Twig; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use Psr\Http\Message\StreamInterface; |
|
16
|
|
|
use Twig_Environment; |
|
17
|
|
|
use Twig_Extension_Debug; |
|
18
|
|
|
use Twig_Loader_Filesystem; |
|
19
|
|
|
use Twig_SimpleFunction; |
|
20
|
|
|
use WebHemi\Adapter\Renderer\RendererAdapterInterface; |
|
21
|
|
|
use WebHemi\Config\ConfigInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class TwigRendererAdapter. |
|
25
|
|
|
*/ |
|
26
|
|
|
class TwigRendererAdapter implements RendererAdapterInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private $adapter; |
|
29
|
|
|
/** @var ConfigInterface */ |
|
30
|
|
|
private $config; |
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $defaultViewPath; |
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $templateViewPath; |
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
private $templateResourcePath; |
|
37
|
|
|
/** @var string */ |
|
38
|
|
|
private $applicationBaseUri; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* RendererAdapterInterface constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param ConfigInterface $templateConfig |
|
44
|
|
|
* @param string $templatePath |
|
45
|
|
|
* @param string $applicationBaseUri |
|
46
|
|
|
*/ |
|
47
|
4 |
|
public function __construct(ConfigInterface $templateConfig, $templatePath, $applicationBaseUri) |
|
48
|
|
|
{ |
|
49
|
4 |
|
$this->config = $templateConfig; |
|
50
|
4 |
|
$this->defaultViewPath = realpath(__DIR__.'/../../../../../resources/default_theme/view'); |
|
51
|
4 |
|
$this->templateViewPath = realpath(__DIR__.'/../../../../../'.$templatePath.'/view'); |
|
52
|
4 |
|
$this->templateResourcePath = $templatePath.'/static'; |
|
53
|
4 |
|
$this->applicationBaseUri = $applicationBaseUri; |
|
54
|
|
|
|
|
55
|
4 |
|
$loader = new Twig_Loader_Filesystem($this->templateViewPath); |
|
56
|
4 |
|
$loader->addPath($this->defaultViewPath, 'WebHemi'); |
|
57
|
4 |
|
$loader->addPath($this->templateViewPath, 'Theme'); |
|
58
|
4 |
|
$this->adapter = new Twig_Environment($loader, array('debug' => true, 'cache' => false)); |
|
59
|
4 |
|
$this->adapter->addExtension(new Twig_Extension_Debug()); |
|
60
|
|
|
|
|
61
|
4 |
|
$viewPath = $this->templateViewPath; |
|
62
|
|
|
// @codeCoverageIgnoreStart |
|
63
|
|
|
// link a core function into template level |
|
64
|
|
|
$function = new Twig_SimpleFunction('defined', function ($fileName) use ($viewPath) { |
|
65
|
|
|
$fileName = str_replace('@Theme', $viewPath, $fileName); |
|
66
|
|
|
return file_exists($fileName); |
|
67
|
|
|
}); |
|
68
|
|
|
$this->adapter->addFunction($function); |
|
69
|
|
|
// @codeCoverageIgnoreEnd |
|
70
|
4 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Renders the template for the output. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $template |
|
76
|
|
|
* @param array $parameters |
|
77
|
|
|
* |
|
78
|
|
|
* @throws InvalidArgumentException |
|
79
|
|
|
* |
|
80
|
|
|
* @return StreamInterface |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function render($template, $parameters = []) |
|
83
|
|
|
{ |
|
84
|
2 |
|
if ($this->config->has('map/'.$template)) { |
|
85
|
2 |
|
$template = $this->config->getData('map/'.$template); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
if (!file_exists($this->templateViewPath.'/'.$template)) { |
|
89
|
1 |
|
throw new InvalidArgumentException(sprintf('Unable to render file: "%s". No such file.', $template)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Tell the template where the resources are. |
|
93
|
2 |
|
$parameters['template_resource_path'] = $this->templateResourcePath; |
|
94
|
2 |
|
$parameters['application_base_uri'] = $this->applicationBaseUri; |
|
95
|
|
|
|
|
96
|
2 |
|
$output = $this->adapter->render($template, $parameters); |
|
97
|
|
|
|
|
98
|
|
|
// The ugliest shit ever. But that is how they made it... :/ |
|
99
|
2 |
|
return \GuzzleHttp\Psr7\stream_for($output); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|