1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the jade/jade package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jade\Twig; |
13
|
|
|
|
14
|
|
|
use Jade\Routing\Router; |
15
|
|
|
use Psr\Http\Message\UriInterface; |
16
|
|
|
|
17
|
|
|
class TwigExtension extends \Twig\Extension\AbstractExtension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Router |
21
|
|
|
*/ |
22
|
|
|
private $router; |
23
|
|
|
/** |
24
|
|
|
* @var string|UriInterface |
25
|
|
|
*/ |
26
|
|
|
private $uri; |
27
|
|
|
|
28
|
|
|
public function __construct($router, $uri) |
29
|
|
|
{ |
30
|
|
|
$this->router = $router; |
31
|
|
|
$this->uri = $uri; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getName() |
35
|
|
|
{ |
36
|
|
|
return 'slim'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getFunctions() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
new \Twig\TwigFunction('path_for', array($this, 'pathFor')), |
43
|
|
|
new \Twig\TwigFunction('full_url_for', array($this, 'fullUrlFor')), |
44
|
|
|
new \Twig\TwigFunction('base_url', array($this, 'baseUrl')), |
45
|
|
|
new \Twig\TwigFunction('is_current_path', array($this, 'isCurrentPath')), |
46
|
|
|
new \Twig\TwigFunction('current_path', array($this, 'currentPath')), |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function pathFor($name, $data = [], $queryParams = [], $appName = 'default') |
51
|
|
|
{ |
52
|
|
|
return $this->router->pathFor($name, $data, $queryParams); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Similar to pathFor but returns a fully qualified URL |
57
|
|
|
* |
58
|
|
|
* @param string $name The name of the route |
59
|
|
|
* @param array $data Route placeholders |
60
|
|
|
* @param array $queryParams |
61
|
|
|
* @param string $appName |
62
|
|
|
* @return string fully qualified URL |
63
|
|
|
*/ |
64
|
|
|
public function fullUrlFor($name, $data = [], $queryParams = [], $appName = 'default') |
65
|
|
|
{ |
66
|
|
|
$path = $this->pathFor($name, $data, $queryParams, $appName); |
67
|
|
|
/** @var Uri $uri */ |
68
|
|
|
if (is_string($this->uri)) { |
69
|
|
|
$uri = Uri::createFromString($this->uri); |
70
|
|
|
} else { |
71
|
|
|
$uri = $this->uri; |
72
|
|
|
} |
73
|
|
|
$scheme = $uri->getScheme(); |
74
|
|
|
$authority = $uri->getAuthority(); |
75
|
|
|
$host = ($scheme ? $scheme . ':' : '') |
76
|
|
|
. ($authority ? '//' . $authority : ''); |
77
|
|
|
return $host . $path; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function baseUrl() |
81
|
|
|
{ |
82
|
|
|
if (is_string($this->uri)) { |
83
|
|
|
return $this->uri; |
84
|
|
|
} |
85
|
|
|
if (method_exists($this->uri, 'getBaseUrl')) { |
86
|
|
|
return $this->uri->getBaseUrl(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function isCurrentPath($name, $data = []) |
91
|
|
|
{ |
92
|
|
|
return $this->router->pathFor($name, $data) === $this->uri->getBasePath() . '/' . ltrim($this->uri->getPath(), '/'); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns current path on given URI. |
97
|
|
|
* |
98
|
|
|
* @param bool $withQueryString |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function currentPath($withQueryString = false) |
102
|
|
|
{ |
103
|
|
|
if (is_string($this->uri)) { |
104
|
|
|
return $this->uri; |
105
|
|
|
} |
106
|
|
|
$path = $this->uri->getBasePath() . '/' . ltrim($this->uri->getPath(), '/'); |
|
|
|
|
107
|
|
|
if ($withQueryString && '' !== $query = $this->uri->getQuery()) { |
108
|
|
|
$path .= '?' . $query; |
109
|
|
|
} |
110
|
|
|
return $path; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the base url |
115
|
|
|
* |
116
|
|
|
* @param string|Slim\Http\Uri $baseUrl |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function setBaseUrl($baseUrl) |
120
|
|
|
{ |
121
|
|
|
$this->uri = $baseUrl; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.