diff --git a/UPGRADE-3.1.md b/UPGRADE-3.1.md index 2920418afa..a7240561de 100644 --- a/UPGRADE-3.1.md +++ b/UPGRADE-3.1.md @@ -78,6 +78,21 @@ Yaml 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. Before: diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 0f79ed5a24..37236bac12 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -69,6 +69,21 @@ Yaml 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. Before: diff --git a/src/Symfony/Bridge/Twig/Extension/YamlExtension.php b/src/Symfony/Bridge/Twig/Extension/YamlExtension.php index c488893ff8..df33135db9 100644 --- a/src/Symfony/Bridge/Twig/Extension/YamlExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/YamlExtension.php @@ -12,6 +12,7 @@ namespace Symfony\Bridge\Twig\Extension; use Symfony\Component\Yaml\Dumper as YamlDumper; +use Symfony\Component\Yaml\Yaml; /** * Provides integration of the Yaml component with Twig. @@ -39,9 +40,16 @@ class YamlExtension extends \Twig_Extension $dumper = new YamlDumper(); } - if (defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT') && is_bool($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); - $dumpObjects = (int) $dumpObjects; + if (defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT')) { + if (is_bool($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); diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index d396713045..1ce9f6553a 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -13,7 +13,7 @@ CHANGELOG * Added support for customizing the dumped YAML string through an optional bit field: ```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 diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index a427e3c752..ab56150037 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -38,27 +38,38 @@ class Dumper /** * Dumps a PHP value to YAML. * - * @param mixed $input The PHP value - * @param int $inline The level where you switch to inline YAML - * @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 mixed $input The PHP value + * @param int $inline The level where you switch to inline YAML + * @param int $indent The level of indentation (used internally) + * @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 */ - 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)) { + @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); - $flags = (int) $flags; + if (func_get_arg(4)) { + $flags |= Yaml::DUMP_OBJECT; + } } $output = ''; $prefix = $indent ? str_repeat(' ', $indent) : ''; if ($inline <= 0 || !is_array($input) || empty($input)) { - $output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $flags); + $output .= $prefix.Inline::dump($input, $flags); } else { $isAHash = array_keys($input) !== range(0, count($input) - 1); @@ -67,9 +78,9 @@ class Dumper $output .= sprintf('%s%s%s%s', $prefix, - $isAHash ? Inline::dump($key, $exceptionOnInvalidType, $flags).':' : '-', + $isAHash ? Inline::dump($key, $flags).':' : '-', $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" : ''); } } diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 298ca1a212..2a3094d6ae 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -116,25 +116,36 @@ class Inline /** * Dumps a given PHP variable to a YAML string. * - * @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 mixed $value The PHP variable to convert + * @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 * * @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)) { + @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); - $flags = (int) $flags; + if (func_get_arg(2)) { + $flags |= Yaml::DUMP_OBJECT; + } } switch (true) { 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))); } @@ -144,13 +155,13 @@ class Inline 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.'); } return 'null'; case is_array($value): - return self::dumpArray($value, $exceptionOnInvalidType, $flags); + return self::dumpArray($value, $flags); case null === $value: return 'null'; case true === $value: @@ -196,13 +207,12 @@ class Inline /** * Dumps a PHP array to a YAML string. * - * @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 bool $objectSupport true if object support is enabled, false otherwise + * @param array $value The PHP array to dump + * @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 */ - private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport) + private static function dumpArray($value, $flags) { // array $keys = array_keys($value); @@ -212,7 +222,7 @@ class Inline ) { $output = array(); foreach ($value as $val) { - $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport); + $output[] = self::dump($val, $flags); } return sprintf('[%s]', implode(', ', $output)); @@ -221,7 +231,7 @@ class Inline // mapping $output = array(); 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)); diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index f5a9521f1c..c3ab4fef5d 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -179,7 +179,7 @@ EOF; 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'); } @@ -205,6 +205,15 @@ EOF; * @expectedException \Symfony\Component\Yaml\Exception\DumpException */ 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); } diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index b06a7250ed..56a5cf5dec 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -24,6 +24,7 @@ class Yaml const PARSE_EXCEPTION_ON_INVALID_TYPE = 2; const PARSE_OBJECT = 4; const PARSE_OBJECT_FOR_MAP = 8; + const DUMP_EXCEPTION_ON_INVALID_TYPE = 16; /** * Parses YAML into a PHP value. @@ -80,25 +81,36 @@ class Yaml * The dump method, when supplied with an array, will do its best * to convert the array into friendly YAML. * - * @param array $array PHP array - * @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 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 array $array PHP array + * @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 $flags A bit field of DUMP_* constants to customize the dumped YAML string * * @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)) { + @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); - $flags = (int) $flags; + if (func_get_arg(4)) { + $flags |= self::DUMP_OBJECT; + } } $yaml = new Dumper(); $yaml->setIndentation($indent); - return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags); + return $yaml->dump($array, $inline, 0, $flags); } }