dumper flag to enable exceptions on invalid types
This commit is contained in:
parent
33c797ee05
commit
e572a641b0
|
@ -78,6 +78,21 @@ Yaml
|
||||||
Yaml::parse('{ "foo": "bar", "fiz": "cat" }', Yaml::PARSE_OBJECT_FOR_MAP);
|
Yaml::parse('{ "foo": "bar", "fiz": "cat" }', Yaml::PARSE_OBJECT_FOR_MAP);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* Deprecated support for passing `true`/`false` as the fourth argument to the
|
||||||
|
`dump()` method to trigger exceptions when an invalid type was passed.
|
||||||
|
|
||||||
|
Before:
|
||||||
|
|
||||||
|
```php
|
||||||
|
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
|
||||||
|
```
|
||||||
|
|
||||||
|
After:
|
||||||
|
|
||||||
|
```php
|
||||||
|
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
|
||||||
|
```
|
||||||
|
|
||||||
* Deprecated support for passing `true`/`false` as the fifth argument to the `dump()` method to toggle object support.
|
* Deprecated support for passing `true`/`false` as the fifth argument to the `dump()` method to toggle object support.
|
||||||
|
|
||||||
Before:
|
Before:
|
||||||
|
|
|
@ -69,6 +69,21 @@ Yaml
|
||||||
Yaml::parse('{ "foo": "bar", "fiz": "cat" }', Yaml::PARSE_OBJECT_FOR_MAP);
|
Yaml::parse('{ "foo": "bar", "fiz": "cat" }', Yaml::PARSE_OBJECT_FOR_MAP);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* Removed support for passing `true`/`false` as the fourth argument to the
|
||||||
|
`dump()` method to trigger exceptions when an invalid type was passed.
|
||||||
|
|
||||||
|
Before:
|
||||||
|
|
||||||
|
```php
|
||||||
|
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
|
||||||
|
```
|
||||||
|
|
||||||
|
After:
|
||||||
|
|
||||||
|
```php
|
||||||
|
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
|
||||||
|
```
|
||||||
|
|
||||||
* Removed support for passing `true`/`false` as the fifth argument to the `dump()` method to toggle object support.
|
* Removed support for passing `true`/`false` as the fifth argument to the `dump()` method to toggle object support.
|
||||||
|
|
||||||
Before:
|
Before:
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
namespace Symfony\Bridge\Twig\Extension;
|
namespace Symfony\Bridge\Twig\Extension;
|
||||||
|
|
||||||
use Symfony\Component\Yaml\Dumper as YamlDumper;
|
use Symfony\Component\Yaml\Dumper as YamlDumper;
|
||||||
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides integration of the Yaml component with Twig.
|
* Provides integration of the Yaml component with Twig.
|
||||||
|
@ -39,9 +40,16 @@ class YamlExtension extends \Twig_Extension
|
||||||
$dumper = new YamlDumper();
|
$dumper = new YamlDumper();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT') && is_bool($dumpObjects)) {
|
if (defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT')) {
|
||||||
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
if (is_bool($dumpObjects)) {
|
||||||
$dumpObjects = (int) $dumpObjects;
|
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
||||||
|
|
||||||
|
$flags = $dumpObjects ? Yaml::DUMP_OBJECT : 0;
|
||||||
|
} else {
|
||||||
|
$flags = $dumpObjects;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $dumper->dump($input, $inline, 0, $flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dumper->dump($input, $inline, 0, false, $dumpObjects);
|
return $dumper->dump($input, $inline, 0, false, $dumpObjects);
|
||||||
|
|
|
@ -13,7 +13,7 @@ CHANGELOG
|
||||||
* Added support for customizing the dumped YAML string through an optional bit field:
|
* Added support for customizing the dumped YAML string through an optional bit field:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
|
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE | Yaml::DUMP_OBJECT);
|
||||||
```
|
```
|
||||||
|
|
||||||
3.0.0
|
3.0.0
|
||||||
|
|
|
@ -38,27 +38,38 @@ class Dumper
|
||||||
/**
|
/**
|
||||||
* Dumps a PHP value to YAML.
|
* Dumps a PHP value to YAML.
|
||||||
*
|
*
|
||||||
* @param mixed $input The PHP value
|
* @param mixed $input The PHP value
|
||||||
* @param int $inline The level where you switch to inline YAML
|
* @param int $inline The level where you switch to inline YAML
|
||||||
* @param int $indent The level of indentation (used internally)
|
* @param int $indent The level of indentation (used internally)
|
||||||
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
|
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
||||||
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
|
||||||
*
|
*
|
||||||
* @return string The YAML representation of the PHP value
|
* @return string The YAML representation of the PHP value
|
||||||
*/
|
*/
|
||||||
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $flags = 0)
|
public function dump($input, $inline = 0, $indent = 0, $flags = 0)
|
||||||
{
|
{
|
||||||
if (is_bool($flags)) {
|
if (is_bool($flags)) {
|
||||||
|
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
||||||
|
|
||||||
|
if ($flags) {
|
||||||
|
$flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
||||||
|
} else {
|
||||||
|
$flags = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (func_num_args() >= 5) {
|
||||||
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
||||||
|
|
||||||
$flags = (int) $flags;
|
if (func_get_arg(4)) {
|
||||||
|
$flags |= Yaml::DUMP_OBJECT;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = '';
|
$output = '';
|
||||||
$prefix = $indent ? str_repeat(' ', $indent) : '';
|
$prefix = $indent ? str_repeat(' ', $indent) : '';
|
||||||
|
|
||||||
if ($inline <= 0 || !is_array($input) || empty($input)) {
|
if ($inline <= 0 || !is_array($input) || empty($input)) {
|
||||||
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $flags);
|
$output .= $prefix.Inline::dump($input, $flags);
|
||||||
} else {
|
} else {
|
||||||
$isAHash = array_keys($input) !== range(0, count($input) - 1);
|
$isAHash = array_keys($input) !== range(0, count($input) - 1);
|
||||||
|
|
||||||
|
@ -67,9 +78,9 @@ class Dumper
|
||||||
|
|
||||||
$output .= sprintf('%s%s%s%s',
|
$output .= sprintf('%s%s%s%s',
|
||||||
$prefix,
|
$prefix,
|
||||||
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $flags).':' : '-',
|
$isAHash ? Inline::dump($key, $flags).':' : '-',
|
||||||
$willBeInlined ? ' ' : "\n",
|
$willBeInlined ? ' ' : "\n",
|
||||||
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $flags)
|
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
|
||||||
).($willBeInlined ? "\n" : '');
|
).($willBeInlined ? "\n" : '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,25 +116,36 @@ class Inline
|
||||||
/**
|
/**
|
||||||
* Dumps a given PHP variable to a YAML string.
|
* Dumps a given PHP variable to a YAML string.
|
||||||
*
|
*
|
||||||
* @param mixed $value The PHP variable to convert
|
* @param mixed $value The PHP variable to convert
|
||||||
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
|
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
||||||
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
|
||||||
*
|
*
|
||||||
* @return string The YAML string representing the PHP array
|
* @return string The YAML string representing the PHP array
|
||||||
*
|
*
|
||||||
* @throws DumpException When trying to dump PHP resource
|
* @throws DumpException When trying to dump PHP resource
|
||||||
*/
|
*/
|
||||||
public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
|
public static function dump($value, $flags = 0)
|
||||||
{
|
{
|
||||||
if (is_bool($flags)) {
|
if (is_bool($flags)) {
|
||||||
|
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
||||||
|
|
||||||
|
if ($flags) {
|
||||||
|
$flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
||||||
|
} else {
|
||||||
|
$flags = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (func_num_args() >= 3) {
|
||||||
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
||||||
|
|
||||||
$flags = (int) $flags;
|
if (func_get_arg(2)) {
|
||||||
|
$flags |= Yaml::DUMP_OBJECT;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case is_resource($value):
|
case is_resource($value):
|
||||||
if ($exceptionOnInvalidType) {
|
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
|
||||||
throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
|
throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,13 +155,13 @@ class Inline
|
||||||
return '!php/object:'.serialize($value);
|
return '!php/object:'.serialize($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($exceptionOnInvalidType) {
|
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
|
||||||
throw new DumpException('Object support when dumping a YAML file has been disabled.');
|
throw new DumpException('Object support when dumping a YAML file has been disabled.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'null';
|
return 'null';
|
||||||
case is_array($value):
|
case is_array($value):
|
||||||
return self::dumpArray($value, $exceptionOnInvalidType, $flags);
|
return self::dumpArray($value, $flags);
|
||||||
case null === $value:
|
case null === $value:
|
||||||
return 'null';
|
return 'null';
|
||||||
case true === $value:
|
case true === $value:
|
||||||
|
@ -196,13 +207,12 @@ class Inline
|
||||||
/**
|
/**
|
||||||
* Dumps a PHP array to a YAML string.
|
* Dumps a PHP array to a YAML string.
|
||||||
*
|
*
|
||||||
* @param array $value The PHP array to dump
|
* @param array $value The PHP array to dump
|
||||||
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
|
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
||||||
* @param bool $objectSupport true if object support is enabled, false otherwise
|
|
||||||
*
|
*
|
||||||
* @return string The YAML string representing the PHP array
|
* @return string The YAML string representing the PHP array
|
||||||
*/
|
*/
|
||||||
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
|
private static function dumpArray($value, $flags)
|
||||||
{
|
{
|
||||||
// array
|
// array
|
||||||
$keys = array_keys($value);
|
$keys = array_keys($value);
|
||||||
|
@ -212,7 +222,7 @@ class Inline
|
||||||
) {
|
) {
|
||||||
$output = array();
|
$output = array();
|
||||||
foreach ($value as $val) {
|
foreach ($value as $val) {
|
||||||
$output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
|
$output[] = self::dump($val, $flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sprintf('[%s]', implode(', ', $output));
|
return sprintf('[%s]', implode(', ', $output));
|
||||||
|
@ -221,7 +231,7 @@ class Inline
|
||||||
// mapping
|
// mapping
|
||||||
$output = array();
|
$output = array();
|
||||||
foreach ($value as $key => $val) {
|
foreach ($value as $key => $val) {
|
||||||
$output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport));
|
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
|
||||||
}
|
}
|
||||||
|
|
||||||
return sprintf('{ %s }', implode(', ', $output));
|
return sprintf('{ %s }', implode(', ', $output));
|
||||||
|
|
|
@ -179,7 +179,7 @@ EOF;
|
||||||
|
|
||||||
public function testObjectSupportEnabled()
|
public function testObjectSupportEnabled()
|
||||||
{
|
{
|
||||||
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
|
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
|
||||||
|
|
||||||
$this->assertEquals('{ foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
|
$this->assertEquals('{ foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
|
||||||
}
|
}
|
||||||
|
@ -205,6 +205,15 @@ EOF;
|
||||||
* @expectedException \Symfony\Component\Yaml\Exception\DumpException
|
* @expectedException \Symfony\Component\Yaml\Exception\DumpException
|
||||||
*/
|
*/
|
||||||
public function testObjectSupportDisabledWithExceptions()
|
public function testObjectSupportDisabledWithExceptions()
|
||||||
|
{
|
||||||
|
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group legacy
|
||||||
|
* @expectedException \Symfony\Component\Yaml\Exception\DumpException
|
||||||
|
*/
|
||||||
|
public function testObjectSupportDisabledWithExceptionsPassingTrue()
|
||||||
{
|
{
|
||||||
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
|
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ class Yaml
|
||||||
const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
|
const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
|
||||||
const PARSE_OBJECT = 4;
|
const PARSE_OBJECT = 4;
|
||||||
const PARSE_OBJECT_FOR_MAP = 8;
|
const PARSE_OBJECT_FOR_MAP = 8;
|
||||||
|
const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses YAML into a PHP value.
|
* Parses YAML into a PHP value.
|
||||||
|
@ -80,25 +81,36 @@ class Yaml
|
||||||
* The dump method, when supplied with an array, will do its best
|
* The dump method, when supplied with an array, will do its best
|
||||||
* to convert the array into friendly YAML.
|
* to convert the array into friendly YAML.
|
||||||
*
|
*
|
||||||
* @param array $array PHP array
|
* @param array $array PHP array
|
||||||
* @param int $inline The level where you switch to inline YAML
|
* @param int $inline The level where you switch to inline YAML
|
||||||
* @param int $indent The amount of spaces to use for indentation of nested nodes.
|
* @param int $indent The amount of spaces to use for indentation of nested nodes.
|
||||||
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
|
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
|
||||||
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
|
|
||||||
*
|
*
|
||||||
* @return string A YAML string representing the original PHP array
|
* @return string A YAML string representing the original PHP array
|
||||||
*/
|
*/
|
||||||
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $flags = 0)
|
public static function dump($array, $inline = 2, $indent = 4, $flags = 0)
|
||||||
{
|
{
|
||||||
if (is_bool($flags)) {
|
if (is_bool($flags)) {
|
||||||
|
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
||||||
|
|
||||||
|
if ($flags) {
|
||||||
|
$flags = self::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
||||||
|
} else {
|
||||||
|
$flags = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (func_num_args() >= 5) {
|
||||||
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
||||||
|
|
||||||
$flags = (int) $flags;
|
if (func_get_arg(4)) {
|
||||||
|
$flags |= self::DUMP_OBJECT;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$yaml = new Dumper();
|
$yaml = new Dumper();
|
||||||
$yaml->setIndentation($indent);
|
$yaml->setIndentation($indent);
|
||||||
|
|
||||||
return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags);
|
return $yaml->dump($array, $inline, 0, $flags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue