1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Templating\Twig\Extension; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Document\RepeaterPageView; |
11
|
|
|
use allejo\stakx\Service; |
12
|
|
|
use Twig_Environment; |
13
|
|
|
|
14
|
|
|
class BaseUrlFunction extends AbstractTwigExtension implements TwigFunctionInterface |
15
|
|
|
{ |
16
|
|
|
private $site; |
17
|
|
|
|
18
|
18 |
|
public function __invoke(Twig_Environment $env, $assetPath, $absolute = false, $params = []) |
19
|
|
|
{ |
20
|
18 |
|
$globals = $env->getGlobals(); |
21
|
18 |
|
$this->site = $globals['site']; |
22
|
|
|
|
23
|
18 |
|
$url = $this->getUrl($absolute); |
24
|
18 |
|
$baseURL = $this->getBaseUrl(); |
25
|
18 |
|
$permalink = $this->guessAssetPath($assetPath, $params); |
26
|
|
|
|
27
|
18 |
|
return $this->buildPermalink($url, $baseURL, $permalink); |
28
|
|
|
} |
29
|
|
|
|
30
|
18 |
|
public static function get() |
31
|
|
|
{ |
32
|
18 |
|
return new \Twig_SimpleFunction('url', new self(), [ |
33
|
18 |
|
'needs_environment' => true, |
34
|
|
|
]); |
35
|
|
|
} |
36
|
|
|
|
37
|
18 |
|
private function getUrl($absolute) |
38
|
|
|
{ |
39
|
18 |
|
$url = '/'; |
40
|
|
|
|
41
|
18 |
|
if (!$absolute) |
42
|
|
|
{ |
43
|
16 |
|
return $url; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
if (isset($this->site['url'])) |
47
|
|
|
{ |
48
|
2 |
|
$url = $this->site['url']; |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
return ltrim($url, '/'); |
52
|
|
|
} |
53
|
|
|
|
54
|
18 |
|
private function getBaseUrl() |
|
|
|
|
55
|
|
|
{ |
56
|
18 |
|
$base = ''; |
57
|
|
|
|
58
|
18 |
|
if (isset($this->site['baseurl'])) |
59
|
|
|
{ |
60
|
15 |
|
$base = $this->site['baseurl']; |
61
|
|
|
} |
62
|
3 |
|
elseif (isset($this->site['base'])) |
63
|
|
|
{ |
64
|
|
|
$base = $this->site['base']; |
65
|
|
|
} |
66
|
|
|
|
67
|
18 |
|
return $base; |
68
|
|
|
} |
69
|
|
|
|
70
|
18 |
|
private function guessAssetPath($assetPath, $params) |
71
|
|
|
{ |
72
|
18 |
|
if ($assetPath instanceof RepeaterPageView) |
73
|
|
|
{ |
74
|
|
|
return ($link = $assetPath->getPermalinkWhere($params)) ? $link : '/'; |
75
|
|
|
} |
76
|
18 |
|
elseif (is_array($assetPath) || ($assetPath instanceof \ArrayAccess)) |
77
|
|
|
{ |
78
|
4 |
|
return (isset($assetPath['permalink'])) ? $assetPath['permalink'] : '/'; |
79
|
|
|
} |
80
|
14 |
|
elseif (is_null($assetPath)) |
81
|
|
|
{ |
82
|
1 |
|
return '/'; |
83
|
|
|
} |
84
|
13 |
|
elseif ($assetPath instanceof \SplFileInfo) |
85
|
|
|
{ |
86
|
|
|
return str_replace(Service::getWorkingDirectory(), '', $assetPath); |
87
|
|
|
} |
88
|
|
|
|
89
|
13 |
|
return $assetPath; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @see https://stackoverflow.com/a/15575293 |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
18 |
View Code Duplication |
private function buildPermalink() |
|
|
|
|
98
|
|
|
{ |
99
|
18 |
|
$paths = []; |
100
|
|
|
|
101
|
18 |
|
foreach (func_get_args() as $arg) |
102
|
|
|
{ |
103
|
18 |
|
if ($arg !== '') |
104
|
|
|
{ |
105
|
18 |
|
$paths[] = $arg; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
18 |
|
return preg_replace('#(?<!:)/+#', '/', join('/', $paths)); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.