Skip to content

[DependencyInjection] Added @@ escaping strategy for YamlFileLoader and YamlDumper #7357

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -260,7 +260,7 @@ private function prepareParameters($parameters, $escape = true)
foreach ($parameters as $key => $value) {
if (is_array($value)) {
$value = $this->prepareParameters($value, $escape);
} elseif ($value instanceof Reference) {
} elseif ($value instanceof Reference || is_string($value) && 0 === strpos($value, '@')) {
$value = '@'.$value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ private function resolveServices($value)
if (is_array($value)) {
$value = array_map(array($this, 'resolveServices'), $value);
} elseif (is_string($value) && 0 === strpos($value, '@')) {
if (0 === strpos($value, '@?')) {
if (0 === strpos($value, '@@')) {
$value = substr($value, 1);
$invalidBehavior = null;
} elseif (0 === strpos($value, '@?')) {
$value = substr($value, 2);
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
} else {
Expand All @@ -305,7 +308,9 @@ private function resolveServices($value)
$strict = true;
}

$value = new Reference($value, $invalidBehavior, $strict);
if (null !== $invalidBehavior) {
$value = new Reference($value, $invalidBehavior, $strict);
}
}

return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'FOO' => '%baz%',
'baz' => 'bar',
'bar' => 'foo is %%foo bar',
'escape' => '@escapeme',
'values' => array(true, false, null, 0, 1000.3, 'true', 'false', 'null'),
)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function getDefaultParameters()
'foo' => '%baz%',
'baz' => 'bar',
'bar' => 'foo is %%foo bar',
'escape' => '@escapeme',
'values' => array(
0 => true,
1 => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<parameter key="foo">%baz%</parameter>
<parameter key="baz">bar</parameter>
<parameter key="bar">foo is %%foo bar</parameter>
<parameter key="escape">@escapeme</parameter>
<parameter key="values" type="collection">
<parameter>true</parameter>
<parameter>false</parameter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ parameters:
- 0
- 1000.3
bar: foo
escape: @@escapeme
foo_bar: @foo_bar
MixedCase:
MixedCaseKey: value
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ parameters:
foo: '%baz%'
baz: bar
bar: 'foo is %%foo bar'
escape: '@@escapeme'
values: [true, false, null, 0, 1000.3, 'true', 'false', 'null']

Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testLoadParameters()
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services2.yml');
$this->assertEquals(array('foo' => 'bar', 'mixedcase' => array('MixedCaseKey' => 'value'), 'values' => array(true, false, 0, 1000.3), 'bar' => 'foo', 'foo_bar' => new Reference('foo_bar')), $container->getParameterBag()->all(), '->load() converts YAML keys to lowercase');
$this->assertEquals(array('foo' => 'bar', 'mixedcase' => array('MixedCaseKey' => 'value'), 'values' => array(true, false, 0, 1000.3), 'bar' => 'foo', 'escape' => '@escapeme', 'foo_bar' => new Reference('foo_bar')), $container->getParameterBag()->all(), '->load() converts YAML keys to lowercase');
}

public function testLoadImports()
Expand All @@ -99,7 +99,7 @@ public function testLoadImports()
$loader->load('services4.yml');

$actual = $container->getParameterBag()->all();
$expected = array('foo' => 'bar', 'values' => array(true, false), 'bar' => '%foo%', 'foo_bar' => new Reference('foo_bar'), 'mixedcase' => array('MixedCaseKey' => 'value'), 'imported_from_ini' => true, 'imported_from_xml' => true);
$expected = array('foo' => 'bar', 'values' => array(true, false), 'bar' => '%foo%', 'escape' => '@escapeme', 'foo_bar' => new Reference('foo_bar'), 'mixedcase' => array('MixedCaseKey' => 'value'), 'imported_from_ini' => true, 'imported_from_xml' => true);
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');

// Bad import throws no exception due to ignore_errors value.
Expand Down