8000 Using Path class · symfony/symfony@5895dc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5895dc7

Browse files
committed
Using Path class
1 parent 868ab99 commit 5895dc7

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/Symfony/Component/AssetMapper/CompiledAssetMapperConfigReader.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\AssetMapper;
1313

14+
use Symfony\Component\Filesystem\Path;
15+
1416
/**
1517
* Reads and writes compiled configuration files for asset mapper.
1618
*/
@@ -22,17 +24,17 @@ public function __construct(private readonly string $directory)
2224

2325
public function configExists(string $filename): bool
2426
{
25-
return is_file($this->directory.'/'.$filename);
27+
return is_file(Path::join($this->directory, $filename));
2628
}
2729

2830
public function loadConfig(string $filename): array
2931
{
30-
return json_decode(file_get_contents($this->directory.'/'.$filename), true, 512, \JSON_THROW_ON_ERROR);
32+
return json_decode(file_get_contents(Path::join($this->directory, $filename)), true, 512, \JSON_THROW_ON_ERROR);
3133
}
3234

3335
public function saveConfig(string $filename, array $data): string
3436
{
35-
$path = $this->directory.'/'.$filename;
37+
$path = Path::join($this->directory, $filename);
3638
@mkdir(\dirname($path), 0777, true);
3739
file_put_contents($path, json_encode($data, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR));
3840

@@ -41,7 +43,7 @@ public function saveConfig(string $filename, array $data): string
4143

4244
public function removeConfig(string $filename): void
4345
{
44-
$path = $this->directory.'/'.$filename;
46+
$path = Path::join($this->directory, $filename);
4547

4648
if (is_file($path)) {
4749
unlink($path);

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,50 @@
1818
class CompiledAssetMapperConfigReaderTest extends TestCase
1919
{
2020
private Filesystem $filesystem;
21-
private static string $writableRoot = __DIR__.'/../fixtures/importmaps_for_writing';
21+
private string $writableRoot;
2222

2323
protected function setUp(): void
2424
{
2525
$this->filesystem = new Filesystem();
26+
$this->writableRoot = __DIR__.'/../fixtures/importmaps_for_writing';
2627
if (!file_exists(__DIR__.'/../fixtures/importmaps_for_writing')) {
27-
$this->filesystem->mkdir(self::$writableRoot);
28+
$this->filesystem->mkdir($this->writableRoot);
2829
}
30+
// realpath to help path comparisons in the tests
31+
$this->writableRoot = realpath($this->writableRoot);
2932
}
3033

3134
protected function tearDown(): void
3235
{
33-
$this->filesystem->remove(self::$writableRoot);
36+
$this->filesystem->remove($this->writableRoot);
3437
}
3538

3639
public function testConfigExists()
3740
{
38-
$reader = new CompiledAssetMapperConfigReader(self::$writableRoot);
41+
$reader = new CompiledAssetMapperConfigReader($this->writableRoot);
3942
$this->assertFalse($reader->configExists('foo.json'));
40-
$this->filesystem->touch(self::$writableRoot.'/foo.json');
43+
$this->filesystem->touch($this->writableRoot.'/foo.json');
4144
$this->assertTrue($reader->configExists('foo.json'));
4245
}
4346

4447
public function testLoadConfig()
4548
{
46-
$reader = new CompiledAssetMapperConfigReader(self::$writableRoot);
47-
$this->filesystem->dumpFile(self::$writableRoot.'/foo.json', '{"foo": "bar"}');
49+
$reader = new CompiledAssetMapperConfigReader($this->writableRoot);
50+
$this->filesystem->dumpFile($this->writableRoot.'/foo.json', '{"foo": "bar"}');
4851
$this->assertEquals(['foo' => 'bar'], $reader->loadConfig('foo.json'));
4952
}
5053

5154
public function testSaveConfig()
5255
{
53-
$reader = new CompiledAssetMapperConfigReader(self::$writableRoot);
54-
$this->assertEquals(self::$writableRoot.'/foo.json', $reader->saveConfig('foo.json', ['foo' => 'bar']));
55-
$this->assertEquals(['foo' => 'bar'], json_decode(file_get_contents(self::$writableRoot.'/foo.json'), true));
56+
$reader = new CompiledAssetMapperConfigReader($this->writableRoot);
57+
$this->assertEquals($this->writableRoot.'/foo.json', $reader->saveConfig('foo.json', ['foo' => 'bar']));
58+
$this->assertEquals(['foo' => 'bar'], json_decode(file_get_contents($this->writableRoot.'/foo.json'), true));
5659
}
5760

5861
public function testRemoveConfig()
5962
{
60-
$reader = new CompiledAssetMapperConfigReader(self::$writableRoot);
61-
$this->filesystem->touch(self::$writableRoot.'/foo.json');
63+
$reader = new CompiledAssetMapperConfigReader($this->writableRoot);
64+
$this->filesystem->touch($this->writableRoot.'/foo.json');
6265
$this->assertTrue($reader->configExists('foo.json'));
6366
$reader->removeConfig('foo.json');
6467
$this->assertFalse($reader->configExists('foo.json'));

0 commit comments

Comments
 (0)
0