Completed
Push — master ( 40a686...578b2a )
by Gabor
06:04
created

TwigRendererAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1.0013

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 16
cts 18
cp 0.8889
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 3
crap 1.0013
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 4
        $function = new Twig_SimpleFunction('defined', function ($fileName) use ($viewPath) {
63
            $fileName = str_replace('@Theme', $viewPath, $fileName);
64
            return file_exists($fileName);
65 4
        });
66 4
        $this->adapter->addFunction($function);
67 4
    }
68
69
    /**
70
     * Renders the template for the output.
71
     *
72
     * @param string $template
73
     * @param array  $parameters
74
     *
75
     * @throws InvalidArgumentException
76
     *
77
     * @return StreamInterface
78
     */
79 2
    public function render($template, $parameters = [])
80
    {
81 2
        if ($this->config->has('map/'.$template)) {
82 2
            $template = $this->config->getData('map/'.$template);
83 2
        }
84
85 2
        if (!file_exists($this->templateViewPath.'/'.$template)) {
86 1
            throw new InvalidArgumentException(sprintf('Unable to render file: "%s". No such file.', $template));
87
        }
88
89
        // Tell the template where the resources are.
90 2
        $parameters['template_resource_path'] = $this->templateResourcePath;
91 2
        $parameters['application_base_uri'] = $this->applicationBaseUri;
92
93 2
        $output = $this->adapter->render($template, $parameters);
94
95
        // The ugliest shit ever. But that is how they made it... :/
96 2
        return \GuzzleHttp\Psr7\stream_for($output);
97
    }
98
}
99