Apply compat Twig parser patches during build

This commit is contained in:
Monika
2026-08-11 16:06:37 +05:30
parent 37cee81eef
commit 4302381d66
4 changed files with 100 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
#!/usr/bin/env php
<?php
chdir(__DIR__ . '/..');
$parserFiles = [
'platforms/joomla/lib_gantry5/compat/vendor/twig/twig/src/Parser.php',
'platforms/wordpress/gantry5/compat/vendor/twig/twig/src/Parser.php',
];
foreach ($parserFiles as $file) {
patch_twig_parser($file);
}
function patch_twig_parser($file)
{
if (!is_file($file)) {
return;
}
$contents = file_get_contents($file);
if ($contents === false) {
throw new RuntimeException("Unable to read {$file}");
}
$contents = str_replace(
<<<'PHP'
if (null === $this->handlers) {
$this->handlers = $this->env->getTokenParsers();
$this->handlers->setParser($this);
}
PHP,
<<<'PHP'
if (null === $this->handlers) {
$this->handlers = $this->env->getTokenParsers();
if (is_object($this->handlers) && method_exists($this->handlers, 'setParser')) {
$this->handlers->setParser($this);
} elseif (is_array($this->handlers)) {
foreach ($this->handlers as $handler) {
if (is_object($handler) && method_exists($handler, 'setParser')) {
$handler->setParser($this);
}
}
}
}
PHP,
$contents,
$count
);
if ($count === 0 && strpos($contents, 'foreach ($this->handlers as $handler)') === false) {
throw new RuntimeException("Unable to patch token parser setup in {$file}");
}
$contents = str_replace(
<<<'PHP'
$subparser = $this->handlers->getTokenParser($token->getValue());
PHP,
<<<'PHP'
if (is_object($this->handlers) && method_exists($this->handlers, 'getTokenParser')) {
$subparser = $this->handlers->getTokenParser($token->getValue());
} elseif (is_array($this->handlers)) {
$subparser = isset($this->handlers[$token->getValue()]) ? $this->handlers[$token->getValue()] : null;
} else {
$subparser = null;
}
PHP,
$contents,
$count
);
if ($count === 0 && strpos($contents, 'isset($this->handlers[$token->getValue()])') === false) {
throw new RuntimeException("Unable to patch token parser lookup in {$file}");
}
if (file_put_contents($file, $contents) === false) {
throw new RuntimeException("Unable to write {$file}");
}
}
+3
View File
@@ -24,6 +24,9 @@ composer_install('wordpress/gantry5', true);
composer_install('wordpress/gantry5_debugbar', false);
composer_install('wordpress/gantry5/compat', false);
echo "# Applying compat vendor patches\n";
require __DIR__ . '/apply-compat-patches';
function composer_install($folder, $link)
{
chdir($folder);
+3
View File
@@ -24,6 +24,9 @@ composer_update('wordpress/gantry5', true);
composer_update('wordpress/gantry5_debugbar', false);
composer_update('wordpress/gantry5/compat', false);
echo "# Applying compat vendor patches\n";
require __DIR__ . '/apply-compat-patches';
function composer_update($folder, $link)
{
chdir($folder);
@@ -94,6 +94,8 @@ abstract class AbstractTheme
public function extendTwig(Environment $twig, ?LoaderInterface $loader = null)
{
if ($twig->hasExtension(TwigExtension::class)) {
$this->addGantryTwigGlobal($twig);
return $twig;
}
@@ -104,6 +106,7 @@ abstract class AbstractTheme
$this->setTwigLoaderPaths($loader);
$twig->addExtension(new TwigExtension);
$this->addGantryTwigGlobal($twig);
if (method_exists($this, 'toGrid')) {
$filter = new TwigFilter('toGrid', [$this, 'toGrid']);
@@ -250,6 +253,18 @@ abstract class AbstractTheme
return $loader;
}
/**
* Ensure phpBB-provided Twig environments have Gantry available as a global.
*
* @param Environment $twig
*/
protected function addGantryTwigGlobal(Environment $twig)
{
if (!array_key_exists('gantry', $twig->getGlobals())) {
$twig->addGlobal('gantry', static::gantry());
}
}
/**
* Get path to Twig cache.
*