Skip to content

Commit

Permalink
Do not load config from empty cache file
Browse files Browse the repository at this point in the history
This change solves a fatal error that I've been encountering, whenever
the cache file is present, but empty:

Uncaught TypeError: Cannot assign int to property
Laminas\ConfigAggregator\ConfigAggregator::$config of type array

So, this change returns false if the config file is present but empty.

Signed-off-by: Matthew Setter <[email protected]>
  • Loading branch information
settermjd committed Jun 19, 2024
1 parent 49df885 commit 3d200e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ConfigAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use function class_exists;
use function date;
use function file_exists;
use function filesize;
use function gettype;
use function is_array;
use function is_callable;
Expand Down Expand Up @@ -257,6 +258,10 @@ private function loadConfigFromCache(null|string $cachedConfigFile): bool
return false;
}

if (filesize($cachedConfigFile) === 0) {
return false;
}

$this->config = require $cachedConfigFile;
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions test/ConfigAggregatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ public function testConfigAggregatorSetsHandlesUnwritableCache(): void
self::assertFileDoesNotExist($this->cacheFile);
}

public function testConfigAggregatorDoesNotLoadConfigFromCacheIfCacheFileIsEmpty(): void
{
file_put_contents($this->cacheFile, '');
$aggregator = new ConfigAggregator([], $this->cacheFile);

self::assertEmpty($aggregator->getMergedConfig());
}

public function testConfigAggregatorCanLoadConfigFromCache(): void
{
$expected = [
Expand Down

0 comments on commit 3d200e8

Please sign in to comment.