8000 Merge branch '5.4' into 6.2 · symfony/symfony@9a3b651 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a3b651

Browse files
Merge branch '5.4' into 6.2
* 5.4: [Filesystem] Follow symlinks when dumping files [DependencyInjection] Escape `%` from parameter-like default values
2 parents 380e709 + 33357e6 commit 9a3b651

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
@@ -58,18 +58,30 @@ public function __construct(bool $throwOnAutowireException = true)
5858
$this->defaultArgument = new class() {
5959
public $value;
6060
public $names;
61+
public $bag;
62+
63+
public function withValue(\ReflectionParameter $parameter): self
64+
{
65+
$clone = clone $this;
66+
$clone->value = $this->bag->escapeValue($parameter->getDefaultValue());
67+
68+
return $clone;
69+
}
6170
};
6271
}
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;
@@ -315,8 +327,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
315327
}
316328

317329
// specifically pass the default value
318-
$arguments[$index] = clone $this->defaultArgument;
319-
$arguments[$index]->value = $parameter->getDefaultValue();
330+
$arguments[$index] = $this->defaultArgument->withValue($parameter);
320331

321332
continue;
322333
}
@@ -326,8 +337,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
326337
$failureMessage = $this->createTypeNotFoundMessageCallback($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
327338

328339
if ($parameter->isDefaultValueAvailable()) {
329-
$value = clone $this->defaultArgument;
330-
$value->value = $parameter->getDefaultValue();
340+
$value = $this->defaultArgument->withValue($parameter);
331341
} elseif (!$parameter->allowsNull()) {
332342
throw new AutowiringFailedException($this->currentId, $failureMessage);
333343
}

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

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

1177+
public function testAutowireDefaultValueParametersLike()
1178+
{
1179+
$container = new ContainerBuilder();
1180+
1181+
$container->register('foo', ParametersLikeDefaultValue::class)
1182+
->setAutowired(true)
1183+
->setArgument(1, 'ok');
1184+
1185+
(new AutowirePass())->process($container);
1186+
1187+
$this->assertSame('%%not%%one%%parameter%%here%%', $container->getDefinition('foo')->getArgument(0));
1188+
}
1189+
11771190
public function testAutowireAttribute()
11781191
{
11791192
$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
@@ -422,3 +422,10 @@ public function __construct(NotExisting $notExisting)
422422
{
423423
}
424424
}
425+
426+
class ParametersLikeDefaultValue
427+
{
428+
public function __construct(string $parameterLike = '%not%one%parameter%here%', string $willBeSetToKeepFirstArgumentDefaultValue = 'ok')
429+
{
430+
}
431+
}

src/Symfony/Component/Filesystem/Filesystem.php

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

643643
$dir = \dirname($filename);
644644

645+
if (is_link($filename) && $linkTarget = $this->readlink($filename)) {
646+
$this->dumpFile(Path::makeAbsolute($linkTarget, $dir), $content);
647+
648+
return;
649+
}
650+
645651
if (!is_dir($dir)) {
646652
$this->mkdir($dir);
647653
}

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