Slim::baseUrl()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
1
<?php
2
3
namespace Resilient\Twig\Extension;
4
5
use \Slim\Http\Uri as SlimUri;
6
use \Slim\Interfaces\RouterInterface;
7
8
/**
9
* Twig Router Extension for Slim Routing
10
*/
11
class Slim extends \Twig_Extension
12
{
13
    private $uri;
14
    private $router;
15
16
    public function __construct(RouterInterface $router, SlimUri $uri)
17
    {
18
        $this->router = $router;
19
        $this->uri = $uri;
20
    }
21
22
    public function getName()
23
    {
24
        return 'slim kln';
25
    }
26
27
    public function getFunctions()
28
    {
29
        return [
30
            new \Twig_SimpleFunction('path_for', array($this, 'pathFor')),
31
            new \Twig_SimpleFunction('base_url', array($this, 'baseUrl')),
32
            new \Twig_SimpleFunction('is_current_path', array($this, 'isCurrentPath')),
33
        ];
34
    }
35
36
    /**
37
     * pathFor function.
38
     *
39
     * @access public
40
     * @param mixed $name
41
     * @param mixed $data (default: [])
42
     * @param mixed $queryParams (default: [])
43
     * @return string
44
     */
45
    public function pathFor($name, $data = [], $queryParams = [])
46
    {
47
        return $this->router->pathFor($name, $data, $queryParams);
48
    }
49
50
    /**
51
     * baseUrl function.
52
     *
53
     * @access public
54
     * @return string
55
     */
56
    public function baseUrl()
57
    {
58
        if (is_string($this->uri)) {
59
            return $this->uri;
60
        }
61
62
        if (method_exists($this->uri, 'getBaseUrl')) {
63
            return $this->uri->getBaseUrl();
64
        }
65
    }
66
67
    /**
68
     * isCurrentPath function.
69
     *
70
     * @access public
71
     * @param mixed $name
72
     * @return boolean
73
     */
74
    public function isCurrentPath($name)
75
    {
76
        return $this->router->pathFor($name) === $this->uri->getPath();
77
    }
78
79
    /**
80
     * Set the base url
81
     *
82
     * @param string|Slim\Http\Uri $baseUrl
83
     * @return void
84
     */
85
    public function setBaseUrl($baseUrl)
86
    {
87
        $this->uri = $baseUrl;
88
    }
89
}
90