diff --git a/bin/apply-compat-patches b/bin/apply-compat-patches new file mode 100644 index 000000000..4eae52481 --- /dev/null +++ b/bin/apply-compat-patches @@ -0,0 +1,79 @@ +#!/usr/bin/env php +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}"); + } +} diff --git a/bin/composer-install b/bin/composer-install index a34b669a4..8aefeadca 100755 --- a/bin/composer-install +++ b/bin/composer-install @@ -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); diff --git a/bin/composer-update b/bin/composer-update index 62dc0f482..7d17d095e 100755 --- a/bin/composer-update +++ b/bin/composer-update @@ -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); diff --git a/src/classes/Gantry/Component/Theme/AbstractTheme.php b/src/classes/Gantry/Component/Theme/AbstractTheme.php index 961f4f08e..0f162555f 100644 --- a/src/classes/Gantry/Component/Theme/AbstractTheme.php +++ b/src/classes/Gantry/Component/Theme/AbstractTheme.php @@ -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. *