Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[AssetMapper] Leverage Filesystem #57890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public function loadConfig(string $filename): array
public function saveConfig(string $filename, array $data): string
{
$path = Path::join($this->directory, $filename);
@mkdir(\dirname($path), 0777, true);
file_put_contents($path, json_encode($data, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR));
$this->filesystem->dumpFile($path, json_encode($data, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR));

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

if (is_file($path)) {
unlink($path);
$this->filesystem->remove($path);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\AssetMapper\ImportMap;

use Symfony\Component\AssetMapper\Exception\RuntimeException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\VarExporter\VarExporter;

Expand All @@ -23,11 +24,13 @@
class ImportMapConfigReader
{
private ImportMapEntries $rootImportMapEntries;
private readonly Filesystem $filesystem;

public function __construct(
private readonly string $importMapConfigPath,
private readonly RemotePackageStorage $remotePackageStorage,
) {
$this->filesystem = new Filesystem();
}

public function getEntries(): ImportMapEntries
Expand Down Expand Up @@ -101,7 +104,7 @@ public function writeEntries(ImportMapEntries $entries): void
}

$map = class_exists(VarExporter::class) ? VarExporter::export($importMapConfig) : var_export($importMapConfig, true);
file_put_contents($this->importMapConfigPath, <<<EOF
$this->filesystem->dumpFile($this->importMapConfigPath, <<<EOF
<?php

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\AssetMapper\ImportMap;

use Symfony\Component\AssetMapper\ImportMap\Resolver\PackageResolverInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
* @final
Expand All @@ -20,11 +21,14 @@ class RemotePackageDownloader
{
private array $installed;

private readonly Filesystem $filesystem;

public function __construct(
private readonly RemotePackageStorage $remotePackageStorage,
private readonly ImportMapConfigReader $importMapConfigReader,
private readonly PackageResolverInterface $packageResolver,
) {
$this->filesystem = new Filesystem();
}

/**
Expand Down Expand Up @@ -146,7 +150,10 @@ private function loadInstalled(): array
private function saveInstalled(array $installed): void
{
$this->installed = $installed;
file_put_contents($this->remotePackageStorage->getStorageDir().'/installed.php', \sprintf('<?php return %s;', var_export($installed, true)));
$this->filesystem->dumpFile(
$this->remotePackageStorage->getStorageDir().'/installed.php',
'<?php return '.var_export($installed, true).';',
);
}

private function areAllExtraFilesDownloaded(ImportMapEntry $entry, array $extraFilenames): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
namespace Symfony\Component\AssetMapper\ImportMap;

use Symfony\Component\AssetMapper\Exception\RuntimeException;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;

/**
* Manages the local storage of remote/vendor importmap packages.
*/
class RemotePackageStorage
{
private readonly Filesystem $filesystem;

public function __construct(private readonly string $vendorDir)
{
$this->filesystem = new Filesystem();
}

public function getStorageDir(): string
Expand Down Expand Up @@ -53,9 +58,10 @@ public function save(ImportMapEntry $entry, string $contents): void

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

@mkdir(\dirname($vendorPath), 0777, true);
if (false === @file_put_contents($vendorPath, $contents)) {
throw new RuntimeException(error_get_last()['message'] ?? \sprintf('Failed to write file "%s".', $vendorPath));
try {
$this->filesystem->dumpFile($vendorPath, $contents);
} catch (IOException $e) {
throw new RuntimeException(\sprintf('Failed to write file "%s".', $vendorPath), 0, $e);
}
}

Expand All @@ -67,9 +73,10 @@ public function saveExtraFile(ImportMapEntry $entry, string $extraFilename, stri

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

@mkdir(\dirname($vendorPath), 0777, true);
if (false === @file_put_contents($vendorPath, $contents)) {
throw new RuntimeException(error_get_last()['message'] ?? \sprintf('Failed to write file "%s".', $vendorPath));
try {
$this->filesystem->dumpFile($vendorPath, $contents);
} catch (IOException $e) {
throw new RuntimeException(\sprintf('Failed to write file "%s".', $vendorPath), 0, $e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,7 @@ private function mockImportMap(array $importMapEntries): void

private function writeFile(string $filename, string $content): void
{
$path = \dirname(self::$writableRoot.'/'.$filename);
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
file_put_contents(self::$writableRoot.'/'.$filename, $content);
$this->filesystem->dumpFile(self::$writableRoot.'/'.$filename, $content);
}

private static function createLocalEntry(string $importName, string $path, ImportMapType $type = ImportMapType::JS, bool $isEntrypoint = false): ImportMapEntry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class RemotePackageDownloaderTest extends TestCase
protected function setUp(): void
{
$this->filesystem = new Filesystem();
if (!file_exists(self::$writableRoot)) {
$this->filesystem->mkdir(self::$writableRoot);
}
$this->filesystem->mkdir(self::$writableRoot);
}

protected function tearDown(): void
Expand Down Expand Up @@ -106,24 +104,22 @@ public function testPackagesWithCorrectInstalledVersionSkipped()
'bar.js/file' => ['version' => '1.0.0', 'dependencies' => [], 'extraFiles' => []],
'baz' => ['version' => '1.0.0', 'dependencies' => [], 'extraFiles' => []],
];
file_put_contents(
$this->filesystem->dumpFile(
self::$writableRoot.'/assets/vendor/installed.php',
'<?php return '.var_export($installed, true).';'
'<?php return '.var_export($installed, true).';',
);

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

// matches installed version and file exists
$entry1 = ImportMapEntry::createRemote('foo', ImportMapType::JS, path: '/any', version: '1.0.0', packageModuleSpecifier: 'foo', isEntrypoint: false);
@mkdir(self::$writableRoot.'/assets/vendor/foo', 0777, true);
file_put_contents(self::$writableRoot.'/assets/vendor/foo/foo.index.js', 'original foo content');
$this->filesystem->dumpFile(self::$writableRoot.'/assets/vendor/foo/foo.index.js', 'original foo content');
// matches installed version but file does not exist
$entry2 = ImportMapEntry::createRemote('bar.js/file', ImportMapType::JS, path: '/any', version: '1.0.0', packageModuleSpecifier: 'bar.js/file', isEntrypoint: false);
// does not match installed version
$entry3 = ImportMapEntry::createRemote('baz', ImportMapType::CSS, path: '/any', version: '1.1.0', packageModuleSpecifier: 'baz', isEntrypoint: false);
@mkdir(self::$writableRoot.'/assets/vendor/baz', 0777, true);
file_put_contents(self::$writableRoot.'/assets/vendor/baz/baz.index.css', 'original baz content');
$this->filesystem->dumpFile(self::$writableRoot.'/assets/vendor/baz/baz.index.css', 'original baz content');
// matches installed & file exists, but has missing extra file
$entry4 = ImportMapEntry::createRemote('has-missing-extra', ImportMapType::JS, path: '/any', version: '1.0.0', packageModuleSpecifier: 'has-missing-extra', isEntrypoint: false);
$importMapEntries = new ImportMapEntries([$entry1, $entry2, $entry3, $entry4]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,26 @@ public function testSaveThrowsWhenFailing()
$vendorDir = self::$writableRoot.'/assets/acme/vendor';
$this->filesystem->mkdir($vendorDir.'/module_specifier');
$this->filesystem->touch($vendorDir.'/module_specifier/module_specifier.index.js');
$this->filesystem->chmod($vendorDir.'/module_specifier/module_specifier.index.js', 0555);
if ('\\' === \DIRECTORY_SEPARATOR) {
$this->filesystem->chmod($vendorDir.'/module_specifier/module_specifier.index.js', 0555);
} else {
$this->filesystem->chmod($vendorDir.'/module_specifier/', 0555);
}

$storage = new RemotePackageStorage($vendorDir);
$entry = ImportMapEntry::createRemote('foo', ImportMapType::JS, '/does/not/matter', '1.0.0', 'module_specifier', false);

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

try {
$storage->save($entry, 'any content');
} finally {
$this->filesystem->chmod($vendorDir.'/module_specifier/module_specifier.index.js', 0777);
if ('\\' === \DIRECTORY_SEPARATOR) {
$this->filesystem->chmod($vendorDir.'/module_specifier/module_specifier.index.js', 0777);
} else {
$this->filesystem->chmod($vendorDir.'/module_specifier/', 0777);
}
}
}

Expand Down
Loading