Completed
Push — master ( 435119...3a6053 )
by Park Jong-Hun
04:19
created

TwigView::assetFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
    private $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
    }
47
48
    public function set($key, $value)
49
    {
50
        $this->var[$key] = $value;
51
    }
52
53
    public function getVariables()
54
    {
55
        return $this->var;
56
    }
57
58
    public function file($fileName)
59
    {
60
        $this->templateFilename = $fileName . $this->settings['postfix'];
61
    }
62
63
    public function getFile()
64
    {
65
        return $this->templateFilename;
66
    }
67
68
    public function render()
69
    {
70
        echo $this->twig->render($this->templateFilename, $this->var);
71
    }
72
73
    private function addCssFunction()
74
    {
75
        $this->twig->addFunction(new Twig_SimpleFunction(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
76
            'css',
77
            [$this, 'cssFunction'],
78
            ['is_safe' => ['html']]
79
        ));
80
    }
81
82
    private function addAssetFunction()
83
    {
84
        $this->twig->addFunction(new Twig_SimpleFunction(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
85
            'asset',
86
            [$this, 'assetFunction']
87
        ));
88
    }
89
90
    private function addUrlFunction()
91
    {
92
        $this->twig->addFunction(new Twig_SimpleFunction(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
93
            'url',
94
            [$this, 'urlFunction']
95
        ));
96
    }
97
98
    public function cssFunction($url)
99
    {
100
        return sprintf('<link rel="stylesheet" type="text/css" href="%s">', $url);
101
    }
102
103
    public function assetFunction($file)
104
    {
105
        return Application::getInstance()->getPublicUrl($file);
106
    }
107
108
    public function urlFunction($url = '')
109
    {
110
        return Application::getInstance()->url($url);
111
    }
112
}
113