diff --git a/packages/view/src/Elements/ViewComponentElement.php b/packages/view/src/Elements/ViewComponentElement.php index 8e0b30e0e..bc2674c67 100644 --- a/packages/view/src/Elements/ViewComponentElement.php +++ b/packages/view/src/Elements/ViewComponentElement.php @@ -9,6 +9,7 @@ use Tempest\Support\Str\ImmutableString; use Tempest\Support\Str\MutableString; use Tempest\View\Element; +use Tempest\View\Export\ViewObjectExporter; use Tempest\View\Parser\TempestViewCompiler; use Tempest\View\Parser\TempestViewParser; use Tempest\View\Parser\Token; @@ -112,7 +113,7 @@ public function compile(): string ); // Add scoped variables - $slots = $this->getSlots()->toArray(); + $slots = $this->getSlots(); $compiled = $compiled ->prepend( @@ -122,7 +123,7 @@ public function compile(): string // Add dynamic slots to the current scope '', // Store previous slots in temporary variable to keep scope - sprintf('', var_export($slots, true)), // @mago-expect best-practices/no-debug-symbols Set the new value of $slots for this view component + sprintf('', ViewObjectExporter::export($slots)), ) ->append( // Restore previous slots diff --git a/packages/view/src/Export/ExportableViewObject.php b/packages/view/src/Export/ExportableViewObject.php new file mode 100644 index 000000000..c05f64774 --- /dev/null +++ b/packages/view/src/Export/ExportableViewObject.php @@ -0,0 +1,14 @@ +map(function (mixed $value, string|int $key) { + $key = is_int($key) ? $key : "'{$key}'"; + + return $key . ' => ' . rtrim(self::export($value), ';'); + })->implode(', '), + ); + } + + return sprintf( + '\%s::restore(%s);', + $object::class, + $object + ->exportData + ->map(function (mixed $value, string $key) { + $value = match (true) { + $value instanceof ExportableViewObject => self::export($value), + is_string($value) => "<<<'STRING'" . PHP_EOL . $value . PHP_EOL . 'STRING', + default => var_export($value, true), // @mago-expect best-practices/no-debug-symbols + }; + + return "{$key} : {$value}"; + }) + ->implode(','), + ); + } +} diff --git a/packages/view/src/Slot.php b/packages/view/src/Slot.php index 211352829..881c2c600 100644 --- a/packages/view/src/Slot.php +++ b/packages/view/src/Slot.php @@ -4,17 +4,45 @@ namespace Tempest\View; +use Tempest\Reflection\ClassReflector; +use Tempest\Support\Arr\ImmutableArray; +use Tempest\View\Export\ExportableViewObject; use Tempest\View\Parser\Token; -final class Slot +final class Slot implements ExportableViewObject { public const string DEFAULT = 'default'; public function __construct( public string $name, public array $attributes, - public string $content, - ) {} + string $content, + ) { + $this->content = base64_encode($content); + } + + public string $content { + get => base64_decode($this->content); + } + + public ImmutableArray $exportData { + get => new ImmutableArray([ + 'name' => $this->name, + 'attributes' => $this->attributes, + 'content' => base64_encode($this->content), + ]); + } + + public static function restore(mixed ...$data): ExportableViewObject + { + $self = new ClassReflector(self::class)->newInstanceWithoutConstructor(); + + $self->name = $data['name']; + $self->attributes = $data['attributes']; + $self->content = $data['content']; + + return $self; + } public function __get(string $name): mixed { @@ -50,9 +78,4 @@ public static function default(Token ...$tokens): self content: $content, ); } - - public static function __set_state(array $array): object - { - return new self(...$array); - } }