Skip to content

Commit 632ff62

Browse files
committed
[Filesystem] Add the readFile() method
1 parent 719803d commit 632ff62

25 files changed

+122
-49
lines changed

link

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ $directories = array_merge(...array_values(array_map(function ($part) {
4949
$directories[] = __DIR__.'/src/Symfony/Contracts';
5050
foreach ($directories as $dir) {
5151
if ($filesystem->exists($composer = "$dir/composer.json")) {
52-
$sfPackages[json_decode(file_get_contents($composer))->name] = $dir;
52+
$sfPackages[json_decode($filesystem->readFile($composer), flags: JSON_THROW_ON_ERROR)->name] = $dir;
5353
}
5454
}
5555

src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private function getPublicDirectory(ContainerInterface $container): string
260260
return $defaultPublicDir;
261261
}
262262

263-
$composerConfig = json_decode(file_get_contents($composerFilePath), true);
263+
$composerConfig = json_decode($this->filesystem->readFile($composerFilePath), true, flags: \JSON_THROW_ON_ERROR);
264264

265265
return $composerConfig['extra']['public-dir'] ?? $defaultPublicDir;
266266
}

src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private function warmup(string $warmupDir, string $realBuildDir): void
232232
$search = [$warmupDir, str_replace('\\', '\\\\', $warmupDir)];
233233
$replace = str_replace('\\', '/', $realBuildDir);
234234
foreach (Finder::create()->files()->in($warmupDir) as $file) {
235-
$content = str_replace($search, $replace, file_get_contents($file), $count);
235+
$content = str_replace($search, $replace, $this->filesystem->readFile($file), $count);
236236
if ($count) {
237237
file_put_contents($file, $content);
238238
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
7272
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7373
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
74+
use Symfony\Component\Filesystem\Filesystem;
7475
use Symfony\Component\Finder\Finder;
7576
use Symfony\Component\Finder\Glob;
7677
use Symfony\Component\Form\Extension\HtmlSanitizer\Type\TextTypeHtmlSanitizerExtension;
@@ -3141,7 +3142,7 @@ private function getPublicDirectory(ContainerBuilder $container): string
31413142
}
31423143

31433144
$container->addResource(new FileResource($composerFilePath));
3144-
$composerConfig = json_decode(file_get_contents($composerFilePath), true);
3145+
$composerConfig = json_decode((new Filesystem())->readFile($composerFilePath), true, flags: \JSON_THROW_ON_ERROR);
31453146

31463147
return isset($composerConfig['extra']['public-dir']) ? $projectDir.'/'.$composerConfig['extra']['public-dir'] : $defaultPublicDir;
31473148
}

src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function () use ($file) {
7575
$kernelRef = new \ReflectionObject($this->kernel);
7676
$kernelFile = $kernelRef->getFileName();
7777
/** @var ResourceInterface[] $meta */
78-
$meta = unserialize(file_get_contents($containerMetaFile));
78+
$meta = unserialize($this->fs->readFile($containerMetaFile));
7979
$found = false;
8080
foreach ($meta as $resource) {
8181
if ((string) $resource === $kernelFile) {
@@ -93,7 +93,7 @@ function () use ($file) {
9393
);
9494
$this->assertMatchesRegularExpression(
9595
sprintf('/\'kernel.container_class\'\s*=>\s*\'%s\'/', $containerClass),
96-
file_get_contents($containerFile),
96+
$this->fs->readFile($containerFile),
9797
'kernel.container_class is properly set on the dumped container'
9898
);
9999
}

src/Symfony/Bundle/FrameworkBundle/Tests/Secrets/SodiumVaultTest.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@
2121
class SodiumVaultTest extends TestCase
2222
{
2323
private string $secretsDir;
24+
private Filesystem $filesystem;
2425

2526
protected function setUp(): void
2627
{
28+
$this->filesystem = new Filesystem();
2729
$this->secretsDir = sys_get_temp_dir().'/sf_secrets/test/';
28-
(new Filesystem())->remove($this->secretsDir);
30+
$this->filesystem->remove($this->secretsDir);
2931
}
3032

3133
protected function tearDown(): void
3234
{
33-
(new Filesystem())->remove($this->secretsDir);
35+
$this->filesystem->remove($this->secretsDir);
3436
}
3537

3638
public function testGenerateKeys()
@@ -41,8 +43,8 @@ public function testGenerateKeys()
4143
$this->assertFileExists($this->secretsDir.'/test.encrypt.public.php');
4244
$this->assertFileExists($this->secretsDir.'/test.decrypt.private.php');
4345

44-
$encKey = file_get_contents($this->secretsDir.'/test.encrypt.public.php');
45-
$decKey = file_get_contents($this->secretsDir.'/test.decrypt.private.php');
46+
$encKey = $this->filesystem->readFile($this->secretsDir.'/test.encrypt.public.php');
47+
$decKey = $this->filesystem->readFile($this->secretsDir.'/test.decrypt.private.php');
4648

4749
$this->assertFalse($vault->generateKeys());
4850
$this->assertStringEqualsFile($this->secretsDir.'/test.encrypt.public.php', $encKey);

src/Symfony/Bundle/FrameworkBundle/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"symfony/http-foundation": "^6.4|^7.0",
2929
"symfony/http-kernel": "^6.4|^7.0",
3030
"symfony/polyfill-mbstring": "~1.0",
31-
"symfony/filesystem": "^6.4|^7.0",
31+
"symfony/filesystem": "^7.1",
3232
"symfony/finder": "^6.4|^7.0",
3333
"symfony/routing": "^6.4|^7.0"
3434
},

src/Symfony/Component/AssetMapper/CompiledAssetMapperConfigReader.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@
1111

1212
namespace Symfony\Component\AssetMapper;
1313

14+
use Symfony\Component\Filesystem\Filesystem;
1415
use Symfony\Component\Filesystem\Path;
1516

1617
/**
1718
* Reads and writes compiled configuration files for asset mapper.
1819
*/
1920
class CompiledAssetMapperConfigReader
2021
{
21-
public function __construct(private readonly string $directory)
22-
{
22+
private readonly Filesystem $filesystem;
23+
24+
public function __construct(
25+
private readonly string $directory,
26+
) {
27+
$this->filesystem = new Filesystem();
2328
}
2429

2530
public function configExists(string $filename): bool
@@ -29,7 +34,7 @@ public function configExists(string $filename): bool
2934

3035
public function loadConfig(string $filename): array
3136
{
32-
return json_decode(file_get_contents(Path::join($this->directory, $filename)), true, 512, \JSON_THROW_ON_ERROR);
37+
return json_decode($this->filesystem->readFile(Path::join($this->directory, $filename)), true, 512, \JSON_THROW_ON_ERROR);
3338
}
3439

3540
public function saveConfig(string $filename, array $data): string

src/Symfony/Component/AssetMapper/Factory/CachedMappedAssetFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Config\Resource\FileExistenceResource;
1818
use Symfony\Component\Config\Resource\FileResource;
1919
use Symfony\Component\Config\Resource\ResourceInterface;
20+
use Symfony\Component\Filesystem\Filesystem;
2021

2122
/**
2223
* Decorates the asset factory to load MappedAssets from cache when possible.
@@ -36,7 +37,7 @@ public function createMappedAsset(string $logicalPath, string $sourcePath): ?Map
3637
$configCache = new ConfigCache($cachePath, $this->debug);
3738

3839
if ($configCache->isFresh()) {
39-
return unserialize(file_get_contents($cachePath));
40+
return unserialize((new Filesystem())->readFile($cachePath));
4041
}
4142

4243
$mappedAsset = $this->innerFactory->createMappedAsset($logicalPath, $sourcePath);

src/Symfony/Component/AssetMapper/Factory/MappedAssetFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\AssetMapper\Exception\RuntimeException;
1717
use Symfony\Component\AssetMapper\MappedAsset;
1818
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
19+
use Symfony\Component\Filesystem\Filesystem;
1920

2021
/**
2122
* Creates MappedAsset objects by reading their contents & passing it through compilers.
@@ -104,7 +105,7 @@ private function compileContent(MappedAsset $asset): ?string
104105
return null;
105106
}
106107

107-
$content = file_get_contents($asset->sourcePath);
108+
$content = (new Filesystem())->readFile($asset->sourcePath);
108109
$compiled = $this->compiler->compile($content, $asset);
109110

110111
return $compiled !== $content ? $compiled : null;

src/Symfony/Component/AssetMapper/Tests/Command/AssetMapperCompileCommandTest.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ public function testAssetsAreCompiled()
4848
$this->filesystem->remove($targetBuildDir);
4949
}
5050
// put old "built" versions to make sure the system skips using these
51-
$this->filesystem->mkdir($targetBuildDir);
52-
file_put_contents($targetBuildDir.'/manifest.json', '{}');
53-
file_put_contents($targetBuildDir.'/importmap.json', '{}');
54-
file_put_contents($targetBuildDir.'/entrypoint.file6.json', '[]');
51+
$this->filesystem->dumpFile($targetBuildDir.'/manifest.json', '{}');
52+
$this->filesystem->dumpFile($targetBuildDir.'/importmap.json', '{}');
53+
$this->filesystem->dumpFile($targetBuildDir.'/entrypoint.file6.json', '[]');
5554

5655
$command = $application->find('asset-map:compile');
5756
$tester = new CommandTester($command);
@@ -65,7 +64,7 @@ public function testAssetsAreCompiled()
6564
import '../file4.js';
6665
console.log('file5.js');
6766
68-
EOF, file_get_contents($targetBuildDir.'/subdir/file5-f4fdc37375c7f5f2629c5659a0579967.js'));
67+
EOF, $this->filesystem->readFile($targetBuildDir.'/subdir/file5-f4fdc37375c7f5f2629c5659a0579967.js'));
6968

7069
$finder = new Finder();
7170
$finder->in($targetBuildDir)->files();
@@ -83,10 +82,10 @@ public function testAssetsAreCompiled()
8382
'vendor/@hotwired/stimulus/stimulus.index.js',
8483
'vendor/lodash/lodash.index.js',
8584
'voilà.css',
86-
], array_keys(json_decode(file_get_contents($targetBuildDir.'/manifest.json'), true)));
85+
], array_keys(json_decode($this->filesystem->readFile($targetBuildDir.'/manifest.json'), true)));
8786

8887
$this->assertFileExists($targetBuildDir.'/importmap.json');
89-
$actualImportMap = json_decode(file_get_contents($targetBuildDir.'/importmap.json'), true);
88+
$actualImportMap = json_decode($this->filesystem->readFile($targetBuildDir.'/importmap.json'), true);
9089
$this->assertSame([
9190
'@hotwired/stimulus', // in importmap
9291
'lodash', // in importmap
@@ -102,7 +101,7 @@ public function testAssetsAreCompiled()
102101
$this->assertSame('js', $actualImportMap['@hotwired/stimulus']['type']);
103102

104103
$this->assertFileExists($targetBuildDir.'/entrypoint.file6.json');
105-
$entrypointData = json_decode(file_get_contents($targetBuildDir.'/entrypoint.file6.json'), true);
104+
$entrypointData = json_decode($this->filesystem->readFile($targetBuildDir.'/entrypoint.file6.json'), true);
106105
$this->assertSame([
107106
'/assets/subdir/file5.js',
108107
'/assets/file4.js',

src/Symfony/Component/AssetMapper/Tests/CompiledAssetMapperConfigReaderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testSaveConfig()
5555
{
5656
$reader = new CompiledAssetMapperConfigReader($this->writableRoot);
5757
$this->assertEquals($this->writableRoot.\DIRECTORY_SEPARATOR.'foo.json', realpath($reader->saveConfig('foo.json', ['foo' => 'bar'])));
58-
$this->assertEquals(['foo' => 'bar'], json_decode(file_get_contents($this->writableRoot.'/foo.json'), true));
58+
$this->assertEquals(['foo' => 'bar'], json_decode($this->filesystem->readFile($this->writableRoot.'/foo.json'), true));
5959
}
6060

6161
public function testRemoveConfig()

src/Symfony/Component/AssetMapper/Tests/Factory/CachedMappedAssetFactoryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private function loadConfigCacheMetadataFor(MappedAsset $mappedAsset): array
131131
{
132132
$cachedPath = $this->getConfigCachePath($mappedAsset).'.meta';
133133

134-
return unserialize(file_get_contents($cachedPath));
134+
return unserialize($this->filesystem->readFile($cachedPath));
135135
}
136136

137137
private function saveConfigCache(MappedAsset $mappedAsset): void

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ public function testDownloadPackagesDownloadsEverythingWithNoInstalled()
7979
$this->assertFileExists(self::$writableRoot.'/assets/vendor/foo/foo.index.js');
8080
$this->assertFileExists(self::$writableRoot.'/assets/vendor/bar.js/file.js');
8181
$this->assertFileExists(self::$writableRoot.'/assets/vendor/baz/baz.index.css');
82-
$this->assertEquals('foo content', file_get_contents(self::$writableRoot.'/assets/vendor/foo/foo.index.js'));
82+
$this->assertEquals('foo content', $this->filesystem->readFile(self::$writableRoot.'/assets/vendor/foo/foo.index.js'));
8383
$this->assertFileExists(self::$writableRoot.'/assets/vendor/foo/path/to/extra-file.woff');
84-
$this->assertEquals('extra file contents', file_get_contents(self::$writableRoot.'/assets/vendor/foo/path/to/extra-file.woff'));
85-
$this->assertEquals('bar content', file_get_contents(self::$writableRoot.'/assets/vendor/bar.js/file.js'));
86-
$this->assertEquals('baz content', file_get_contents(self::$writableRoot.'/assets/vendor/baz/baz.index.css'));
87-
$this->assertEquals('different content', file_get_contents(self::$writableRoot.'/assets/vendor/custom_specifier/custom_specifier.index.js'));
84+
$this->assertEquals('extra file contents', $this->filesystem->readFile(self::$writableRoot.'/assets/vendor/foo/path/to/extra-file.woff'));
85+
$this->assertEquals('bar content', $this->filesystem->readFile(self::$writableRoot.'/assets/vendor/bar.js/file.js'));
86+
$this->assertEquals('baz content', $this->filesystem->readFile(self::$writableRoot.'/assets/vendor/baz/baz.index.css'));
87+
$this->assertEquals('different content', $this->filesystem->readFile(self::$writableRoot.'/assets/vendor/custom_specifier/custom_specifier.index.js'));
8888

8989
$installed = require self::$writableRoot.'/assets/vendor/installed.php';
9090
$this->assertEquals(
@@ -150,9 +150,9 @@ public function testPackagesWithCorrectInstalledVersionSkipped()
150150
$this->assertFileExists(self::$writableRoot.'/assets/vendor/foo/foo.index.js');
151151
$this->assertFileExists(self::$writableRoot.'/assets/vendor/bar.js/file.js');
152152
$this->assertFileExists(self::$writableRoot.'/assets/vendor/baz/baz.index.css');
153-
$this->assertEquals('original foo content', file_get_contents(self::$writableRoot.'/assets/vendor/foo/foo.index.js'));
154-
$this->assertEquals('new bar content', file_get_contents(self::$writableRoot.'/assets/vendor/bar.js/file.js'));
155-
$this->assertEquals('new baz content', file_get_contents(self::$writableRoot.'/assets/vendor/baz/baz.index.css'));
153+
$this->assertEquals('original foo content', $this->filesystem->readFile(self::$writableRoot.'/assets/vendor/foo/foo.index.js'));
154+
$this->assertEquals('new bar content', $this->filesystem->readFile(self::$writableRoot.'/assets/vendor/bar.js/file.js'));
155+
$this->assertEquals('new baz content', $this->filesystem->readFile(self::$writableRoot.'/assets/vendor/baz/baz.index.css'));
156156
$this->assertFileExists(self::$writableRoot.'/assets/vendor/has-missing-extra/has-missing-extra.index.js');
157157

158158
$installed = require self::$writableRoot.'/assets/vendor/installed.php';

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testSave()
7070
$storage->save($entry, 'any content');
7171
$targetPath = self::$writableRoot.'/assets/vendor/module_specifier/module_specifier.index.js';
7272
$this->assertFileExists($targetPath);
73-
$this->assertEquals('any content', file_get_contents($targetPath));
73+
$this->assertEquals('any content', $this->filesystem->readFile($targetPath));
7474
}
7575

7676
public function testSaveExtraFile()
@@ -80,7 +80,7 @@ public function testSaveExtraFile()
8080
$storage->saveExtraFile($entry, '/path/to/extra-file.woff2', 'any content');
8181
$targetPath = self::$writableRoot.'/assets/vendor/module_specifier/path/to/extra-file.woff2';
8282
$this->assertFileExists($targetPath);
83-
$this->assertEquals('any content', file_get_contents($targetPath));
83+
$this->assertEquals('any content', $this->filesystem->readFile($targetPath));
8484
}
8585

8686
/**

src/Symfony/Component/AssetMapper/Tests/Path/LocalPublicAssetsFilesystemTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testWrite()
3838
$filesystem = new LocalPublicAssetsFilesystem(self::$writableRoot);
3939
$filesystem->write('foo/bar.js', 'foobar');
4040
$this->assertFileExists(self::$writableRoot.'/foo/bar.js');
41-
$this->assertSame('foobar', file_get_contents(self::$writableRoot.'/foo/bar.js'));
41+
$this->assertSame('foobar', $this->filesystem->readFile(self::$writableRoot.'/foo/bar.js'));
4242

4343
// with a directory
4444
$filesystem->write('foo/baz/bar.js', 'foobar');
@@ -50,6 +50,6 @@ public function testCopy()
5050
$filesystem = new LocalPublicAssetsFilesystem(self::$writableRoot);
5151
$filesystem->copy(__DIR__.'/../Fixtures/importmaps/assets/pizza/index.js', 'foo/bar.js');
5252
$this->assertFileExists(self::$writableRoot.'/foo/bar.js');
53-
$this->assertSame("console.log('pizza/index.js');", trim(file_get_contents(self::$writableRoot.'/foo/bar.js')));
53+
$this->assertSame("console.log('pizza/index.js');", trim($this->filesystem->readFile(self::$writableRoot.'/foo/bar.js')));
5454
}
5555
}

src/Symfony/Component/AssetMapper/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=8.2",
2020
"composer/semver": "^3.0",
2121
"symfony/deprecation-contracts": "^2.1|^3",
22-
"symfony/filesystem": "^6.4|^7.0",
22+
"symfony/filesystem": "^7.1",
2323
"symfony/http-client": "^6.4|^7.0"
2424
},
2525
"require-dev": {

src/Symfony/Component/Config/ResourceCheckerConfigCache.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function write(string $content, ?array $metadata = null): void
134134
private function safelyUnserialize(string $file): mixed
135135
{
136136
$meta = false;
137-
$content = file_get_contents($file);
137+
$content = (new Filesystem())->readFile($file);
138138
$signalingException = new \UnexpectedValueException();
139139
$prevUnserializeHandler = ini_set('unserialize_callback_func', self::class.'::handleUnserializeCallback');
140140
$prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler, $signalingException) {

src/Symfony/Component/Config/Util/XmlUtils.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\Util\Exception\InvalidXmlException;
1515
use Symfony\Component\Config\Util\Exception\XmlParsingException;
16+
use Symfony\Component\Filesystem\Filesystem;
1617

1718
/**
1819
* XMLUtils is a bunch of utility methods to XML operations.
@@ -79,7 +80,7 @@ public static function parse(string $content, string|callable|null $schemaOrCall
7980
$valid = false;
8081
}
8182
} elseif (is_file($schemaOrCallable)) {
82-
$schemaSource = file_get_contents((string) $schemaOrCallable);
83+
$schemaSource = (new Filesystem())->readFile((string) $schemaOrCallable);
8384
$valid = @$dom->schemaValidateSource($schemaSource);
8485
} else {
8586
libxml_use_internal_errors($internalErrors);
@@ -122,7 +123,7 @@ public static function loadFile(string $file, string|callable|null $schemaOrCall
122123
throw new \InvalidArgumentException(sprintf('File "%s" is not readable.', $file));
123124
}
124125

125-
$content = @file_get_contents($file);
126+
$content = (new Filesystem())->readFile($file);
126127

127128
if ('' === trim($content)) {
128129
throw new \InvalidArgumentException(sprintf('File "%s" does not contain valid XML, it is empty.', $file));

src/Symfony/Component/Config/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": ">=8.2",
2020
"symfony/deprecation-contracts": "^2.5|^3",
21-
"symfony/filesystem": "^6.4|^7.0",
21+
"symfony/filesystem": "^7.1",
2222
"symfony/polyfill-ctype": "~1.8"
2323
},
2424
"require-dev": {

0 commit comments

Comments
 (0)