Skip to content

Commit 48e2e8f

Browse files
authored
Enable Twig strict variables
1 parent 932bae8 commit 48e2e8f

File tree

7 files changed

+70
-11
lines changed

7 files changed

+70
-11
lines changed

composer-dependency-analyser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@
6161
->ignoreErrorsOnExtension('ext-zend-opcache', [ErrorType::SHADOW_DEPENDENCY])
6262
->ignoreErrorsOnPackage('paragonie/sodium_compat', [ErrorType::UNUSED_DEPENDENCY])
6363

64-
// Bundles are only loaded in a conditional block that checks if the environment is dev
64+
// Only loaded in a conditional block that checks if the environment is dev
6565
->ignoreErrorsOnPackages([
6666
'symfony/twig-bundle',
6767
'symfony/web-profiler-bundle',
68+
'alisqi/twigqi',
6869
], [ErrorType::DEV_DEPENDENCY_IN_PROD])
6970

7071
->ignoreErrorsOnExtension('ext-bcmath', [ErrorType::UNUSED_DEPENDENCY]) // Required by tc-lib-barcode

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
},
104104
"require-dev": {
105105
"ext-xml": "*",
106+
"alisqi/twigqi": "^3.0",
106107
"atoum/atoum": "dev-main#e43a6a84809635cf5dcdf80bf3c773e7cf1d5a04",
107108
"atoum/stubs": "dev-master#fb8890f347a7ecf1e3cf2b5a139a8b038359a6ab",
108109
"friendsofphp/php-cs-fixer": "^3.75",

composer.lock

Lines changed: 47 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Glpi/Application/Environment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public function getConstantsOverride(string $root_dir): array
149149
],
150150
],
151151
self::DEVELOPMENT => [
152+
'GLPI_STRICT_ENV' => true,
152153
'GLPI_WEBHOOK_ALLOW_RESPONSE_SAVING' => '1',
153154
],
154155
};

src/Glpi/Application/View/TemplateRenderer.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
namespace Glpi\Application\View;
3636

37+
use AlisQI\TwigQI\Extension as TwigIQExtension;
38+
use AlisQI\TwigQI\Logger\TriggerErrorLogger;
39+
use Glpi\Application\Environment as GLPIEnvironment;
3740
use Glpi\Application\View\Extension\ConfigExtension;
3841
use Glpi\Application\View\Extension\DataHelpersExtension;
3942
use Glpi\Application\View\Extension\DocumentExtension;
@@ -52,7 +55,7 @@
5255
use Glpi\Kernel\Kernel;
5356
use Plugin;
5457
use Session;
55-
use Twig\Environment;
58+
use Twig\Environment as TwigEnvironment;
5659
use Twig\Extension\DebugExtension;
5760
use Twig\Extra\String\StringExtension;
5861
use Twig\Loader\FilesystemLoader;
@@ -62,7 +65,7 @@
6265
*/
6366
class TemplateRenderer
6467
{
65-
private Environment $environment;
68+
private TwigEnvironment $environment;
6669

6770
public function __construct(string $rootdir = GLPI_ROOT, ?string $cachedir = null)
6871
{
@@ -80,10 +83,11 @@ public function __construct(string $rootdir = GLPI_ROOT, ?string $cachedir = nul
8083
$loader->addPath(Plugin::getPhpDir($plugin_key . '/templates'), $plugin_key);
8184
}
8285

83-
$glpi_environment = \Glpi\Application\Environment::get();
86+
$glpi_environment = GLPIEnvironment::get();
8487
$env_params = [
8588
'debug' => $glpi_environment->shouldEnableExtraDevAndDebugTools() || ($_SESSION['glpi_use_mode'] ?? null) === Session::DEBUG_MODE,
8689
'auto_reload' => $glpi_environment->shouldExpectResourcesToChange(),
90+
'strict_variables' => GLPI_STRICT_ENV,
8791
];
8892

8993
$tpl_cachedir = $cachedir . '/templates';
@@ -96,7 +100,7 @@ public function __construct(string $rootdir = GLPI_ROOT, ?string $cachedir = nul
96100
$env_params['cache'] = $tpl_cachedir;
97101
}
98102

99-
$this->environment = new Environment(
103+
$this->environment = new TwigEnvironment(
100104
$loader,
101105
$env_params
102106
);
@@ -118,6 +122,13 @@ public function __construct(string $rootdir = GLPI_ROOT, ?string $cachedir = nul
118122
$this->environment->addExtension(new SearchExtension());
119123
$this->environment->addExtension(new SessionExtension());
120124
$this->environment->addExtension(new TeamExtension());
125+
if (GLPIEnvironment::get()->shouldEnableExtraDevAndDebugTools() || GLPI_STRICT_ENV) {
126+
$this->environment->addExtension(
127+
new TwigIQExtension(
128+
new TriggerErrorLogger()
129+
)
130+
);
131+
}
121132

122133
// add superglobals
123134
$this->environment->addGlobal('_post', $_POST);
@@ -144,9 +155,9 @@ public static function getInstance(): TemplateRenderer
144155
/**
145156
* Return Twig environment used to handle templates.
146157
*
147-
* @return Environment
158+
* @return TwigEnvironment
148159
*/
149-
public function getEnvironment(): Environment
160+
public function getEnvironment(): TwigEnvironment
150161
{
151162
return $this->environment;
152163
}

templates/components/helpdesk_forms/delegation_alert.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
{% if get_current_user() is not null %}
3636
{# @var selected_user \User #}
37-
{% if selected_user is empty %}
37+
{% if selected_user is not defined or selected_user is empty %}
3838
{% set selected_user = get_current_user() %}
3939
{% endif %}
4040

templates/components/itilobject/timeline/form_validation.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
</div>
5555
</div>
5656
{% endif %}
57-
{% if entry_i['comment_validation']|length %}
57+
{% if entry_i['comment_validation'] is defined and entry_i['comment_validation']|length %}
5858
<div class='alert alert-info mt-2'>
5959
<div class='d-flex'>
6060
<div><i class='ti ti-quote me-2'></i></div>

0 commit comments

Comments
 (0)