Skip to content

Commit bd4ae8a

Browse files
fix: lazy load Redis connection in RedisKeyScannerService
Prevented Redis connection during package discovery by making the fullPrefix property lazy-loaded. This fixes composer install failures when Redis is not running. Changes: - Removed readonly from class to allow lazy initialization - Made $fullPrefix nullable with default null value - Added getFullPrefix() method for lazy loading - Connection only established when scanAndParseKeys() is called This resolves PHPStan test failures during CI/CD where Redis may not be available during package discovery.
1 parent b387c49 commit bd4ae8a

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/Services/RedisKeyScannerService.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,28 @@
1010
* Service for scanning and parsing Redis keys to discover entities.
1111
* Provides proper dependency injection and testability.
1212
*/
13-
final readonly class RedisKeyScannerService
13+
final class RedisKeyScannerService
1414
{
15-
private string $fullPrefix;
15+
private ?string $fullPrefix = null;
1616

1717
public function __construct(
18-
private RedisMetricsStore $redisStore,
19-
) {
20-
// Cache the full prefix calculation
21-
/** @var string $redisConnection */
22-
$redisConnection = config('queue-metrics.storage.connection', 'default');
23-
$connectionPrefix = app('redis')->connection($redisConnection)->_prefix('');
24-
$ourPrefix = $this->redisStore->key('');
25-
$this->fullPrefix = $connectionPrefix.$ourPrefix;
18+
private readonly RedisMetricsStore $redisStore,
19+
) {}
20+
21+
/**
22+
* Get the full Redis key prefix (lazy loaded to avoid connection during boot).
23+
*/
24+
private function getFullPrefix(): string
25+
{
26+
if ($this->fullPrefix === null) {
27+
/** @var string $redisConnection */
28+
$redisConnection = config('queue-metrics.storage.connection', 'default');
29+
$connectionPrefix = app('redis')->connection($redisConnection)->_prefix('');
30+
$ourPrefix = $this->redisStore->key('');
31+
$this->fullPrefix = $connectionPrefix.$ourPrefix;
32+
}
33+
34+
return $this->fullPrefix;
2635
}
2736

2837
/**
@@ -46,15 +55,17 @@ public function scanAndParseKeys(string $jobsPattern, string $queuedPattern, cal
4655
return [];
4756
}
4857

58+
$fullPrefix = $this->getFullPrefix();
59+
4960
// Extract unique entities from all keys using the provided parser
5061
$discovered = [];
5162
foreach ($allKeys as $key) {
5263
// Remove the full prefix first
53-
if (! str_starts_with($key, $this->fullPrefix)) {
64+
if (! str_starts_with($key, $fullPrefix)) {
5465
continue;
5566
}
5667

57-
$keyWithoutPrefix = substr($key, strlen($this->fullPrefix));
68+
$keyWithoutPrefix = substr($key, strlen($fullPrefix));
5869

5970
// Parse the key using the provided callable
6071
$entity = $keyParser($keyWithoutPrefix);

0 commit comments

Comments
 (0)