Skip to content

Commit 78fcd31

Browse files
committed
add force_generate_template option
1 parent 3331467 commit 78fcd31

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/Symfony/Component/SerDes/Context/SerializeContext.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function toArray(): array
3434
return $this->options;
3535
}
3636

37+
public function withForceGenerateTemplate(bool $forceGenerateTemplate = true): self
38+
{
39+
return new self(['force_generate_template' => $forceGenerateTemplate] + $this->options);
40+
}
41+
3742
public function withType(string $type): self
3843
{
3944
return new self(['type' => $type] + $this->options);

src/Symfony/Component/SerDes/Resources/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function serialize(mixed $data, $resource, string $format, array $context = []):
3939
$cacheDir = $context['cache_dir'] ?? sys_get_temp_dir().\DIRECTORY_SEPARATOR.'symfony_ser_des';
4040
$cacheFilename = sprintf('%s%s%s.%s.php', $cacheDir, \DIRECTORY_SEPARATOR, hash('xxh128', $type), $format);
4141

42-
if (!file_exists($cacheFilename)) {
42+
if (!file_exists($cacheFilename) || ($context['force_generate_template'] ?? false)) {
4343
if (!file_exists($cacheDir)) {
4444
mkdir($cacheDir, recursive: true);
4545
}

src/Symfony/Component/SerDes/Tests/Context/SerializeContextTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function testWithers()
2121
$hook = static function () {};
2222

2323
$context = (new SerializeContext(['constructor_option' => true, 'hooks' => ['serialize' => ['constructor_hook' => $hook]]]))
24+
->withForceGenerateTemplate()
2425
->withType('TYPE')
2526
->withJsonEncodeFlags(123)
2627
->withUnionSelector(['int|string' => 'int'])
@@ -42,6 +43,7 @@ public function testWithers()
4243
'union_selector' => ['int|string' => 'int'],
4344
'json_encode_flags' => 123,
4445
'type' => 'TYPE',
46+
'force_generate_template' => true,
4547
'constructor_option' => true,
4648
], $context->toArray());
4749
}

src/Symfony/Component/SerDes/Tests/Internal/Serialize/SerializeTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ public function testCreateCacheFileOnlyIfNotExists()
7777
$this->assertSame('CACHED_FILE', $serialized);
7878
}
7979

80+
public function testRecreateCacheFileIfForceGenerateTemplate()
81+
{
82+
$cacheFilename = sprintf('%s/%s.json.php', $this->cacheDir, hash('xxh128', 'int'));
83+
if (!file_exists($this->cacheDir)) {
84+
mkdir($this->cacheDir, recursive: true);
85+
}
86+
87+
file_put_contents($cacheFilename, '<?php return static function ($data, $resource) { \fwrite($resource, "CACHED_FILE"); };');
88+
89+
/** @var resource $resource */
90+
$resource = fopen('php://temp', 'w');
91+
serialize(1, $resource, 'json', ['force_generate_template' => true]);
92+
93+
rewind($resource);
94+
95+
$serialized = stream_get_contents($resource);
96+
97+
$this->assertSame('1', $serialized);
98+
}
99+
80100
public function testThrowOnUnknownFormat()
81101
{
82102
$this->expectException(UnsupportedFormatException::class);

0 commit comments

Comments
 (0)