Skip to content

Commit 148c24c

Browse files
committed
refactor: optimize template normalization
1 parent 42610b8 commit 148c24c

16 files changed

Lines changed: 414 additions & 434 deletions

packages/core/src/Services/BlockFlattener.php

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function flattenNestedStructure(array $templateData): array
2626

2727
return [
2828
'blocks' => $flatBlocks,
29-
'regions' => $this->extractRegions($templateData),
29+
'regions' => $templateData['regions'] ?? [],
3030
'_idMappings' => $this->idMappings,
3131
];
3232
}
@@ -49,27 +49,6 @@ public function hasNestedStructure(array $templateData): bool
4949
return false;
5050
}
5151

52-
/**
53-
* Extract regions from template data.
54-
*/
55-
protected function extractRegions(array $templateData): array
56-
{
57-
if (isset($templateData['regions'])) {
58-
return $templateData['regions'];
59-
}
60-
61-
// Default: single region with all blocks
62-
$regionName = $templateData['name'] ?? 'main';
63-
$blockIds = array_values(array_map(fn ($block) => $block['id'], $templateData['blocks'] ?? []));
64-
65-
return [
66-
[
67-
'name' => $regionName,
68-
'blocks' => $blockIds,
69-
],
70-
];
71-
}
72-
7352
/**
7453
* Recursively extract blocks from nested structure.
7554
*/

packages/laravel/src/BlockDatastore.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class BlockDatastore
1414
private static array $loadedBlocks = [];
1515

1616
public function __construct(
17-
protected BlockFlattener $flattener,
1817
protected JsonViewParser $parser
1918
) {}
2019

@@ -104,27 +103,15 @@ public function getAllBlocks(): array
104103
public function getBlocksArray(string $sourceFilePath): array
105104
{
106105
try {
107-
// Parse template with automatic caching
108-
$data = $this->parser->parse($sourceFilePath);
106+
$template = $this->parser->parse($sourceFilePath);
109107
} catch (\Exception $e) {
110-
// Gracefully handle parsing errors - return empty
111108
return [];
112109
}
113110

114-
if (! is_array($data)) {
111+
if (! is_array($template)) {
115112
return [];
116113
}
117114

118-
// Apply custom pre-normalizer if registered
119-
$data = Craftile::normalizeTemplate($data);
120-
121-
if ($this->flattener->hasNestedStructure($data)) {
122-
$template = $this->flattener->flattenNestedStructure($data);
123-
unset($template['_idMappings']);
124-
} else {
125-
$template = $data;
126-
}
127-
128115
$blocks = $template['blocks'] ?? [];
129116

130117
$this->assignBlockIndices($blocks, $template);

packages/laravel/src/Support/HandleUpdates.php

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,40 @@
22

33
namespace Craftile\Laravel\Support;
44

5-
use Craftile\Laravel\BlockFlattener;
65
use Craftile\Laravel\Data\UpdateRequest;
7-
use Craftile\Laravel\Facades\Craftile;
6+
use Craftile\Laravel\View\JsonViewParser;
87

98
class HandleUpdates
109
{
1110
public function __construct(
12-
private BlockFlattener $flattener
11+
private JsonViewParser $parser
1312
) {}
1413

1514
/**
16-
* Apply updates to source data, optionally filtering by target regions
15+
* Apply updates to source data from file path, optionally filtering by target regions
1716
*
18-
* @param array $sourceData Original template data
17+
* @param string $sourceFilePath Path to source template file
1918
* @param UpdateRequest $updateRequest Update request with new block states
2019
* @param array|null $targetRegions Optional array of region names to limit updates to
2120
* @return array{data: array, updated: bool} Updated template data and whether changes were applied
2221
*/
23-
public function execute(array $sourceData, UpdateRequest $updateRequest, ?array $targetRegions = null): array
22+
public function execute(string $sourceFilePath, UpdateRequest $updateRequest, ?array $targetRegions = null): array
2423
{
25-
$data = $this->normalizeSourceData($sourceData);
24+
// Get fully normalized data from parser's pipeline
25+
$sourceData = $this->parser->parse($sourceFilePath);
2626

27-
if (! $this->hasUpdates($data, $updateRequest, $targetRegions)) {
27+
if (! $this->hasUpdates($sourceData, $updateRequest, $targetRegions)) {
2828
return ['data' => $sourceData, 'updated' => false];
2929
}
3030

3131
if ($targetRegions === null) {
32-
return $this->applyUpdates($data, $updateRequest);
32+
return $this->applyUpdates($sourceData, $updateRequest);
3333
}
3434

35-
$regionBlocks = $this->getAllRegionBlocks($data, $targetRegions, $updateRequest);
36-
$filteredChanges = $this->filterChangesForRegions($updateRequest, $regionBlocks, $data, $targetRegions);
35+
$regionBlocks = $this->getAllRegionBlocks($sourceData, $targetRegions, $updateRequest);
36+
$filteredChanges = $this->filterChangesForRegions($updateRequest, $regionBlocks, $sourceData, $targetRegions);
3737

38-
return $this->applyUpdates($data, $updateRequest, $filteredChanges, $regionBlocks, $targetRegions);
39-
}
40-
41-
/**
42-
* Normalize source data to flat structure and ensure required keys
43-
*/
44-
private function normalizeSourceData(array $sourceData): array
45-
{
46-
// Apply custom normalizer if registered
47-
$sourceData = Craftile::normalizeTemplate($sourceData);
48-
if ($this->flattener->hasNestedStructure($sourceData)) {
49-
$sourceData = $this->flattener->flattenNestedStructure($sourceData);
50-
51-
return [
52-
'blocks' => $sourceData['blocks'] ?? [],
53-
'regions' => $sourceData['regions'] ?? [],
54-
];
55-
}
56-
57-
$regionName = $sourceData['name'] ?? 'main';
58-
$blocks = $sourceData['blocks'] ?? [];
59-
$blocksOrder = $sourceData['order'] ?? array_keys($blocks);
60-
61-
return [
62-
'blocks' => $blocks,
63-
'regions' => [
64-
[
65-
'name' => $regionName,
66-
'blocks' => $blocksOrder,
67-
],
68-
],
69-
];
38+
return $this->applyUpdates($sourceData, $updateRequest, $filteredChanges, $regionBlocks, $targetRegions);
7039
}
7140

7241
private function applyUpdates(array $data, UpdateRequest $updateRequest, ?array $filteredChanges = null, ?array $regionBlocks = null, ?array $targetRegions = null): array

packages/laravel/src/View/JsonViewCompiler.php

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
namespace Craftile\Laravel\View;
44

55
use Craftile\Core\Data\BlockSchema;
6-
use Craftile\Laravel\BlockFlattener;
76
use Craftile\Laravel\BlockSchemaRegistry;
87
use Craftile\Laravel\Contracts\BlockCompilerInterface;
98
use Craftile\Laravel\Exceptions\JsonViewException;
10-
use Craftile\Laravel\Facades\Craftile;
119
use Illuminate\Filesystem\Filesystem;
1210
use Illuminate\View\Compilers\BladeCompiler;
1311
use 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

Comments
 (0)