Skip to content

Commit eb07284

Browse files
Merge pull request #19756 from MauricioFauth/twig-attribute-ext
Refactor Twig extensions to use Twig's AttributeExtension
2 parents a11cc9e + 9c576fc commit eb07284

25 files changed

Lines changed: 166 additions & 420 deletions

app/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
'table_maintenance' => ['class' => PhpMyAdmin\Table\Maintenance::class, 'arguments' => ['$dbi' => '@dbi']],
205205
'table_search' => ['class' => Search::class, 'arguments' => ['$dbi' => '@dbi']],
206206
'template' => ['class' => Template::class, 'arguments' => ['$config' => '@config']],
207-
ThemeManager::class => ['class' => PhpMyAdmin\Theme\ThemeManager::class],
207+
ThemeManager::class => ['class' => ThemeManager::class],
208208
'tracking' => [
209209
'class' => Tracking::class,
210210
'arguments' => [

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"symfony/expression-language": "^7.1",
7676
"symfony/polyfill-ctype": "^1.24",
7777
"symfony/polyfill-mbstring": "^1.24",
78-
"twig/twig": "^3.19",
78+
"twig/twig": "^3.21",
7979
"webmozart/assert": "^1.10",
8080
"williamdes/mariadb-mysql-kbs": "^1.2"
8181
},

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BrowseForeigners.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function getHtmlForOneKey(
104104
]);
105105

106106
$output .= '<td width="20%"><img src="'
107-
. $this->themeManager->theme->getImgPath('spacer.png')
107+
. $this->themeManager->getThemeImagePath('spacer.png')
108108
. '" alt="" width="1" height="1"></td>';
109109

110110
$output .= $this->template->render('table/browse_foreigners/column_element', [

src/Core.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpMyAdmin\Error\ErrorHandler;
88
use PhpMyAdmin\Exceptions\MissingExtensionException;
99
use PhpMyAdmin\Http\ServerRequest;
10+
use Twig\Attribute\AsTwigFilter;
1011

1112
use function __;
1213
use function array_pop;
@@ -411,6 +412,7 @@ public static function arrayRemove(string $path, array &$array): void
411412
*
412413
* @return string URL for a link.
413414
*/
415+
#[AsTwigFilter('link')]
414416
public static function linkURL(string $url): string
415417
{
416418
if (preg_match('#^https?://#', $url) !== 1) {

src/Display/Results.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ private function getFullOrPartialTextButtonOrLink(): string
11211121
$themeManager = ContainerBuilder::getContainer()->get(ThemeManager::class);
11221122

11231123
$tmpImage = '<img class="fulltext" src="'
1124-
. $themeManager->theme->getImgPath($tmpImageFile)
1124+
. $themeManager->getThemeImagePath($tmpImageFile)
11251125
. '" alt="' . $tmpTxt . '" title="' . $tmpTxt . '">';
11261126

11271127
return Generator::linkOrButton(Url::getFromRoute('/sql', $urlParamsFullText, false), null, $tmpImage);

src/FlashMessenger.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin;
66

77
use RuntimeException;
8+
use Twig\Attribute\AsTwigFunction;
89

910
use function __;
1011

@@ -47,6 +48,7 @@ public function addMessage(string $context, string $message, string $statement =
4748
}
4849

4950
/** @psalm-return FlashMessageList */
51+
#[AsTwigFunction('flash_messages')]
5052
public function getMessages(): array
5153
{
5254
$this->initSessionStorage();

src/Html/Generator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PhpMyAdmin\Url;
2727
use PhpMyAdmin\Util;
2828
use Throwable;
29+
use Twig\Attribute\AsTwigFunction;
2930
use Twig\Error\LoaderError;
3031
use Twig\Error\RuntimeError;
3132
use Twig\Error\SyntaxError;
@@ -92,6 +93,7 @@ public static function showCopyToClipboard(string $text): string
9293
*
9394
* @return string link or empty string
9495
*/
96+
#[AsTwigFunction('link_to_var_documentation', isSafe: ['html'])]
9597
public static function linkToVarDocumentation(
9698
string $name,
9799
bool $useMariaDB = false,
@@ -109,6 +111,7 @@ public static function linkToVarDocumentation(
109111
*
110112
* @param string $message the message for the tooltip
111113
*/
114+
#[AsTwigFunction('show_hint', isSafe: ['html'])]
112115
public static function showHint(string $message): string
113116
{
114117
$classClause = Config::getInstance()->settings['ShowHint'] ? ' class="pma_hint"' : '';
@@ -165,6 +168,7 @@ public static function getDbLink(string $database): string
165168
*
166169
* @return string an html snippet
167170
*/
171+
#[AsTwigFunction('get_icon', isSafe: ['html'])]
168172
public static function getIcon(
169173
string $icon,
170174
string $alternate = '',
@@ -638,6 +642,7 @@ public static function getMessage(
638642
*
639643
* @return string the html link
640644
*/
645+
#[AsTwigFunction('show_php_docu', isSafe: ['html'])]
641646
public static function showPHPDocumentation(string $target): string
642647
{
643648
return self::showDocumentationLink(Core::getPHPDocLink($target));
@@ -867,6 +872,7 @@ public static function mysqlDie(
867872
*
868873
* @return string an html IMG tag
869874
*/
875+
#[AsTwigFunction('get_image', isSafe: ['html'])]
870876
public static function getImage(string $image, string $alternate = '', array $attributes = []): string
871877
{
872878
$alternate = htmlspecialchars($alternate);
@@ -917,6 +923,7 @@ public static function getImage(string $image, string $alternate = '', array $at
917923
*
918924
* @return string the results to be echoed or saved in an array
919925
*/
926+
#[AsTwigFunction('link_or_button', isSafe: ['html'])]
920927
public static function linkOrButton(
921928
string $urlPath,
922929
array|null $urlParams,
@@ -1014,6 +1021,7 @@ public static function linkOrButton(
10141021
*
10151022
* @todo use $pos from $_url_params
10161023
*/
1024+
#[AsTwigFunction('get_list_navigator', isSafe: ['html'])]
10171025
public static function getListNavigator(
10181026
int $count,
10191027
int $pos,
@@ -1061,6 +1069,7 @@ public static function getListNavigator(
10611069
*
10621070
* @return string the formatted sql
10631071
*/
1072+
#[AsTwigFunction('format_sql', isSafe: ['html'])]
10641073
public static function formatSql(string $sqlQuery, bool $truncate = false): string
10651074
{
10661075
$config = Config::getInstance();
@@ -1080,6 +1089,7 @@ public static function formatSql(string $sqlQuery, bool $truncate = false): stri
10801089
*
10811090
* @param string $selected The value to mark as selected in HTML mode
10821091
*/
1092+
#[AsTwigFunction('get_supported_datatypes', isSafe: ['html'])]
10831093
public static function getSupportedDatatypes(string $selected): string
10841094
{
10851095
// NOTE: the SELECT tag is not included in this snippet.

src/Html/MySQLDocumentation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use PhpMyAdmin\Core;
1111
use PhpMyAdmin\Util;
12+
use Twig\Attribute\AsTwigFunction;
1213

1314
use function __;
1415
use function defined;
@@ -31,6 +32,7 @@ class MySQLDocumentation
3132
*
3233
* @return string the html link
3334
*/
35+
#[AsTwigFunction('show_mysql_docu', isSafe: ['html'])]
3436
public static function show(
3537
string $link,
3638
bool $bigIcon = false,
@@ -66,6 +68,7 @@ public static function show(
6668
*
6769
* @return string the html link
6870
*/
71+
#[AsTwigFunction('show_docu', isSafe: ['html'])]
6972
public static function showDocumentation(
7073
string $page,
7174
string $anchor = '',
@@ -89,6 +92,7 @@ public static function showDocumentation(
8992
*
9093
* @return string URL
9194
*/
95+
#[AsTwigFunction('get_docu_link', isSafe: ['html'])]
9296
public static function getDocumentationLink(string $page, string $anchor = '', string $pathPrefix = './'): string
9397
{
9498
/* Construct base URL */

src/Sanitize.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace PhpMyAdmin;
99

1010
use PhpMyAdmin\Html\MySQLDocumentation;
11+
use Twig\Attribute\AsTwigFilter;
12+
use Twig\Attribute\AsTwigFunction;
1113

1214
use function __;
1315
use function count;
@@ -160,6 +162,7 @@ private static function replaceDocLink(array $found): string
160162
* @param string $message the message
161163
* @param bool $safe whether string is safe (can keep < and > chars)
162164
*/
165+
#[AsTwigFilter('sanitize', isSafe: ['html'])]
163166
public static function convertBBCode(string $message, bool $safe = false): string
164167
{
165168
if (! $safe) {
@@ -244,6 +247,7 @@ public static function sanitizeFilename(string $filename, bool $replaceDots = fa
244247
*
245248
* @return string Javascript code.
246249
*/
250+
#[AsTwigFunction('get_js_value', isSafe: ['html'])]
247251
public static function getJsValue(string $key, mixed $value): string
248252
{
249253
return $key . ' = ' . json_encode($value, JSON_HEX_TAG) . ";\n";

0 commit comments

Comments
 (0)