Skip to content

Commit e99e7b7

Browse files
committed
Cache: Fix cache content schema Windows compatibility
1 parent b915513 commit e99e7b7

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/WpOrg/Api/Cache.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,11 @@ public function read(string $slug): ?bool
2525
return null;
2626
}
2727

28-
$content = $this->cache->read($key);
28+
$content = (string) $this->cache->read($key);
2929

30-
// Should never happen because of the age check.
31-
if ($content === false) {
32-
return null;
33-
}
34-
35-
$lines = explode("\n", $content);
36-
37-
return match ($lines[0]) {
38-
'closed' => true,
39-
'open' => false,
30+
return match (true) {
31+
str_starts_with($content, 'closed'.PHP_EOL) => true,
32+
str_starts_with($content, 'open'.PHP_EOL) => false,
4033
default => null, // Unexpected content. Treat as a cache miss.
4134
};
4235
}
@@ -47,12 +40,12 @@ public function write(string $slug, bool $isClosed): void
4740
return;
4841
}
4942

50-
$content = sprintf(
51-
"%s\n%s\n%s\n",
52-
$isClosed ? 'closed' : 'open',
53-
$slug,
54-
date(DateTimeInterface::RFC3339),
55-
);
43+
$content = $isClosed ? 'closed' : 'open';
44+
$content .= PHP_EOL;
45+
$content .= $slug;
46+
$content .= PHP_EOL;
47+
$content .= date(DateTimeInterface::RFC3339);
48+
$content .= PHP_EOL;
5649

5750
$this->cache->write(
5851
$this->key($slug),

tests/Unit/WpOrg/Api/CacheTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
describe('::read()', static function (): void {
2424
it('reads the first line of from {$slug}.txt',
2525
function (string $slug, bool $expected, string $firstLine): void {
26+
$cachedContent = $firstLine.PHP_EOL.$slug.PHP_EOL.'2006-01-02T15:04:05+07:00'.PHP_EOL;
2627
$composerCache = Mockery::mock(ComposerCache::class);
2728
$composerCache->expects()
2829
->getAge()
@@ -31,7 +32,7 @@ function (string $slug, bool $expected, string $firstLine): void {
3132
$composerCache->expects()
3233
->read()
3334
->with("{$slug}.txt")
34-
->andReturn("{$firstLine}\n{$slug}\n2006-01-02T15:04:05+07:00\n");
35+
->andReturn($cachedContent);
3536

3637
$cache = new Cache($composerCache);
3738

0 commit comments

Comments
 (0)