forked from mezzio/mezzio-twigrenderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwigExtension.php
123 lines (111 loc) · 3.95 KB
/
TwigExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace Mezzio\Twig;
use Mezzio\Helper\ServerUrlHelper;
use Mezzio\Helper\UrlHelper;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Twig\TwigFunction;
/**
* Twig extension for rendering URLs and assets URLs from Mezzio.
*/
class TwigExtension extends AbstractExtension implements GlobalsInterface
{
public function __construct(
private readonly ServerUrlHelper $serverUrlHelper,
private readonly UrlHelper $urlHelper,
private readonly ?string $assetsUrl,
private readonly null|string|int $assetsVersion,
private readonly array $globals = []
) {
}
public function getGlobals(): array
{
return $this->globals;
}
/**
* @return TwigFunction[]
*/
public function getFunctions(): array
{
return [
new TwigFunction('absolute_url', [$this, 'renderUrlFromPath']),
new TwigFunction('asset', [$this, 'renderAssetUrl']),
new TwigFunction('path', [$this, 'renderUri']),
new TwigFunction('url', [$this, 'renderUrl']),
];
}
/**
* Render relative uri for a given named route
*
* Usage: {{ path('article_show', {'id': '3'}) }}
* Generates: /article/3
*
* Usage: {{ path('article_show', {'id': '3'}, {'foo': 'bar'}, 'fragment') }}
* Generates: /article/3?foo=bar#fragment
*
* @param array $routeParams
* @param array $queryParams
* @param array $options Can have the following keys:
* - reuse_result_params (bool): indicates if the current
* RouteResult parameters will be used, defaults to true
*/
public function renderUri(
?string $route = null,
array $routeParams = [],
array $queryParams = [],
?string $fragmentIdentifier = null,
array $options = []
): string {
return $this->urlHelper->generate($route, $routeParams, $queryParams, $fragmentIdentifier, $options);
}
/**
* Render absolute url for a given named route
*
* Usage: {{ url('article_show', {'slug': 'article.slug'}) }}
* Generates: http://example.com/article/article.slug
*
* Usage: {{ url('article_show', {'id': '3'}, {'foo': 'bar'}, 'fragment') }}
* Generates: http://example.com/article/3?foo=bar#fragment
*
* @param array $routeParams
* @param array $queryParams
* @param array $options Can have the following keys:
* - reuse_result_params (bool): indicates if the current
* RouteResult parameters will be used, defaults to true
*/
public function renderUrl(
?string $route = null,
array $routeParams = [],
array $queryParams = [],
?string $fragmentIdentifier = null,
array $options = []
): string {
return $this->serverUrlHelper->generate(
$this->renderUri($route, $routeParams, $queryParams, $fragmentIdentifier, $options)
);
}
/**
* Render absolute url from a path
*
* Usage: {{ absolute_url('path/to/something') }}
* Generates: http://example.com/path/to/something
*/
public function renderUrlFromPath(?string $path = null): string
{
return $this->serverUrlHelper->generate($path);
}
/**
* Render asset url, optionally versioned
*
* Usage: {{ asset('path/to/asset/name.ext', version=3) }}
* Generates: path/to/asset/name.ext?v=3
*/
public function renderAssetUrl(string $path, ?string $version = null): string
{
$assetsVersion = $version !== null && $version !== '' ? $version : $this->assetsVersion;
// One more time, in case $this->assetsVersion was null or an empty string
$assetsVersion = $assetsVersion !== null && $assetsVersion !== '' ? '?v=' . $assetsVersion : '';
return $this->assetsUrl . $path . $assetsVersion;
}
}