@@ -172,7 +172,7 @@ private function childHasInterestingData(Frame $child): bool
172172
173173 private function writeFrame (Frame $ frame , ?string $ linkPrefix , ?string $ typeOverride = null ): string
174174 {
175- $ shouldWriteGlobal = $ frame ->getName () && $ frame ->getRootNode ()::class === Frame::class;
175+ $ shouldWriteGlobal = $ frame ->getRootNode ()-> getName () && $ frame ->getRootNode ()::class === Frame::class;
176176 $ data = '' ;
177177 $ globalChildrenWithParentKey = [];
178178 $ inheritedKeyValues = [];
@@ -185,31 +185,12 @@ private function writeFrame(Frame $frame, ?string $linkPrefix, ?string $typeOver
185185 }
186186 }
187187 if ($ shouldWriteGlobal ) {
188- foreach ($ frame ->getInherits () as $ templateName ) {
189- $ template = $ this ->templateRegistry ->get ($ templateName );
190- if (!$ template ) {
191- continue ;
192- }
193- foreach ($ template ->getKeyValues () as $ key => $ value ) {
194- $ inheritedKeyValues [$ key ] = $ value ;
195- }
196- foreach ($ template ->getChildren () as $ child ) {
197- $ clone = $ child ->withParent ($ frame );
198- if (!empty ($ clone ->getName ())) { // will create a global
199- $ data .= $ this ->writeFrame (
200- $ clone ,
201- $ linkPrefix ,
202- // this must be $child rather than $clone
203- $ this ->childHasInterestingData ($ child )
204- ? $ child ->getClassName ()
205- : $ child ->getType (),
206- );
207- if ($ clone ->getParentKey ()) {
208- $ inheritedKeyValues [$ clone ->getParentKey ()] = [$ clone ->getName ()];
209- }
210- }
211- }
212- }
188+ $ this ->handleInherits (
189+ $ frame ,
190+ $ inheritedKeyValues ,
191+ $ linkPrefix ,
192+ $ data
193+ );
213194 }
214195
215196 if ($ linkPrefix ) {
@@ -252,8 +233,12 @@ private function writeFrame(Frame $frame, ?string $linkPrefix, ?string $typeOver
252233 }
253234 }
254235 }
255- if ($ shouldWriteGlobal ) {
256- $ data .= $ frame ->getName () . " = {} \n" ;
236+ if ($ shouldWriteGlobal && $ frame ->getName ()) {
237+ $ name = $ frame ->getName ();
238+ if (str_contains ($ name , '$ ' )) {
239+ $ name = '_G[" ' . $ name . '"] ' ;
240+ }
241+ $ data .= $ name . " = {} \n" ;
257242 foreach ($ globalChildrenWithParentKey as $ key => $ value ) {
258243 $ data .= $ frame ->getName () . '[" ' . $ key . '"] = ' . $ value . "\n" ;
259244 }
@@ -267,4 +252,39 @@ private function writeFrame(Frame $frame, ?string $linkPrefix, ?string $typeOver
267252
268253 return $ data . "\n" ;
269254 }
255+
256+ private function handleInherits (Frame $ frame , array &$ inheritedKeyValues , ?string $ linkPrefix , string &$ data ): void
257+ {
258+ foreach ($ frame ->getInherits () as $ templateName ) {
259+ $ template = $ this ->templateRegistry ->get ($ templateName );
260+ if (!$ template ) {
261+ continue ;
262+ }
263+ $ template = $ template ->withParent ($ frame );
264+ $ this ->handleInherits ($ template , $ inheritedKeyValues , $ linkPrefix , $ data );
265+ foreach ($ template ->getKeyValues () as $ key => $ value ) {
266+ $ inheritedKeyValues [$ key ] = $ value ;
267+ }
268+ foreach ($ template ->getChildren () as $ child ) {
269+ $ clone = $ child ->withParent ($ frame );
270+ if (!empty ($ clone ->getName ())) { // will create a global
271+ $ data .= $ this ->writeFrame (
272+ $ clone ,
273+ $ linkPrefix ,
274+ // this must be $child rather than $clone
275+ $ this ->childHasInterestingData ($ child )
276+ ? $ child ->getClassName ()
277+ : $ child ->getType (),
278+ );
279+ if ($ clone ->getParentKey ()) {
280+ $ name = $ clone ->getName ();
281+ if (str_contains ($ name , '$ ' )) {
282+ $ name = '_G[" ' . $ name . '"] ' ;
283+ }
284+ $ inheritedKeyValues [$ clone ->getParentKey ()] = [$ name ];
285+ }
286+ }
287+ }
288+ }
289+ }
270290}
0 commit comments