8000 [AssetMapper] Leverage Filesystem · symfony/symfony@fc9308a · GitHub
[go: up one dir, main page]

Skip to content

Commit fc9308a

Browse files
smnandrenicolas-grekas
authored andcommitted
[AssetMapper] Leverage Filesystem
1 parent 92c52e9 commit fc9308a

File tree

7 files changed

+43
-31
lines changed

7 files changed

+43
-31
lines changed

src/Symfony/Component/AssetMapper/CompiledAssetMapperConfigReader.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public function loadConfig(string $filename): array
4040
public function saveConfig(string $filename, array $data): string
4141
{
4242
$path = Path::join($this->directory, $filename);
43-
@mkdir(\dirname($path), 0777, true);
44-
file_put_contents($path, json_encode($data, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR));
43+
$this->filesystem->dumpFile($path, json_encode($data, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR));
4544

4645
return $path;
4746
}
@@ -51,7 +50,7 @@ public function removeConfig(string $filename): void
5150
$path = Path::join($this->directory, $filename);
5251

5352
if (is_file($path)) {
54-
unlink($path);
53+
$this->filesystem->remove($path);
5554
}
5655
}
5756
}

src/Symfony/Component/AssetMapper/ImportMap/ImportMapConfigReader.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\AssetMapper\ImportMap;
1313

1414
use Symfony\Component\AssetMapper\Exception\RuntimeException;
15+
use Symfony\Component\Filesystem\Filesystem;
1516
use Symfony\Component\Filesystem\Path;
1617
use Symfony\Component\VarExporter\VarExporter;
1718

@@ -23,11 +24,13 @@
2324
class ImportMapConfigReader
2425
{
2526
private ImportMapEntries $rootImportMapEntries;
27+
private readonly Filesystem $filesystem;
2628

2729
public function __construct(
2830
private readonly string $importMapConfigPath,
2931
private readonly RemotePackageStorage $remotePackageStorage,
3032
) {
33+
$this->filesystem = new Filesystem();
3134
}
3235

3336
public function getEntries(): ImportMapEntries
@@ -101,7 +104,7 @@ public function writeEntries(ImportMapEntries $entries): void
101104
}
102105

103106
$map = class_exists(VarExporter::class) ? VarExporter::export($importMapConfig) : var_export($importMapConfig, true);
104-
file_put_contents($this->importMapConfigPath, <<<EOF
107+
$this->filesystem->dumpFile($this->importMapConfigPath, <<<EOF
105108
<?php
106109
107110
/**

src/Symfony/Component/AssetMapper/ImportMap/RemotePackageDownloader.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\AssetMapper\ImportMap;
1313

1414
use Symfony\Component\AssetMapper\ImportMap\Resolver\PackageResolverInterface;
15+
use Symfony\Component\Filesystem\Filesystem;
1516

1617
/**
1718
* @final
@@ -20,11 +21,14 @@ class RemotePackageDownloader
2021
{
2122
private array $installed;
2223

24+
private readonly Filesystem $filesystem;
25+
2326
public function __construct(
2427
private readonly RemotePackageStorage $remotePackageStorage,
2528
private readonly ImportMapConfigReader $importMapConfigReader,
2629
private readonly PackageResolverInterface $packageResolver,
2730
) {
31+
$this->filesystem = new Filesystem();
2832
}
2933

3034
/**
@@ -146,7 +150,10 @@ private function loadInstalled(): array
146150
private function saveInstalled(array $installed): void
147151
{
148152
$this->installed = $installed;
149-
file_put_contents($this->remotePackageStorage->getStorageDir().'/installed.php', \sprintf('<?php return %s;', var_export($installed, true)));
153+
$this->filesystem->dumpFile(
154+
$this->remotePackageStorage->getStorageDir().'/installed.php',
155+
'<?php return '.var_export($installed, true).';',
156+
);
150157
}
151158

152159
private function areAllExtraFilesDownloaded(ImportMapEntry $entry, array $extraFilenames): bool

src/Symfony/Component/AssetMapper/ImportMap/RemotePackageStorage.php

Lines changed: 21 additions & 6 deletions
19
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
namespace Symfony\Component\AssetMapper\ImportMap;
1313

1414
use Symfony\Component\AssetMapper\Exception\RuntimeException;
15+
use Symfony\Component\Filesystem\Exception\IOException;
16+
use Symfony\Component\Filesystem\Filesystem;
1517

1618
/**
1719
* Manages the local storage of remote/vendor importmap packages.
1820
*/
21
class RemotePackageStorage
2022
{
23+
private Filesystem $filesystem;
24+
2125
public function __construct(private readonly string $vendorDir)
2226
{
27+
$this->filesystem = new Filesystem();
2328
}
2429

2530
public function getStorageDir(): string
@@ -53,9 +58,14 @@ public function save(ImportMapEntry $entry, string $contents): void
5358

5459
$vendorPath = $this->getDownloadPath($entry->packageModuleSpecifier, $entry->type);
5560

56-
@mkdir(\dirname($vendorPath), 0777, true);
57-
if (false === @file_put_contents($vendorPath, $contents)) {
58-
throw new RuntimeException(error_get_last()['message'] ?? \sprintf('Failed to write file "%s".', $vendorPath));
61+
try {
62+
$this->filesystem->dumpFile($vendorPath, $contents);
63+
} catch (IOException $e) {
64+
throw new RuntimeException(\sprintf('Failed to write file "%s".', $vendorPath), 0, $e);
65+
}
66+
67+
if (!is_file($vendorPath) || !is_writable($vendorPath)) {
68+
throw new RuntimeException(\sprintf('Failed to write file "%s".', $vendorPath));
5969
}
6070
}
6171

@@ -67,9 +77,14 @@ public function saveExtraFile(ImportMapEntry $entry, string $extraFilename, stri
6777

6878
$vendorPath = $this->getExtraFileDownloadPath($entry, $extraFilename);
6979

70-
@mkdir(\dirname($vendorPath), 0777, true);
71-
if (false === @file_put_contents($vendorPath, $contents)) {
72-
throw new RuntimeException(error_get_last()['message'] ?? \sprintf('Failed to write file "%s".', $vendorPath));
80+
try {
81+
$this->filesystem->dumpFile($vendorPath, $contents);
82+
} catch (IOException $e) {
83+
throw new RuntimeException(\sprintf('Failed to write file "%s".', $vendorPath), 0, $e);
84+
}
85+
86+
if (!is_file($vendorPath) || !is_writable($vendorPath)) {
87+
throw new RuntimeException(\sprintf('Failed to write file "%s".', $vendorPath));
7388
}
7489
}
7590

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapManagerTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,7 @@ private function mockImportMap(array $importMapEntries): void
409409

410410
private function writeFile(string $filename, string $content): void
411411
{
412-
$path = \dirname(self::$writableRoot.'/'.$filename);
413-
if (!is_dir($path)) {
414-
mkdir($path, 0777, true);
415-
}
416-
file_put_contents(self::$writableRoot.'/'.$filename, $content);
412+
$this->filesystem->dumpFile(self::$writableRoot.'/'.$filename, $content);
417413
}
418414

419415
private static function createLocalEntry(string $importName, string $path, ImportMapType $type = ImportMapType::JS, bool $isEntrypoint = false): ImportMapEntry

src/Symfony/Component/AssetMapper/Tests/ImportMap/RemotePackageDownloaderTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ class RemotePackageDownloaderTest extends TestCase
2929
protected function setUp(): void
3030
{
3131
$this->filesystem = new Filesystem();
32-
if (!file_exists(self::$writableRoot)) {
33-
$this->filesystem->mkdir(self::$writableRoot);
34-
}
32+
$this->filesystem->mkdir(self::$writableRoot);
3533
}
3634

3735
protected function tearDown(): void
@@ -106,24 +104,22 @@ public function testPackagesWithCorrectInstalledVersionSkipped()
106104
'bar.js/file' => ['version' => '1.0.0', 'dependencies' => [], 'extraFiles' => []],
107105
'baz' => ['version' => '1.0.0', 'dependencies' => [], 'extraFiles' => []],
108106
];
109-
file_put_contents(
107+
$this->filesystem->dumpFile(
110108
self::$writableRoot.&# 10000 39;/assets/vendor/installed.php',
111-
'<?php return '.var_export($installed, true).';'
109+
'<?php return '.var_export($installed, true).';',
112110
);
113111

114112
$configReader = $this->createMock(ImportMapConfigReader::class);
115113
$packageResolver = $this->createMock(PackageResolverInterface::class);
116114

117115
// matches installed version and file exists
118116
$entry1 = ImportMapEntry::createRemote('foo', ImportMapType::JS, path: '/any', version: '1.0.0', packageModuleSpecifier: 'foo', isEntrypoint: false);
119-
@mkdir(self::$writableRoot.'/assets/vendor/foo', 0777, true);
120-
file_put_contents(self::$writableRoot.'/assets/vendor/foo/foo.index.js', 'original foo content');
117+
$this->filesystem->dumpFile(self::$writableRoot.'/assets/vendor/foo/foo.index.js', 'original foo content');
121118
// matches installed version but file does not exist
122119
$entry2 = ImportMapEntry::createRemote('bar.js/file', ImportMapType::JS, path: '/any', version: '1.0.0', packageModuleSpecifier: 'bar.js/file', isEntrypoint: false);
123120
// does not match installed version
124121
$entry3 = ImportMapEntry::createRemote('baz', ImportMapType::CSS, path: '/any', version: '1.1.0', packageModuleSpecifier: 'baz', isEntrypoint: false);
125-
@mkdir(self::$writableRoot.'/assets/vendor/baz', 0777, true);
126-
file_put_contents(self::$writableRoot.'/assets/vendor/baz/baz.index.css', 'original baz content');
122+
$this->filesystem->dumpFile(self::$writableRoot.'/assets/vendor/baz/baz.index.css', 'original baz content');
127123
// matches installed & file exists, but has missing extra file
128124
$entry4 = ImportMapEntry::createRemote('has-missing-extra', ImportMapType::JS, path: '/any', version: '1.0.0', packageModuleSpecifier: 'has-missing-extra', isEntrypoint: false);
129125
$importMapEntries = new ImportMapEntries([$entry1, $entry2, $entry3, $entry4]);

src/Symfony/Component/AssetMapper/Tests/ImportMap/RemotePackageStorageTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,9 @@ public function testSaveThrowsWhenFailing()
5252
$entry = ImportMapEntry::createRemote('foo', ImportMapType::JS, '/does/not/matter', '1.0.0', 'module_specifier', false);
5353

5454
$this->expectException(\RuntimeException::class);
55-
$this->expectExceptionMessage('file_put_contents('.$vendorDir.'/module_specifier/module_specifier.index.js): Failed to open stream: Permission denied');
55+
$this->expectExceptionMessage('Failed to write file "'.$vendorDir.'/module_specifier/module_specifier.index.js');
5656

57-
try {
58-
$storage->save($entry, 'any content');
59-
} finally {
60-
$this->filesystem->chmod($vendorDir.'/module_specifier/module_specifier.index.js', 0777);
61-
}
57+
$storage->save($entry, 'any content');
6258
}
6359

6460
public function testIsDownloaded()

0 commit comments

Comments
 (0)
0