Skip to content

Commit 6def7d3

Browse files
authored
Merge pull request #60 from pug-php/fix/root-env
Search runtimes in root environments
2 parents 8b8da19 + 491e42a commit 6def7d3

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Pug/Twig/Environment.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use RuntimeException;
88
use Symfony\Component\DependencyInjection\ContainerInterface;
99
use Twig\Environment as TwigEnvironment;
10+
use Twig\Error\RuntimeError;
1011
use Twig\Extension\EscaperExtension;
1112
use Twig\Extension\OptimizerExtension;
1213
use Twig\Loader\LoaderInterface;
@@ -39,6 +40,11 @@ class Environment extends TwigEnvironment
3940
*/
4041
public $extensions = [];
4142

43+
/**
44+
* @var TwigEnvironment
45+
*/
46+
public $rootEnv;
47+
4248
public function __construct(LoaderInterface $loader, $options = [])
4349
{
4450
parent::__construct($loader, $options);
@@ -56,6 +62,23 @@ public function getRenderer()
5662
return $this->pugSymfonyEngine->getRenderer();
5763
}
5864

65+
public function getRuntime(string $class)
66+
{
67+
try {
68+
return parent::getRuntime($class);
69+
} catch (RuntimeError $error) {
70+
if (!$this->rootEnv) {
71+
throw $error;
72+
}
73+
74+
try {
75+
return $this->rootEnv->getRuntime($class);
76+
} catch (RuntimeError $_) {
77+
throw $error;
78+
}
79+
}
80+
}
81+
5982
public static function fromTwigEnvironment(TwigEnvironment $baseTwig, PugSymfonyEngine $pugSymfonyEngine, ContainerInterface $container)
6083
{
6184
$twig = new static($baseTwig->getLoader(), [
@@ -67,6 +90,7 @@ public static function fromTwigEnvironment(TwigEnvironment $baseTwig, PugSymfony
6790
'auto_reload' => $baseTwig->isAutoReload(),
6891
'optimizations' => static::getPrivateExtensionProperty($baseTwig, OptimizerExtension::class, 'optimizers'),
6992
]);
93+
$twig->rootEnv = $baseTwig;
7094

7195
$twig->setPugSymfonyEngine($pugSymfonyEngine);
7296
$twig->setContainer($container);

tests/Pug/PugSymfonyEngineTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function testPreRenderCsrfToken()
210210

211211
self::assertSame('<p>Hello</p>', $pugSymfony->renderString('p Hello'));
212212

213-
self::assertRegExp('/<p>[a-zA-Z0-9_-]{20,}<\/p>/', $pugSymfony->renderString('p=csrf_token("authentificate")'));
213+
self::assertRegExp('/<p>[a-zA-Z0-9_-]{20,}<\/p>/', $pugSymfony->renderString('p=csrf_token("authenticate")'));
214214
}
215215

216216
public function testGetEngine()

0 commit comments

Comments
 (0)