@@ -48,23 +48,30 @@ public function compile(Compiler $compiler): void
48
48
{
49
49
$ compiler ->addDebugInfo ($ this );
50
50
51
+ $ useYield = method_exists (Environment::class, 'useYield ' ) && $ compiler ->getEnvironment ()->useYield ();
52
+
51
53
// since twig/twig 3.9.0: Using the internal "twig_to_array" function is deprecated.
52
54
if (method_exists (CoreExtension::class, 'toArray ' )) {
53
55
$ twig_to_array = 'Twig\Extension\CoreExtension::toArray ' ;
54
56
} else {
55
57
$ twig_to_array = 'twig_to_array ' ;
56
58
}
57
59
60
+ $ componentRuntime = $ compiler ->getVarName ();
61
+
62
+ $ compiler
63
+ ->write (\sprintf ('$%s = $this->env->getRuntime( ' , $ componentRuntime ))
64
+ ->string (ComponentRuntime::class)
65
+ ->raw ("); \n" );
66
+
58
67
/*
59
68
* Block 1) PreCreateForRender handling
60
69
*
61
70
* We call code to trigger the PreCreateForRender event. If the event returns
62
71
* a string, we return that string and skip the rest of the rendering process.
63
72
*/
64
73
$ compiler
65
- ->write ('$preRendered = $this->extensions[ ' )
66
- ->string (ComponentExtension::class)
67
- ->raw (']->extensionPreCreateForRender( ' )
74
+ ->write (\sprintf ('$preRendered = $%s->preRender( ' , $ componentRuntime ))
68
75
->string ($ this ->getAttribute ('component ' ))
69
76
->raw (', ' )
70
77
->raw ($ twig_to_array )
@@ -96,9 +103,7 @@ public function compile(Compiler $compiler): void
96
103
* the final template, template index & variables.
97
104
*/
98
105
$ compiler
99
- ->write ('$preRenderEvent = $this->extensions[ ' )
100
- ->string (ComponentExtension::class)
101
- ->raw (']->startEmbeddedComponentRender( ' )
106
+ ->write (\sprintf ('$preRenderEvent = $%s->startEmbedComponent( ' , $ componentRuntime ))
102
107
->string ($ this ->getAttribute ('component ' ))
103
108
->raw (', ' )
104
109
->raw ($ twig_to_array )
@@ -111,6 +116,7 @@ public function compile(Compiler $compiler): void
111
116
->raw (', ' )
112
117
->raw ($ this ->getAttribute ('embedded_index ' ))
113
118
->raw ("); \n" );
119
+
114
120
$ compiler
115
121
->write ('$embeddedContext = $preRenderEvent->getVariables(); ' )
116
122
->raw ("\n" )
@@ -132,18 +138,11 @@ public function compile(Compiler $compiler): void
132
138
* We add the outerBlock to the context if it doesn't exist yet.
133
139
* Then add them to the block stack and get the converted embedded blocks.
134
140
*/
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))
142
143
->raw ("\n" );
143
144
144
- $ compiler ->write ('$embeddedBlocks = $embeddedContext[ ' )
145
- ->string ('outerBlocks ' )
146
- ->raw (']->convert($blocks, ' )
145
+ $ compiler ->write ('$embeddedBlocks = $embeddedContext["outerBlocks"]->convert($blocks, ' )
147
146
->raw ($ this ->getAttribute ('embedded_index ' ))
148
147
->raw ("); \n" );
149
148
@@ -152,9 +151,8 @@ public function compile(Compiler $compiler): void
152
151
*
153
152
* This will actually render the child component template.
154
153
*/
155
- if (method_exists (Environment::class, 'useYield ' ) && $ compiler ->getEnvironment ()->useYield ()) {
156
- $ compiler
157
- ->write ('yield from ' );
154
+ if ($ useYield ) {
155
+ $ compiler ->write ('yield from ' );
158
156
}
159
157
$ compiler
160
158
->write ('$this->loadTemplate( ' )
@@ -167,7 +165,7 @@ public function compile(Compiler $compiler): void
167
165
->string ($ this ->getAttribute ('embedded_index ' ))
168
166
->raw (') ' );
169
167
170
- if (method_exists (Environment::class, ' useYield ' ) && $ compiler -> getEnvironment ()-> useYield () ) {
168
+ if ($ useYield ) {
171
169
$ compiler ->raw ('->unwrap()->yield( ' );
172
170
} else {
173
171
$ compiler ->raw ('->display( ' );
@@ -176,10 +174,8 @@ public function compile(Compiler $compiler): void
176
174
->raw ('$embeddedContext, $embeddedBlocks ' )
177
175
->raw ("); \n" );
178
176
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" )
183
179
;
184
180
185
181
$ compiler
0 commit comments