Completed
Push — master ( 9efb49...7ad915 )
by Park Jong-Hun
03:23
created

Twig   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 12
c 7
b 0
f 0
lcom 2
cbo 4
dl 0
loc 105
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 4 1
A getVariables() 0 4 1
A file() 0 4 1
A getFile() 0 4 1
A render() 0 4 1
A addCssFunction() 0 8 1
A addAssetFunction() 0 7 1
A addUrlFunction() 0 7 1
A cssFunction() 0 4 1
A assetFunction() 0 4 1
A urlFunction() 0 4 1
A __construct() 0 11 1
1
<?php
2
3
namespace App\ViewEngine;
4
5
use Core\ViewEngineInterface;
6
use Core\Application;
7
use \Twig_Loader_Filesystem;
8
use \Twig_Environment;
9
use \Twig_SimpleFunction;
10
11
class Twig implements ViewEngineInterface
12
{
13
14
    private $PUBLIC_PATH = 'public/';
15
16
    /**
17
     * @var Twig_Environment
18
     */
19
    private $twig;
20
21
    /**
22
     * Twig template file
23
     *
24
     * @var string
25
     */
26
    private $templateFilename = '';
27
28
    /**
29
     * @var array engine setting
30
     */
31
    private $settings = [];
32
33
    /**
34
     * rendering template variables
35
     * @var array
36
     */
37
    private $var = [];
38
39
    public function __construct($settings = [])
40
    {
41
        $loader = new Twig_Loader_Filesystem($settings['path']);
42
        $this->twig = new Twig_Environment($loader, $settings['settings']);
43
44
        $this->settings = $settings;
45
46
        $this->addCssFunction();
47
        $this->addAssetFunction();
48
        $this->addUrlFunction();
49
    }
50
51
    public function set($key, $value)
52
    {
53
        $this->var[$key] = $value;
54
    }
55
56
    public function getVariables()
57
    {
58
        return $this->var;
59
    }
60
61
    public function file($fileName)
62
    {
63
        $this->templateFilename = $fileName . $this->settings['postfix'];
64
    }
65
66
    public function getFile()
67
    {
68
        return $this->templateFilename;
69
    }
70
71
    public function render()
72
    {
73
        echo $this->twig->render($this->templateFilename, $this->var);
74
    }
75
76
    private function addCssFunction()
77
    {
78
        $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...
79
            'css',
80
            [$this, 'cssFunction'],
81
            ['is_safe' => ['html']]
82
        ));
83
    }
84
85
    private function addAssetFunction()
86
    {
87
        $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...
88
            'asset',
89
            [$this, 'assetFunction']
90
        ));
91
    }
92
93
    private function addUrlFunction()
94
    {
95
        $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...
96
            'url',
97
            [$this, 'urlFunction']
98
        ));
99
    }
100
101
    public function cssFunction($url)
102
    {
103
        return sprintf('<link rel="stylesheet" type="text/css" href="%s">', $url);
104
    }
105
106
    public function assetFunction($file)
107
    {
108
        return Application::getInstance()->url($this->PUBLIC_PATH . $file);
109
    }
110
111
    public function urlFunction($url = '')
112
    {
113
        return Application::getInstance()->url($url);
114
    }
115
}
116