Skip to content

Commit cbb4582

Browse files
Fix DTO config serialization for config caching (#9)
Laravel config caching uses var_export during serialization, which broke SlideWire's DTO-based config values. Add __set_state support to the config DTOs and cover the round-trip behavior with a regression test.
1 parent c82a2ab commit cbb4582

6 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/DTOs/FontConfig.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public function __construct(
1717
public array $weights = [],
1818
) {}
1919

20+
/**
21+
* @param array{source: FontSource, weights: list<int>} $properties
22+
*/
23+
public static function __set_state(array $properties): self
24+
{
25+
return new self(...$properties);
26+
}
27+
2028
public function __toString(): string
2129
{
2230
return $this->source->value;

src/DTOs/HighlightConfig.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ public function __construct(
1414
public string $font = 'JetBrainsMono',
1515
public string $fontSize = 'text-base',
1616
) {}
17+
18+
/**
19+
* @param array{enabled: bool, theme: Theme, font: string, fontSize: string} $properties
20+
*/
21+
public static function __set_state(array $properties): self
22+
{
23+
return new self(...$properties);
24+
}
1725
}

src/DTOs/SlidesConfig.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ public function __construct(
2323
public bool $autoSlidePauseOnInteraction = true,
2424
public HighlightConfig $highlight = new HighlightConfig(fontSize: 'text-base'),
2525
) {}
26+
27+
/**
28+
* @param array{theme: string, showControls: bool, showProgress: bool, showFullscreenButton: bool, keyboard: bool, touch: bool, transition: SlideTransition, transitionDuration: int, transitionSpeed: SlideTransitionSpeed, autoSlide: int, autoSlidePauseOnInteraction: bool, highlight: HighlightConfig} $properties
29+
*/
30+
public static function __set_state(array $properties): self
31+
{
32+
return new self(...$properties);
33+
}
2634
}

src/DTOs/ThemeConfig.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public function __construct(
1616
public ThemeFont $text,
1717
) {}
1818

19+
/**
20+
* @param array{background: string, highlightTheme: Theme, title: ThemeFont, text: ThemeFont} $properties
21+
*/
22+
public static function __set_state(array $properties): self
23+
{
24+
return new self(...$properties);
25+
}
26+
1927
public function __toString(): string
2028
{
2129
return $this->background;

src/DTOs/ThemeFont.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public function __construct(
1414
public string $size,
1515
) {}
1616

17+
/**
18+
* @param array{font: string, color: string, size: string} $properties
19+
*/
20+
public static function __set_state(array $properties): self
21+
{
22+
return new self(...$properties);
23+
}
24+
1725
public function __toString(): string
1826
{
1927
return implode(' ', array_filter([$this->font, $this->color, $this->size]));

tests/Unit/ConfigValidatorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,37 @@
9292
highlight: new HighlightConfig(fontSize: ' '),
9393
));
9494
})->throws(InvalidArgumentException::class, 'font_size must be a non-empty string');
95+
96+
it('supports var export hydration for slidewire config dto objects', function (): void {
97+
$slides = new SlidesConfig(
98+
transition: SlideTransition::Fade,
99+
transitionSpeed: SlideTransitionSpeed::Fast,
100+
highlight: new HighlightConfig(
101+
enabled: false,
102+
theme: Theme::GithubDark,
103+
font: 'FiraCode',
104+
fontSize: 'text-sm',
105+
),
106+
);
107+
108+
$theme = new ThemeConfig(
109+
background: 'bg-slate-950 text-white',
110+
highlightTheme: Theme::GithubDark,
111+
title: new ThemeFont('Sora', 'text-white', 'text-5xl'),
112+
text: new ThemeFont('Sora', 'text-slate-200', 'text-lg'),
113+
);
114+
115+
$font = new FontConfig(FontSource::Google, [400, 600, 700]);
116+
117+
/** @var SlidesConfig $rehydratedSlides */
118+
$rehydratedSlides = eval('return ' . var_export($slides, true) . ';');
119+
/** @var ThemeConfig $rehydratedTheme */
120+
$rehydratedTheme = eval('return ' . var_export($theme, true) . ';');
121+
/** @var FontConfig $rehydratedFont */
122+
$rehydratedFont = eval('return ' . var_export($font, true) . ';');
123+
124+
expect($rehydratedSlides)
125+
->toEqual($slides)
126+
->and($rehydratedTheme)->toEqual($theme)
127+
->and($rehydratedFont)->toEqual($font);
128+
});

0 commit comments

Comments
 (0)