Skip to content

Commit 86bbd0c

Browse files
committed
Atomic write file via symfony/filesystem
1 parent 85e5076 commit 86bbd0c

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/PTS/SymfonyDiLoader/LoaderContainer.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
1111
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
1212
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13+
use Symfony\Component\Filesystem\Filesystem;
1314
use Throwable;
1415

1516
class LoaderContainer implements LoaderContainerInterface
1617
{
1718
/** @var string[] */
1819
protected array $configFiles = [];
1920

21+
protected ?Filesystem $fs = null;
2022
protected FactoryContainer $factory;
2123
protected ?ContainerInterface $container = null;
2224
protected CacheWatcher $cacheWatcher;
@@ -84,7 +86,7 @@ protected function createContainer(array $configs, array $extensions = []): Cont
8486
protected function dumpMeta(string $filePath, array $configFiles): void
8587
{
8688
try {
87-
file_put_contents($filePath, serialize($configFiles));
89+
$this->getFilesystem()->dumpFile($filePath, serialize($configFiles));
8890
} catch (Throwable $throwable) {
8991
throw new RuntimeException('Can`t dump meta for DI container', 0, $throwable);
9092
}
@@ -95,9 +97,9 @@ protected function dump(string $filePath, string $className, ContainerBuilder $c
9597
$dumper = new PhpDumper($container);
9698

9799
try {
98-
file_put_contents($filePath, $dumper->dump([
99-
'class' => $className,
100-
]));
100+
$this->getFilesystem()->dumpFile($filePath, $dumper->dump([
101+
'class' => $className,
102+
]));
101103
} catch (Throwable $throwable) {
102104
throw new RuntimeException('Can`t dump cache for DI container', 0, $throwable);
103105
}
@@ -127,4 +129,13 @@ protected function getWatcher(): CacheWatcher
127129
{
128130
return $this->cacheWatcher;
129131
}
132+
133+
protected function getFilesystem(): Filesystem
134+
{
135+
if ($this->fs === null) {
136+
$this->fs = new Filesystem;
137+
}
138+
139+
return $this->fs;
140+
}
130141
}

test/unit/LoaderContainer/DumpMetaTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public function testDumpMetaNotPermission(): void
4141
$cacheFileName = 'cache.php';
4242
$loader = new LoaderContainer($configs, $cacheFileName);
4343

44-
$vfs = vfsStream::setup('temp-di-loader');
45-
$path = vfsStream::newFile($cacheFileName . '.meta', 0444)->at($vfs)->url();
44+
$vfs = vfsStream::setup('temp-di-loader', 0550);
45+
$path = vfsStream::newFile($cacheFileName . '.meta', 0440)->at($vfs)->url();
4646

4747
$this->expectException(\RuntimeException::class);
4848
$this->expectExceptionMessage('Can`t dump meta for DI container');

test/unit/LoaderContainer/DumpTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public function testDumpNotPermission(): void
5555
->getMock();
5656
$container->method('isCompiled')->willReturn(true);
5757

58-
$vfs = vfsStream::setup('temp-di-loader');
59-
$path = vfsStream::newFile($cacheFileName, 0444)->at($vfs)->url();
58+
$vfs = vfsStream::setup('temp-di-loader', 0550);
59+
$path = vfsStream::newFile($cacheFileName, 0440)->at($vfs)->url();
6060

6161
$this->expectException(RuntimeException::class);
6262
$this->expectExceptionMessage('Can`t dump cache for DI container');

0 commit comments

Comments
 (0)