33namespace Craftile \Laravel \View ;
44
55use Craftile \Core \Data \BlockSchema ;
6- use Craftile \Laravel \BlockFlattener ;
76use Craftile \Laravel \BlockSchemaRegistry ;
87use Craftile \Laravel \Contracts \BlockCompilerInterface ;
98use Craftile \Laravel \Exceptions \JsonViewException ;
10- use Craftile \Laravel \Facades \Craftile ;
119use Illuminate \Filesystem \Filesystem ;
1210use Illuminate \View \Compilers \BladeCompiler ;
1311use Illuminate \View \Compilers \Compiler ;
@@ -93,27 +91,26 @@ public function compileJsonFile(string $path): string
9391 */
9492 public function compileTemplate (array $ templateData , string $ path = '' ): string
9593 {
96- $ template = $ this ->normalizeTemplate ($ templateData );
97- if (empty ($ template ['regions ' ])) {
94+ if (empty ($ templateData ['regions ' ])) {
9895 return "<?php // Empty template ?> \n" ;
9996 }
10097
101- $ this ->invalidateStaleBlockCaches ($ template );
98+ $ this ->invalidateStaleBlockCaches ($ templateData );
10299
103100 // Compile static block children to files
104- $ this ->compileStaticBlockChildren ($ template , $ path );
101+ $ this ->compileStaticBlockChildren ($ templateData , $ path );
105102
106103 $ regionsCodes = [];
107- foreach ($ template ['regions ' ] as $ region ) {
104+ foreach ($ templateData ['regions ' ] as $ region ) {
108105 $ regionName = $ region ['name ' ] ?? 'unnamed ' ;
109106
110107 $ blocks = '' ;
111108 foreach ($ region ['blocks ' ] as $ blockId ) {
112- if (! isset ($ template ['blocks ' ][$ blockId ])) {
109+ if (! isset ($ templateData ['blocks ' ][$ blockId ])) {
113110 throw new JsonViewException ("Block ' {$ blockId }' referenced in template but not defined in blocks section " , $ path );
114111 }
115112
116- $ blockData = $ template ['blocks ' ][$ blockId ];
113+ $ blockData = $ templateData ['blocks ' ][$ blockId ];
117114
118115 // Skip static blocks - they are rendered in Blade templates via @craftileBlock
119116 if ($ blockData ['static ' ] ?? false ) {
@@ -125,7 +122,7 @@ public function compileTemplate(array $templateData, string $path = ''): string
125122 continue ;
126123 }
127124
128- $ blocks .= $ this ->compileBlockSelectively ($ blockData , $ template , $ path );
125+ $ blocks .= $ this ->compileBlockSelectively ($ blockData , $ templateData , $ path );
129126 }
130127
131128 $ regionCode = <<<PHP
@@ -299,65 +296,6 @@ protected function findBlockCompiler(BlockSchema $schema): BlockCompilerInterfac
299296 return $ registry ->findCompiler ($ schema );
300297 }
301298
302- /**
303- * Normalize template to default format.
304- */
305- protected function normalizeTemplate (array $ templateData ): array
306- {
307- // Apply custom pre-normalizer
308- $ templateData = Craftile::normalizeTemplate ($ templateData );
309-
310- $ normalized = $ this ->normalizeTemplateFormat ($ templateData );
311-
312- $ flattener = app (BlockFlattener::class);
313- if ($ flattener ->hasNestedStructure ($ normalized )) {
314- $ flattened = $ flattener ->flattenNestedStructure ($ normalized );
315- unset($ flattened ['_idMappings ' ]);
316-
317- return $ flattened ;
318- }
319-
320- return $ normalized ;
321- }
322-
323- /**
324- * Normalize template format to standard structure.
325- */
326- protected function normalizeTemplateFormat (array $ templateData ): array
327- {
328- if (isset ($ templateData ['regions ' ])) {
329- return $ templateData ;
330- }
331-
332- $ blocks = $ templateData ['blocks ' ] ?? [];
333-
334- // Determine region name and block order based on template format
335- $ regionName = match (true ) {
336- isset ($ templateData ['name ' ]) => $ templateData ['name ' ],
337- default => 'main '
338- };
339-
340- $ blockOrder = match (true ) {
341- isset ($ templateData ['order ' ]) => $ templateData ['order ' ],
342- ! empty ($ blocks ) => array_values (array_map (fn ($ block ) => $ block ['id ' ], $ blocks )),
343- default => []
344- };
345-
346- if (empty ($ blocks ) && empty ($ blockOrder ) && ! isset ($ templateData ['name ' ]) && ! isset ($ templateData ['order ' ])) {
347- return ['blocks ' => [], 'regions ' => []];
348- }
349-
350- return [
351- 'blocks ' => $ blocks ,
352- 'regions ' => [
353- [
354- 'name ' => $ regionName ,
355- 'blocks ' => $ blockOrder ,
356- ],
357- ],
358- ];
359- }
360-
361299 /**
362300 * Append the file path to the compiled string.
363301 *
0 commit comments