|
14 | 14 | use Psr\Http\Message\ResponseFactoryInterface; |
15 | 15 | use Psr\Http\Message\ResponseInterface; |
16 | 16 | use Psr\Http\Message\StreamFactoryInterface; |
| 17 | +use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
| 18 | +use Psr\Cache\CacheItemPoolInterface; |
17 | 19 | use WebProject\PhpOpenApiMockServer\Factory\OpenApiMockMiddlewareFactory; |
18 | 20 | use WebProject\PhpOpenApiMockServer\Middleware\ForceMockActiveMiddleware; |
19 | 21 |
|
|
40 | 42 | $container->setService(ResponseFactoryInterface::class, new ResponseFactory()); |
41 | 43 | $container->setService(StreamFactoryInterface::class, new StreamFactory()); |
42 | 44 |
|
43 | | -use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
44 | | -use Psr\Cache\CacheItemPoolInterface; |
| 45 | +// Cache Configuration — isolated per process to avoid conflicts between parallel server instances |
| 46 | +$processCacheDir = sys_get_temp_dir() . '/openapi_mock_cache/' . getmypid(); |
| 47 | +$container->setFactory(CacheItemPoolInterface::class, static fn(): FilesystemAdapter => new FilesystemAdapter('openapi_mock', 0, $processCacheDir)); |
| 48 | + |
| 49 | +register_shutdown_function(static function () use ($processCacheDir): void { |
| 50 | + if (! is_dir($processCacheDir)) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + $iterator = new RecursiveIteratorIterator( |
| 55 | + new RecursiveDirectoryIterator($processCacheDir, FilesystemIterator::SKIP_DOTS), |
| 56 | + RecursiveIteratorIterator::CHILD_FIRST, |
| 57 | + ); |
45 | 58 |
|
46 | | -// ... |
| 59 | + foreach ($iterator as $path) { |
| 60 | + $path->isDir() ? rmdir($path->getPathname()) : unlink($path->getPathname()); |
| 61 | + } |
47 | 62 |
|
48 | | -// Cache Configuration |
49 | | -$container->setFactory(CacheItemPoolInterface::class, static fn(): FilesystemAdapter => new FilesystemAdapter('openapi_mock', 0, dirname(__DIR__) . '/data/cache')); |
| 63 | + rmdir($processCacheDir); |
| 64 | +}); |
50 | 65 |
|
51 | 66 | // Application Middleware |
52 | 67 | $container->setFactory(OpenApiMockMiddleware::class, OpenApiMockMiddlewareFactory::class); |
|
0 commit comments