77use RuntimeException ;
88use Symfony \Component \DependencyInjection \ContainerInterface ;
99use Twig \Environment as TwigEnvironment ;
10+ use Twig \Error \RuntimeError ;
1011use Twig \Extension \EscaperExtension ;
1112use Twig \Extension \OptimizerExtension ;
1213use 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 );
0 commit comments