Uri::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Resilient\Twig\Extension;
4
5
use \Psr\Http\Message\UriInterface;
6
7
/**
8
* Twig Router Extension for psr 6  UriInterface
9
*/
10
class Uri extends \Twig_Extension
11
{
12
    private $uri;
13
14
    public function __construct(UriInterface $uri)
15
    {
16
        $this->uri = $uri;
17
    }
18
19
    public function getFunctions()
20
    {
21
        return [
22
            new \Twig_SimpleFunction('base_url', array($this, 'baseUrl')),
23
            new \Twig_SimpleFunction('current_path', array($this, 'currentPath')),
24
            new \Twig_SimpleFunction('current_url', array($this, 'currentUrl')),
25
        ];
26
    }
27
28
    /**
29
     * baseUrl function.
30
     *
31
     * @access public
32
     * @return string uri
33
     */
34
    public function baseUrl()
35
    {
36
        if (is_string($this->uri)) {
37
            return $this->uri;
38
        }
39
40
        $scheme = $this->uri->getScheme();
41
        $authority = $this->uri->getAuthority();
42
43
        return ($scheme ? $scheme . ':' : '')
44
            . ($authority ? '//' . $authority : '');
45
    }
46
47
    /**
48
     * currentPath function.
49
     *
50
     * @access public
51
     * @return string
52
     */
53
    public function currentPath()
54
    {
55
        return $this->uri->getPath();
56
    }
57
58
    /**
59
     * currentUrl function.
60
     *
61
     * @access public
62
     * @return string
63
     */
64
    public function currentUrl()
65
    {
66
        return (string) $this->uri;
67
    }
68
}
69