[DI] Allow binary values in parameters.

This commit is contained in:
Benoît Burnichon 2018-01-25 14:23:25 +01:00 committed by Fabien Potencier
parent a8dc953279
commit cb2313422b
7 changed files with 19 additions and 0 deletions

View File

@ -298,6 +298,10 @@ class XmlDumper extends Dumper
$element->setAttribute('type', 'expression'); $element->setAttribute('type', 'expression');
$text = $this->document->createTextNode(self::phpToXml((string) $value)); $text = $this->document->createTextNode(self::phpToXml((string) $value));
$element->appendChild($text); $element->appendChild($text);
} elseif (\is_string($value) && !preg_match('/^[^\x00-\x08\x0B\x0E-\x1A\x1C-\x1F\x7F]*+$/u', $value)) {
$element->setAttribute('type', 'binary');
$text = $this->document->createTextNode(self::phpToXml(base64_encode($value)));
$element->appendChild($text);
} else { } else {
if (in_array($value, array('null', 'true', 'false'), true)) { if (in_array($value, array('null', 'true', 'false'), true)) {
$element->setAttribute('type', 'string'); $element->setAttribute('type', 'string');

View File

@ -511,6 +511,12 @@ class XmlFileLoader extends FileLoader
} }
$arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag')); $arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'));
break; break;
case 'binary':
if (false === $value = base64_decode($arg->nodeValue)) {
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="binary" is not a valid base64 encoded string.', $name));
}
$arguments[$key] = $value;
break;
case 'string': case 'string':
$arguments[$key] = $arg->nodeValue; $arguments[$key] = $arg->nodeValue;
break; break;

View File

@ -246,6 +246,7 @@
<xsd:enumeration value="collection" /> <xsd:enumeration value="collection" />
<xsd:enumeration value="string" /> <xsd:enumeration value="string" />
<xsd:enumeration value="constant" /> <xsd:enumeration value="constant" />
<xsd:enumeration value="binary" />
</xsd:restriction> </xsd:restriction>
</xsd:simpleType> </xsd:simpleType>

View File

@ -9,6 +9,8 @@ $container = new ContainerBuilder(new ParameterBag(array(
'bar' => 'foo is %%foo bar', 'bar' => 'foo is %%foo bar',
'escape' => '@escapeme', 'escape' => '@escapeme',
'values' => array(true, false, null, 0, 1000.3, 'true', 'false', 'null'), 'values' => array(true, false, null, 0, 1000.3, 'true', 'false', 'null'),
'binary' => "\xf0\xf0\xf0\xf0",
'binary-control-char' => "This is a Bell char \x07",
))); )));
return $container; return $container;

View File

@ -135,6 +135,8 @@ class ProjectServiceContainer extends Container
6 => 'false', 6 => 'false',
7 => 'null', 7 => 'null',
), ),
'binary' => 'ðððð',
'binary-control-char' => 'This is a Bell char ',
); );
} }
} }

View File

@ -18,6 +18,8 @@
<parameter type="string">false</parameter> <parameter type="string">false</parameter>
<parameter type="string">null</parameter> <parameter type="string">null</parameter>
</parameter> </parameter>
<parameter key="binary" type="binary">8PDw8A==</parameter>
<parameter key="binary-control-char" type="binary">VGhpcyBpcyBhIEJlbGwgY2hhciAH</parameter>
</parameters> </parameters>
<services> <services>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/> <service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>

View File

@ -4,6 +4,8 @@ parameters:
bar: 'foo is %%foo bar' bar: 'foo is %%foo bar'
escape: '@@escapeme' escape: '@@escapeme'
values: [true, false, null, 0, 1000.3, 'true', 'false', 'null'] values: [true, false, null, 0, 1000.3, 'true', 'false', 'null']
binary: !!binary 8PDw8A==
binary-control-char: !!binary VGhpcyBpcyBhIEJlbGwgY2hhciAH
services: services:
service_container: service_container: