Skip to content

Commit

Permalink
Merge pull request #23 from Stillat/memory-serialization-refactors
Browse files Browse the repository at this point in the history
Memory serialization refactors
  • Loading branch information
JohnathonKoster authored Feb 8, 2025
2 parents 3e498e1 + 901128d commit 52b8df2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- Adds a `#cache` compiler attribute, which may be used to cache the results of any Dagger component
- Bumps the minimum Laravel version to `11.23`, for `Cache::flexible` support
- Improves compilation of custom functions declared within a component's template
- Reduces overall memory utilization
- Simplifies serialized output of dynamic and circular components

## [v1.0.6](https://github.com/Stillat/dagger/compare/v1.0.5...v1.0.6) - 2025-01-31

Expand Down
32 changes: 29 additions & 3 deletions src/Compiler/TemplateCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,14 @@ protected function isDynamicComponent(ComponentNode $node): bool
*/
protected function compileNodes(array $nodes): string
{
if (empty($nodes)) {
return '';
}

$compiled = '';

$lastNodeIndex = $nodes[array_key_last($nodes)]->index;

foreach ($nodes as $node) {
if (! $node instanceof ComponentNode) {
$compiled .= $node->content;
Expand Down Expand Up @@ -402,7 +408,7 @@ protected function compileNodes(array $nodes): string
}

if ($node->tagName === 'compiler:template_end') {
if ($node->hasNextNode()) {
if ($node->index != $lastNodeIndex) {
throw SourceMapper::makeComponentCompilerException(
$node,
'Compiler component [compiler:template_end] must be the last component.',
Expand Down Expand Up @@ -708,10 +714,30 @@ public function compile(string $template): string
->onlyParseComponents()
->parseTemplate($template)
->toDocument()
->getRootNodes()
->getNodes()
->all();

return $this->compileNodes($nodes);
return $this->compileNodes($this->simplifyNodes($nodes));
}

protected function simplifyNodes(array $nodes): array
{
/** @var AbstractNode $node */
foreach ($nodes as $node) {
$node->setDocument(null);
$node->previousNode = $node->nextNode = null;
$node->structure = null;

if ($node instanceof ComponentNode) {
$node->namePosition = null;
$node->parameterContentPosition = null;
}
}

return collect($nodes)
->where(fn (AbstractNode $node) => $node->parent == null)
->values()
->all();
}

protected function storeComponentBlock(string $value): string
Expand Down

0 comments on commit 52b8df2

Please sign in to comment.