10000 Merge branch '6.2' into 6.3 · symfony/symfony@875a729 · GitHub
[go: up one dir, main page]

Skip to content

Commit 875a729

Browse files
Merge branch '6.2' into 6.3
* 6.2: [Filesystem] Follow symlinks when dumping files [DependencyInjection] Escape `%` from parameter-like default values
2 parents dec93e9 + 9a3b651 commit 875a729

File tree

5 files changed

+98
-8
lines changed

5 files changed

+98
-8
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public function __construct(bool $throwOnAutowireException = true)
5555
$this->defaultArgument = new class() {
5656
public $value;
5757
public $names;
58+
public $bag;
59+
60+
public function withValue(\ReflectionParameter $parameter): self
61+
{
62+
$clone = clone $this;
63+
$clone->value = $this->bag->escapeValue($parameter->getDefaultValue());
64+
65+
return $clone;
66+
}
5867
};
5968
}
6069

@@ -63,13 +72,16 @@ public function __construct(bool $throwOnAutowireException = true)
6372
*/
6473
public function process(ContainerBuilder $container)
6574
{
75+
$this->defaultArgument->bag = $container->getParameterBag();
76+
6677
try {
6778
$this->typesClone = clone $this;
6879
parent::process($container);
6980
} finally {
7081
$this->decoratedClass = null;
7182
$this->decoratedId = null;
7283
$this->methodCalls = null;
84+
$this->defaultArgument->bag = null;
7385
$this->defaultArgument->names = null;
7486
$this->getPreviousValue = null;
7587
$this->decoratedMethodIndex = null;
@@ -275,8 +287,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
275287
$failureMessage = $this->createTypeNotFoundMessageCallback($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
276288

277289
if ($parameter->isDefaultValueAvailable()) {
278-
$value = clone $this->defaultArgument;
279-
$value->value = $parameter->getDefaultValue();
290+
$value = $this->defaultArgument->withValue($parameter);
280291
} elseif (!$parameter->allowsNull()) {
281292
throw new AutowiringFailedException($this->currentId, $failureMessage);
282293
}
@@ -374,8 +385,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
374385
}
375386

376387
// specifically pass the default value
377-
$arguments[$index] = clone $this->defaultArgument;
378-
$arguments[$index]->value = $parameter->getDefaultValue();
388+
$arguments[$index] = $this->defaultArgument->withValue($parameter);
379389

380390
continue;
381391
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,19 @@ public function testAutowireWithNamedArgs()
12841284
$this->assertEquals([new TypedReference(A::class, A::class), 'abc'], $container->getDefinition('foo')->getArguments());
12851285
}
12861286

1287+
public function testAutowireDefaultValueParametersLike()
1288+
{
1289+
$container = new ContainerBuilder();
1290+
1291+
$container->register('foo', ParametersLikeDefaultValue::class)
1292+
->setAutowired(true)
1293+
->setArgument(1, 'ok');
1294+
1295+
(new AutowirePass())->process($container);
1296+
1297+
$this->assertSame('%%not%%one%%parameter%%here%%', $container->getDefinition('foo')->getArgument(0));
1298+
}
1299+
12871300
public function testAutowireAttribute()
12881301
{
12891302
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,13 @@ public function __construct(NotExisting $notExisting)
528528
}
529529
}
530530

531+
class ParametersLikeDefaultValue
532+
{
533+
public function __construct(string $parameterLike = '%not%one%parameter%here%', string $willBeSetToKeepFirstArgumentDefaultValue = 'ok')
534+
{
535+
}
536+
}
537+
531538
class StaticConstructor
532539
{
533540
public function __construct(private string $bar)

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,12 @@ public function dumpFile(string $filename, $content)
664664

665665
$dir = \dirname($filename);
666666

667+
if (is_link($filename) && $linkTarget = $this->readlink($filename)) {
668+
$this->dumpFile(Path::makeAbsolute($linkTarget, $dir), $content);
669+
670+
return;
671+
}
672+
667673
if (!is_dir($dir)) {
668674
$this->mkdir($dir);
669675
}

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,14 +1087,12 @@ public function testReadBrokenLink()
10871087
{
10881088
$this->markAsSkippedIfSymlinkIsMissing();
10891089

1090-
if ('\\' === \DIRECTORY_SEPARATOR) {
1091-
$this->markTestSkipped('Windows does not support creating "broken" symlinks');
1092-
}
1093-
10941090
$file = $this->workspace.'/file';
10951091
$link = $this->workspace.'/link';
10961092

1093+
touch($file);
10971094
$this->filesystem->symlink($file, $link);
1095+
$this->filesystem->remove($file);
10981096

10991097
$this->assertEquals($file, $this->filesystem->readlink($link));
11001098
$this->assertNull($this->filesystem->readlink($link, true));
@@ -1601,6 +1599,33 @@ public function testDumpFileOverwritesAnExistingFile()
16011599
$this->assertStringEqualsFile($filename, 'bar');
16021600
}
16031601

1602+
public function testDumpFileFollowsSymlink()
1603+
{
1604+
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo.txt';
1605+
file_put_contents($filename, 'FOO BAR');
1606+
$linknameA = $this->workspace.\DIRECTORY_SEPARATOR.'bar.txt';
1607+
$linknameB = $this->workspace.\DIRECTORY_SEPARATOR.'baz.txt';
1608+
$this->filesystem->symlink($filename, $linknameA);
1609+
$this->filesystem->symlink($linknameA, $linknameB);
1610+
1611+
$this->filesystem->dumpFile($linknameB, 'bar');
1612+
1613+
$this->assertFileExists($filename);
1614+
$this->assertFileExists($linknameA);
1615+
$this->assertFileExists($linknameB);
1616+
$this->assertStringEqualsFile($filename, 'bar');
1617+
$this->assertStringEqualsFile($linknameA, 'bar');
1618+
$this->assertStringEqualsFile($linknameB, 'bar');
1619+
1620+
$this->filesystem->remove($filename);
1621+
$this->filesystem->dumpFile($linknameB, 'baz');
1622+
1623+
$this->assertFileExists($filename);
1624+
$this->assertStringEqualsFile($filename, 'baz');
1625+
$this->assertStringEqualsFile($linknameA, 'baz');
1626+
$this->assertStringEqualsFile($linknameB, 'baz');
1627+
}
1628+
16041629
public function testDumpFileWithFileScheme()
16051630
{
16061631
$scheme = 'file://';
@@ -1674,6 +1699,35 @@ public function testAppendToFileWithResource()
16741699
}
16751700
}
16761701

1702+
public function testAppendToFileFollowsSymlink()
1703+
{
1704+
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo.txt';
1705+
file_put_contents($filename, 'foo');
1706+
$linknameA = $this->workspace.\DIRECTORY_SEPARATOR.'bar.txt';
1707+
$linknameB = $this->workspace.\DIRECTORY_SEPARATOR.'baz.txt';
1708+
$this->filesystem->symlink($filename, $linknameA);
1709+
$this->filesystem->symlink($linknameA, $linknameB);
1710+
1711+
$this->filesystem->appendToFile($linknameA, 'bar');
1712+
$this->filesystem->appendToFile($linknameB, 'baz');
1713+
1714+
$this->assertFileExists($filename);
1715+
$this->assertFileExists($linknameA);
1716+
$this->assertFileExists($linknameB);
1717+
$this->assertStringEqualsFile($filename, 'foobarbaz');
1718+
$this->assertStringEqualsFile($linknameA, 'foobarbaz');
1719+
$this->assertStringEqualsFile($linknameB, 'foobarbaz');
1720+
1721+
$this->filesystem->remove($filename);
1722+
$this->filesystem->appendToFile($linknameB, 'foo');
1723+
$this->filesystem->appendToFile($linknameA, 'bar');
1724+
1725+
$this->assertFileExists($filename);
1726+
$this->assertStringEqualsFile($filename, 'foobar');
1727+
$this->assertStringEqualsFile($linknameA, 'foobar');
1728+
$this->assertStringEqualsFile($linknameB, 'foobar');
1729+
}
1730+
16771731
public function testAppendToFileWithScheme()
16781732
{
16791733
$scheme = 'file://';

0 commit comments

Comments
 (0)
0