Skip to content

Commit 09a6210

Browse files
committed
bug #1525 [LiveComponent] Store TemplateMap in build_dir (smnandre)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Store TemplateMap in `build_dir` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #1522 | License | MIT Per default `build_dir` = `cache_dir` so that won't change a thing for most of installs. But in projects with a distinct build directory, this PR fixes a bug where `cache:clear` did not create the file. (cf #1522) Commits ------- b6d10c3 [LiveComponent] Store TemplateMap in `build_dir`
2 parents e029f36 + b6d10c3 commit 09a6210

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
253253
;
254254

255255
$container->register('ux.live_component.twig.template_mapper', TemplateMap::class)
256-
->setArguments(['%kernel.cache_dir%/'.self::TEMPLATES_MAP_FILENAME]);
256+
->setArguments(['%kernel.build_dir%/'.self::TEMPLATES_MAP_FILENAME]);
257257

258258
$container->register('ux.live_component.twig.cache_warmer', TemplateCacheWarmer::class)
259259
->setArguments([

src/LiveComponent/src/Twig/TemplateCacheWarmer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public function warmUp(string $cacheDir, ?string $buildDir = null): array
3636
$map[hash('xxh128', $item.$this->secret)] = $item;
3737
}
3838

39-
(new PhpArrayAdapter($cacheDir.'/'.$this->cacheFilename, new NullAdapter()))->warmUp(['map' => $map]);
39+
$cacheFile = sprintf('%s%s%s', $buildDir ?? $cacheDir, DIRECTORY_SEPARATOR, $this->cacheFilename);
40+
PhpArrayAdapter::create($cacheFile, new NullAdapter())->warmUp(['map' => $map]);
4041

4142
return [];
4243
}

src/LiveComponent/src/Twig/TemplateMap.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
*/
2222
final class TemplateMap
2323
{
24+
/**
25+
* @var array<string, string> Map of <obscured name> => <template name>
26+
*/
2427
private readonly array $map;
2528

2629
public function __construct(string $cacheFile)
2730
{
28-
$this->map = (new PhpArrayAdapter($cacheFile, new NullAdapter()))->getItem('map')->get();
31+
$this->map = PhpArrayAdapter::create($cacheFile, new NullAdapter())->getItem('map')->get();
2932
}
3033

3134
public function resolve(string $obscuredName): string
@@ -35,8 +38,7 @@ public function resolve(string $obscuredName): string
3538

3639
public function obscuredName(string $templateName): string
3740
{
38-
$obscuredName = array_search($templateName, $this->map, true);
39-
if (false === $obscuredName) {
41+
if (false === $obscuredName = array_search($templateName, $this->map, true)) {
4042
throw new \RuntimeException(sprintf('Cannot find a match for template "%s". Cache may be corrupt.', $templateName));
4143
}
4244

0 commit comments

Comments
 (0)