Completed
Push — master ( f3d414...a522f9 )
by Park Jong-Hun
09:11
created

TwigView::addFileFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285
1
<?php
2
3
namespace App\ViewEngine;
4
5
use Core\View\ViewEngineInterface;
6
use Core\Application;
7
use \Twig_Loader_Filesystem;
8
use \Twig_Environment;
9
use \Twig_SimpleFunction;
10
11
class TwigView implements ViewEngineInterface
12
{
13
    /**
14
     * @var Twig_Environment
15
     */
16
    protected $twig;
17
18
    /**
19
     * Twig template file
20
     *
21
     * @var string
22
     */
23
    private $templateFilename = '';
24
25
    /**
26
     * @var array engine setting
27
     */
28
    private $settings = [];
29
30
    /**
31
     * rendering template variables
32
     * @var array
33
     */
34
    private $var = [];
35
36
    public function __construct($settings = [])
37
    {
38
        $loader = new Twig_Loader_Filesystem($settings['path']);
39
        $this->twig = new Twig_Environment($loader, $settings['settings']);
40
41
        $this->settings = $settings;
42
43
        $this->addCssFunction();
44
        $this->addAssetFunction();
45
        $this->addUrlFunction();
46
        $this->addFileFunction();
47
    }
48
49
    public function set($key, $value)
50
    {
51
        $this->var[$key] = $value;
52
    }
53
54
    public function getVariables()
55
    {
56
        return $this->var;
57
    }
58
59
    public function file($fileName)
60
    {
61
        $this->templateFilename = $fileName . $this->settings['postfix'];
62
    }
63
64
    public function getFile()
65
    {
66
        return $this->templateFilename;
67
    }
68
69
    public function render()
70
    {
71
        return $this->twig->render($this->templateFilename, $this->var);
72
    }
73
74
    private function addCssFunction()
75
    {
76
        $this->twig->addFunction(new Twig_SimpleFunction(
77
            'css',
78
            [$this, 'cssFunction'],
79
            ['is_safe' => ['html']]
80
        ));
81
    }
82
83
    private function addAssetFunction()
84
    {
85
        $this->twig->addFunction(new Twig_SimpleFunction(
86
            'asset',
87
            [$this, 'assetFunction']
88
        ));
89
    }
90
91
    private function addUrlFunction()
92
    {
93
        $this->twig->addFunction(new Twig_SimpleFunction(
94
            'url',
95
            [$this, 'urlFunction']
96
        ));
97
    }
98
99
    private function addFileFunction()
100
    {
101
        $this->twig->addFunction(new Twig_SimpleFunction(
102
            'file',
103
            [$this, 'fileFunction']
104
        ));
105
    }
106
107
    public function cssFunction($url)
108
    {
109
        return sprintf('<link rel="stylesheet" type="text/css" href="%s">', $url);
110
    }
111
112
    public function assetFunction($file)
113
    {
114
        return Application::getPublicUrl($file);
115
    }
116
117
    public function urlFunction($url = '')
118
    {
119
        return Application::getUrl($url);
120
    }
121
122
    public function fileFunction($file)
123
    {
124
        $pos = strrpos($this->getFile(), '/');
125
126
        if($pos === false) {
127
            return $file . $this->settings['postfix'];
128
        }
129
130
        return substr($this->getFile(), 0, $pos) . '/' . $file . $this->settings['postfix'];
131
    }
132
}
133