1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Zikula package. |
5
|
|
|
* |
6
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Zikula\RoutesModule\Twig; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
use Twig\TwigFilter; |
18
|
|
|
use Zikula\RoutesModule\Entity\RouteEntity; |
19
|
|
|
use Zikula\RoutesModule\Helper\PathBuilderHelper; |
20
|
|
|
use Zikula\RoutesModule\Twig\Base\AbstractTwigExtension; |
21
|
|
|
use Zikula\SettingsModule\Api\ApiInterface\LocaleApiInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Twig extension implementation class. |
25
|
|
|
*/ |
26
|
|
|
class TwigExtension extends AbstractTwigExtension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ContainerInterface |
30
|
|
|
*/ |
31
|
|
|
private $container; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LocaleApiInterface |
35
|
|
|
*/ |
36
|
|
|
private $localeApi; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var PathBuilderHelper |
40
|
|
|
*/ |
41
|
|
|
private $pathBuilderHelper; |
42
|
|
|
|
43
|
|
|
public function getFunctions() |
44
|
|
|
{ |
45
|
|
|
return []; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getFilters() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
new TwigFilter('zikularoutesmodule_listEntry', [$this, 'getListEntry']), // from base class |
52
|
|
|
new TwigFilter('zikularoutesmodule_formattedTitle', [$this, 'getFormattedEntityTitle']), // from base class |
53
|
|
|
new TwigFilter('zikularoutesmodule_arrayToString', [$this, 'displayArrayAsString'], ['is_safe' => ['html']]), |
54
|
|
|
new TwigFilter('zikularoutesmodule_pathToString', [$this, 'displayPathAsString'], ['is_safe' => ['html']]) |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The zikularoutesmodule_arrayToString filter displays the content of a given array. |
60
|
|
|
* Example: |
61
|
|
|
* {{ route.defaults|zikularoutesmodule_arrayToString }} |
62
|
|
|
*/ |
63
|
|
|
public function displayArrayAsString(array $input = []): string |
64
|
|
|
{ |
65
|
|
|
return '<pre>' . print_r($input, true) . '</pre>'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The zikularoutesmodule_pathToString filter displays a route's path. |
70
|
|
|
* Example: |
71
|
|
|
* {{ route.path|zikularoutesmodule_pathToString(route) }} |
72
|
|
|
*/ |
73
|
|
|
public function displayPathAsString(string $path, RouteEntity $route): string |
74
|
|
|
{ |
75
|
|
|
$prefix = ''; |
76
|
|
|
$translationPrefix = $route->getTranslationPrefix(); |
77
|
|
|
if (!empty($translationPrefix)) { |
78
|
|
|
$prefix = '/' . $translationPrefix; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($route->getTranslatable()) { |
82
|
|
|
$languages = $this->localeApi->getSupportedLocales(); |
83
|
|
|
$isRequiredLangParameter = $this->variableApi->getSystemVar('languageurl', 0); |
84
|
|
|
if (!$isRequiredLangParameter) { |
85
|
|
|
$defaultLanguage = $this->variableApi->getSystemVar('locale'); |
86
|
|
|
unset($languages[array_search($defaultLanguage, $languages, true)]); |
87
|
|
|
} |
88
|
|
|
if (count($languages) > 0) { |
89
|
|
|
$prefix = ($isRequiredLangParameter ? '/' : '{/') . implode('|', $languages) . ($isRequiredLangParameter ? '' : '}'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$prefix = htmlspecialchars($prefix); |
94
|
|
|
$path = htmlspecialchars( |
95
|
|
|
$this->pathBuilderHelper->getPathWithBundlePrefix($route) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$container = $this->container; |
99
|
|
|
$path = preg_replace_callback('#%(.*?)%#', static function ($matches) use ($container) { |
100
|
|
|
return '<abbr title="' . htmlspecialchars($matches[0]) . '">' |
101
|
|
|
. htmlspecialchars($container->getParameter($matches[1])) |
102
|
|
|
. '</abbr>' |
103
|
|
|
; |
104
|
|
|
}, $path); |
105
|
|
|
|
106
|
|
|
$defaults = $route->getDefaults(); |
107
|
|
|
$requirements = $route->getRequirements(); |
108
|
|
|
$path = preg_replace_callback('#{(.*?)}#', function ($matches) use ($defaults, $requirements) { |
109
|
|
|
$title = ''; |
110
|
|
|
if (isset($defaults[$matches[1]])) { |
111
|
|
|
$title .= $this->trans('Default: %value%', ['%value%' => htmlspecialchars($defaults[$matches[1]])]); |
112
|
|
|
} |
113
|
|
|
if (isset($requirements[$matches[1]])) { |
114
|
|
|
if ('' !== $title) { |
115
|
|
|
$title .= ' | '; |
116
|
|
|
} |
117
|
|
|
$title .= $this->trans('Requirement: %value%', ['%value%' => htmlspecialchars($requirements[$matches[1]])]); |
118
|
|
|
} |
119
|
|
|
if ('' === $title) { |
120
|
|
|
return $matches[0]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return '<abbr title="' . $title . '">' . $matches[0] . '</abbr>'; |
124
|
|
|
}, $path); |
125
|
|
|
|
126
|
|
|
return $prefix . '<strong>' . $path . '</strong>'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @required |
131
|
|
|
*/ |
132
|
|
|
public function setAdditionalDependencies( |
133
|
|
|
ContainerInterface $container, |
134
|
|
|
LocaleApiInterface $localeApi, |
135
|
|
|
PathBuilderHelper $pathBuilderHelper |
136
|
|
|
): void { |
137
|
|
|
$this->container = $container; |
138
|
|
|
$this->localeApi = $localeApi; |
139
|
|
|
$this->pathBuilderHelper = $pathBuilderHelper; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|