Skip to content

Commit

Permalink
[DependencyInjection] Added '@@' escaping strategy for YamlFileLoader…
Browse files Browse the repository at this point in the history
… and YamlDumper

Added the possibility to to use '@@' as an escaping strategy for
parameters that should be treated as strings but start with '@'
(i.e. safe mailer passwords).
  • Loading branch information
Thomas Ploch committed Mar 13, 2013
1 parent 2453a58 commit 8cdf387
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 5 deletions.
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
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
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
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
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
Expand Up @@ -6,6 +6,7 @@ parameters:
- 0
- 1000.3
bar: foo
escape: @@escapeme
foo_bar: @foo_bar
MixedCase:
MixedCaseKey: value
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']

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

0 comments on commit 8cdf387

Please sign in to comment.