8000 feature #49412 [DependencyInjection] Allow trimming service parameter… · symfony/symfony@84658da · GitHub
[go: up one dir, main page]

Skip to content

Commit 84658da

Browse files
feature #49412 [DependencyInjection] Allow trimming service parameters value in XML configuration files (alexandre-daubois)
This PR was merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Allow trimming service parameters value in XML configuration files | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | _NA_ | License | MIT | Doc PR | Todo While coming across [this documentation page](https://symfony.com/doc/current/configuration.html#configuration-parameters), I learnt that strings as service parameters value aren't trimmed when using the XML multi-line style to define the value. I think adding the possibility of trimming the value if wanted could be a plus, allowing more flexibility when writing XML files. Also, when using the `binary` parameter type, `base64_decode` actually trims the string and allows to use the multi-line syntax. So we could see an added consistency here by allowing this with string parameters. 🙂 Commits ------- bdc4c10 [DependencyInjection] Allow trimming service parameters value in XML configuration files
2 parents b7cb458 + bdc4c10 commit 84658da

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Fail if Target attribute does not exist during compilation
1313
* Enable deprecating parameters with `ContainerBuilder::deprecateParameter()`
1414
* Add `#[AsAlias]` attribute to tell under which alias a service should be registered or to use the implemented interface if no parameter is given
15+
* Allow to trim XML service parameters value by using `trim="true"` attribute
1516

1617
6.2
1718
---

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
478478
$key = $arg->getAttribute('key');
479479
}
480480

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

553-
$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);
554+
$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')));
554555

555556
if ($forLocator) {
556557
$arguments[$key] = new ServiceLocatorArgument($arguments[$key]);
@@ -566,13 +567,13 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
566567
$arguments[$key] = new AbstractArgument($arg->nodeValue);
567568
break;
568569
case 'string':
569-
$arguments[$key] = $arg->nodeValue;
570+
$arguments[$key] = $trim ? trim($arg->nodeValue) : $arg->nodeValue;
570571
break;
571572
case 'constant':
572573
$arguments[$key] = \constant(trim($arg->nodeValue));
573574
break;
574575
default:
575-
$arguments[$key] = XmlUtils::phpize($arg->nodeValue);
576+
$arguments[$key] = XmlUtils::phpize($trim ? trim($arg->nodeValue) : $arg->nodeValue);
576577
}
577578
}
578579

‎< 10000 !-- -->src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
<xsd:attribute name="id" type="xsd:string" />
259259
<xsd:attribute name="key" type="xsd:string" />
260260
<xsd:attribute name="on-invalid" type="invalid_sequence" />
261+
<xsd:attribute name="trim" type="xsd:boolean" />
261262
</xsd:complexType>
262263

263264
<xsd:complexType name="property" mixed="true">

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services2.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
<parameter key="float">1.3</parameter>
1919
<parameter>1000.3</parameter>
2020
<parameter>a string</parameter>
21+
<parameter trim="false"> a string not trimmed </parameter>
22+
<parameter trim="true">
23+
a trimmed string
24+
</parameter>
25+
<parameter type="string" trim="true">
26+
an explicit trimmed string
27+
</parameter>
2128
<parameter type="collection">
2229
<parameter>foo</parameter>
2330
<parameter>bar</parameter>

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public function testLoadParameters()
140140
'float' => 1.3,
141141
1000.3,
142142
'a string',
143+
' a string not trimmed ',
144+
'a trimmed string',
145+
'an explicit trimmed string',
143146
['foo', 'bar'],
144147
],
145148
'mixedcase' => ['MixedCaseKey' => 'value'],

0 commit comments

Comments
 (0)
0