Skip to content

Commit 7beecad

Browse files
authored
Merge pull request #405 from vyacheslavdanilin/fix/configurable-cache-dir
fix: make stub cache directory configurable via env
2 parents 3bdf847 + 2a5eb1a commit 7beecad

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/Providers/CacheDirectoryProvider.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@
44

55
namespace Psalm\LaravelPlugin\Providers;
66

7+
use function getenv;
8+
use function rtrim;
9+
10+
use const DIRECTORY_SEPARATOR;
11+
712
final class CacheDirectoryProvider
813
{
914
public static function getCacheLocation(): string
1015
{
11-
return __DIR__ . '/../../cache';
16+
$env = getenv('PSALM_LARAVEL_PLUGIN_CACHE_PATH');
17+
if ($env !== false && $env !== '') {
18+
return rtrim($env, DIRECTORY_SEPARATOR);
19+
}
20+
21+
return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'cache';
1222
}
1323
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit\Providers;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Psalm\LaravelPlugin\Providers\CacheDirectoryProvider;
9+
10+
use function putenv;
11+
use function sys_get_temp_dir;
12+
13+
use const DIRECTORY_SEPARATOR;
14+
15+
final class CacheDirectoryProviderTest extends TestCase
16+
{
17+
protected function tearDown(): void
18+
{
19+
putenv('PSALM_LARAVEL_PLUGIN_CACHE_PATH');
20+
}
21+
22+
public function testReturnsDefaultPathIfEnvNotSet(): void
23+
{
24+
$path = CacheDirectoryProvider::getCacheLocation();
25+
26+
$this->assertStringEndsWith(DIRECTORY_SEPARATOR . 'cache', $path);
27+
$this->assertFileExists($path);
28+
}
29+
30+
public function testReturnsCustomPathFromEnv(): void
31+
{
32+
$custom = sys_get_temp_dir() . '/psalm-laravel-stubs-test';
33+
putenv("PSALM_LARAVEL_PLUGIN_CACHE_PATH={$custom}");
34+
35+
$this->assertSame($custom, CacheDirectoryProvider::getCacheLocation());
36+
}
37+
}

0 commit comments

Comments
 (0)