Failed Conditions
Push — master ( 14bc7d...3905a2 )
by Arnold
10:19 queued 10s
created

Twig::createTwigFunction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
nc 2
cc 3
eloc 3
nop 2
1
<?php
2
3
namespace Jasny\Controller\View;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * View using Twig
10
 */
11
trait Twig
12
{
13
    /**
14
     * Twig environment
15
     * @var \Twig_Environment
16
     */
17
    protected $twig = null;
18
19
    /**
20
     * Get server request
21
     * @return ServerRequestInterface
22
     */
23
    abstract public function getRequest();
24
25
    /**
26
     * Get server response
27
     * @return ResponseInterface
28
     */
29
    abstract public function getResponse();
30
31
    /**
32
     * Add a global variable to the view.
33
     * 
34
     * @param string $name   Variable name
35
     * @param mixed  $value
36
     * @return $this
37
     */
38
    public function setViewVariable($name, $value)
39
    {
40
        if (!$name) throw new \InvalidArgumentException("Name should not be empty");        
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
41
42
        $this->getTwig()->addGlobal($name, $value);
43
44
        return $this;
45
    }
46
    
47
    /**
48
     * Expose a function to the view.
49
     * 
50
     * @param string $function  Variable name
51
     * @param mixed  $callback
0 ignored issues
show
Bug introduced by
There is no parameter named $callback. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
     * @param string $as        'function' or 'filter'
53
     * @return $this
54
     */
55
    public function setViewFunction($name, $function = null, $as = 'function')
56
    {
57
        if ($as === 'function') {
58
            $this->getTwig()->addFunction($this->createTwigFunction($name, $function));
0 ignored issues
show
Bug introduced by
It seems like $function defined by parameter $function on line 55 can also be of type null; however, Jasny\Controller\View\Twig::createTwigFunction() does only seem to accept callable, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
59
        } elseif ($as === 'filter') {
60
            $this->getTwig()->addFilter($this->createTwigFilter($name, $function));
0 ignored issues
show
Bug introduced by
It seems like $function defined by parameter $function on line 55 can also be of type null; however, Jasny\Controller\View\Twig::createTwigFilter() does only seem to accept callable, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
        } else {
62
            throw new \InvalidArgumentException("You should create either function or filter, not '$as'");
63
        }
64
        
65
        return $this;
66
    }
67
68
    /**
69
     * Add extension to view
70
     *
71
     * @param object $extension
72
     * @return $this
73
     */
74
    public function setViewExtension($extension)
75
    {
76
        $this->getTwig()->addExtension($extension);
77
78
        return $this;
79
    }
80
81
    /**
82
     * View rendered template
83
     *
84
     * @param string $name   Template name
85
     * @param array $context Template context
86
     * @return ResponseInterface
87
     */
88
    public function view($name, array $context = [])
89
    {
90
        if (!pathinfo($name, PATHINFO_EXTENSION)) $name .= '.html.twig';
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
91
92
        $twig = $this->getTwig();
93
        $tmpl = $twig->loadTemplate($name);
94
95
        $response = $this->getResponse();
96
        $response = $response->withHeader('Content-Type', 'text/html; charset=' . $twig->getCharset());
97
        $response->getBody()->write($tmpl->render($context));            
98
99
        return $response;
100
    }
101
102
    /**
103
     * Get twig environment
104
     *
105
     * @return \Twig_Environment
106
     */
107
    public function getTwig()
108
    {
109
        if ($this->twig) return $this->twig;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
110
111
        $loader = $this->getTwigLoader();
112
        $this->twig = $this->getTwigEnvironment($loader);
113
114
        $extensions = ['DateExtension', 'PcreExtension', 'TextExtension', 'ArrayExtension'];
115
        foreach ($extensions as $name) {
116
            $class = "Jasny\Twig\\$name";
117
118
            if (class_exists($class)) $this->setViewExtension(new $class());
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
119
        }
120
        
121
        $uri = $this->getRequest()->getUri()->getPath();
122
        $this->setViewVariable('current_url', $uri);
123
124
        return $this->twig;
125
    }
126
127
    /**
128
     * Get twig loasder for current working directory
129
     *
130
     * @return \Twig_Loader_Filesystem
131
     */
132
    public function getTwigLoader()
133
    {
134
        return new \Twig_Loader_Filesystem(getcwd());
135
    }
136
137
    /**
138
     * Get twig environment instance
139
     *
140
     * @param \Twig_Loader_Filesystem $loader
141
     * @return \Twig_Environment
142
     */
143
    public function getTwigEnvironment(\Twig_Loader_Filesystem $loader)
144
    {
145
        return new \Twig_Environment($loader);
146
    }
147
148
    /**
149
     * Create twig function
150
     *
151
     * @param string $name          Name of function in view
152
     * @param callable $function 
153
     * @return \Twig_SimpleFunction
154
     */
155
    public function createTwigFunction($name, $function)
156
    {
157
        if (!$name) throw new \InvalidArgumentException("Function name should not be empty");
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
158
159
        return new \Twig_SimpleFunction($name, $function ?: $name);
160
    }
161
162
    /**
163
     * Create twig filter
164
     *
165
     * @param string $name          Name of filter in view
166
     * @param callable $function 
167
     * @return \Twig_SimpleFilter
168
     */
169
    public function createTwigFilter($name, $function)
170
    {
171
        if (!$name) throw new \InvalidArgumentException("Filter name should not be empty");
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
172
173
        return new \Twig_SimpleFilter($name, $function ?: $name);
174
    }
175
}
176