@@ -48,23 +48,30 @@ public function compile(Compiler $compiler): void
4848 {
4949 $ compiler ->addDebugInfo ($ this );
5050
51+ $ useYield = method_exists (Environment::class, 'useYield ' ) && $ compiler ->getEnvironment ()->useYield ();
52+
5153 // since twig/twig 3.9.0: Using the internal "twig_to_array" function is deprecated.
5254 if (method_exists (CoreExtension::class, 'toArray ' )) {
5355 $ twig_to_array = 'Twig\Extension\CoreExtension::toArray ' ;
5456 } else {
5557 $ twig_to_array = 'twig_to_array ' ;
5658 }
5759
60+ $ componentRuntime = $ compiler ->getVarName ();
61+
62+ $ compiler
63+ ->write (\sprintf ('$%s = $this->env->getRuntime( ' , $ componentRuntime ))
64+ ->string (ComponentRuntime::class)
65+ ->raw ("); \n" );
66+
5867 /*
5968 * Block 1) PreCreateForRender handling
6069 *
6170 * We call code to trigger the PreCreateForRender event. If the event returns
6271 * a string, we return that string and skip the rest of the rendering process.
6372 */
6473 $ compiler
65- ->write ('$preRendered = $this->extensions[ ' )
66- ->string (ComponentExtension::class)
67- ->raw (']->extensionPreCreateForRender( ' )
74+ ->write (\sprintf ('$preRendered = $%s->preRender( ' , $ componentRuntime ))
6875 ->string ($ this ->getAttribute ('component ' ))
6976 ->raw (', ' )
7077 ->raw ($ twig_to_array )
@@ -96,9 +103,7 @@ public function compile(Compiler $compiler): void
96103 * the final template, template index & variables.
97104 */
98105 $ compiler
99- ->write ('$preRenderEvent = $this->extensions[ ' )
100- ->string (ComponentExtension::class)
101- ->raw (']->startEmbeddedComponentRender( ' )
106+ ->write (\sprintf ('$preRenderEvent = $%s->startEmbedComponent( ' , $ componentRuntime ))
102107 ->string ($ this ->getAttribute ('component ' ))
103108 ->raw (', ' )
104109 ->raw ($ twig_to_array )
@@ -111,6 +116,7 @@ public function compile(Compiler $compiler): void
111116 ->raw (', ' )
112117 ->raw ($ this ->getAttribute ('embedded_index ' ))
113118 ->raw ("); \n" );
119+
114120 $ compiler
115121 ->write ('$embeddedContext = $preRenderEvent->getVariables(); ' )
116122 ->raw ("\n" )
@@ -132,18 +138,11 @@ public function compile(Compiler $compiler): void
132138 * We add the outerBlock to the context if it doesn't exist yet.
133139 * Then add them to the block stack and get the converted embedded blocks.
134140 */
135- $ compiler ->write ('if (!isset($embeddedContext["outerBlocks"])) { ' )
136- ->raw ("\n" )
137- ->indent ()
138- ->write (\sprintf ('$embeddedContext["outerBlocks"] = new \%s(); ' , BlockStack::class))
139- ->raw ("\n" )
140- ->outdent ()
141- ->write ('} ' )
141+ $ compiler
142+ ->write (\sprintf ('$embeddedContext["outerBlocks"] ??= new \%s(); ' , BlockStack::class))
142143 ->raw ("\n" );
143144
144- $ compiler ->write ('$embeddedBlocks = $embeddedContext[ ' )
145- ->string ('outerBlocks ' )
146- ->raw (']->convert($blocks, ' )
145+ $ compiler ->write ('$embeddedBlocks = $embeddedContext["outerBlocks"]->convert($blocks, ' )
147146 ->raw ($ this ->getAttribute ('embedded_index ' ))
148147 ->raw ("); \n" );
149148
@@ -152,9 +151,8 @@ public function compile(Compiler $compiler): void
152151 *
153152 * This will actually render the child component template.
154153 */
155- if (method_exists (Environment::class, 'useYield ' ) && $ compiler ->getEnvironment ()->useYield ()) {
156- $ compiler
157- ->write ('yield from ' );
154+ if ($ useYield ) {
155+ $ compiler ->write ('yield from ' );
158156 }
159157 $ compiler
160158 ->write ('$this->loadTemplate( ' )
@@ -167,7 +165,7 @@ public function compile(Compiler $compiler): void
167165 ->string ($ this ->getAttribute ('embedded_index ' ))
168166 ->raw (') ' );
169167
170- if (method_exists (Environment::class, ' useYield ' ) && $ compiler -> getEnvironment ()-> useYield () ) {
168+ if ($ useYield ) {
171169 $ compiler ->raw ('->unwrap()->yield( ' );
172170 } else {
173171 $ compiler ->raw ('->display( ' );
@@ -176,10 +174,8 @@ public function compile(Compiler $compiler): void
176174 ->raw ('$embeddedContext, $embeddedBlocks ' )
177175 ->raw ("); \n" );
178176
179- $ compiler ->write ('$this->extensions[ ' )
180- ->string (ComponentExtension::class)
181- ->raw (']->finishEmbeddedComponentRender() ' )
182- ->raw ("; \n" )
177+ $ compiler ->write (\sprintf ('$%s->finishEmbedComponent(); ' , $ componentRuntime ))
178+ ->raw ("\n" )
183179 ;
184180
185181 $ compiler
0 commit comments