Skip to content

[DependencyInjection] Allow trimming service parameters value in XML configuration files #49412

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

Merged
merged 1 commit into from
Feb 21, 2023
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
* Fail if Target attribute does not exist during compilation
* Enable deprecating parameters with `ContainerBuilder::deprecateParameter()`
* Add `#[AsAlias]` attribute to tell under which alias a service should be registered or to use the implemented interface if no parameter is given
* Allow to trim XML service parameters value by using `trim="true"` attribute

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
$key = $arg->getAttribute('key');
}

$trim = $arg->hasAttribute('trim') && XmlUtils::phpize($arg->getAttribute('trim'));
$onInvalid = $arg->getAttribute('on-invalid');
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
if ('ignore' == $onInvalid) {
Expand Down Expand Up @@ -550,7 +551,7 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
$excludes = [$arg->getAttribute('exclude')];
}

$arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'), $arg->getAttribute('index-by') ?: null, $arg->getAttribute('default-index-method') ?: null, $forLocator, $arg->getAttribute('default-priority-method') ?: null, $excludes, $arg->getAttribute('exclude-self') ?: true);
$arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'), $arg->getAttribute('index-by') ?: null, $arg->getAttribute('default-index-method') ?: null, $forLocator, $arg->getAttribute('default-priority-method') ?: null, $excludes, !$arg->hasAttribute('exclude-self') || XmlUtils::phpize($arg->getAttribute('exclude-self')));

if ($forLocator) {
$arguments[$key] = new ServiceLocatorArgument($arguments[$key]);
Expand All @@ -566,13 +567,13 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
$arguments[$key] = new AbstractArgument($arg->nodeValue);
break;
case 'string':
$arguments[$key] = $arg->nodeValue;
$arguments[$key] = $trim ? trim($arg->nodeValue) : $arg->nodeValue;
break;
case 'constant':
$arguments[$key] = \constant(trim($arg->nodeValue));
break;
default:
$arguments[$key] = XmlUtils::phpize($arg->nodeValue);
$arguments[$key] = XmlUtils::phpize($trim ? trim($arg->nodeValue) : $arg->nodeValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="key" type="xsd:string" />
<xsd:attribute name="on-invalid" type="invalid_sequence" />
<xsd:attribute name="trim" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="property" mixed="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
<parameter key="float">1.3</parameter>
<parameter>1000.3</parameter>
<parameter>a string</parameter>
<parameter trim="false"> a string not trimmed </parameter>
<parameter trim="true">
a trimmed string
</parameter>
<parameter type="string" trim="true">
an explicit trimmed string
</parameter>
<parameter type="collection">
<parameter>foo</parameter>
<parameter>bar</parameter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public function testLoadParameters()
'float' => 1.3,
1000.3,
'a string',
' a string not trimmed ',
'a trimmed string',
'an explicit trimmed string',
['foo', 'bar'],
],
'mixedcase' => ['MixedCaseKey' => 'value'],
Expand Down