8000 [Yaml] Deprecate tags using colon · symfony/symfony@84bd287 · GitHub
[go: up one dir, main page]

Skip to content

Commit 84bd287

Browse files
committed
[Yaml] Deprecate tags using colon
1 parent 7695112 commit 84bd287

File tree

9 files changed

+155
-32
lines changed

9 files changed

+155
-32
lines changed

UPGRADE-3.4.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ FrameworkBundle
3030
require symfony/stopwatch` in your `dev` environment.
3131

3232
* Using the `KERNEL_DIR` environment variable or the automatic guessing based
33-
on the `phpunit.xml` / `phpunit.xml.dist` file location is deprecated since 3.4.
33+
on the `phpunit.xml` / `phpunit.xml.dist` file location is deprecated since 3.4.
3434
Set the `KERNEL_CLASS` environment variable to the fully-qualified class name
35-
of your Kernel instead. Not setting the `KERNEL_CLASS` environment variable
36-
will throw an exception on 4.0 unless you override the `KernelTestCase::createKernel()`
35+
of your Kernel instead. Not setting the `KERNEL_CLASS` environment variable
36+
will throw an exception on 4.0 unless you override the `KernelTestCase::createKernel()`
3737
or `KernelTestCase::getKernelClass()` method.
38-
39-
* The `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()`
38+
39+
* The `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()`
4040
methods are deprecated since 3.4 and will be removed in 4.0.
4141

4242
* The `--no-prefix` option of the `translation:update` command is deprecated and
@@ -83,7 +83,7 @@ TwigBridge
8383
* deprecated the `Symfony\Bridge\Twig\Form\TwigRenderer` class, use the `FormRenderer`
8484
class from the Form component instead
8585

86-
* deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
86+
* deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
8787
to pass a command name as first argument
8888

8989
* deprecated `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability
@@ -95,7 +95,7 @@ TwigBundle
9595
* deprecated the `Symfony\Bundle\TwigBundle\Command\DebugCommand` class, use the `DebugCommand`
9696
class from the Twig bridge instead
9797

98-
* deprecated relying on the `ContainerAwareInterface` implementation for
98+
* deprecated relying on the `ContainerAwareInterface` implementation for
9999
`Symfony\Bundle\TwigBundle\Command\LintCommand`
100100

101101
Validator
@@ -107,6 +107,24 @@ Validator
107107
Yaml
108108
----
109109

110+
* using the `!php/object:` tag is deprecated and won't be supported in 4.0. Use
111+
the `!php/object` tag (without the colon) instead.
112+
113+
* using the `!php/const:` tag is deprecated and won't be supported in 4.0. Use
114+
the `!php/const` tag (without the colon) instead.
115+
116+
Before:
117+
118+
```yml
119+
!php/const:PHP_INT_MAX
120+
```
121+
122+
After:
123+
124+
```yml
125+
!php/const PHP_INT_MAX
126+
```
127+
110128
* Support for the `!str` tag is deprecated, use the `!!str` tag instead.
111129

112130
* Using the non-specific tag `!` is deprecated and will have a different

UPGRADE-4.0.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,13 @@ FrameworkBundle
336336
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ValidateWorkflowsPass` class
337337
has been removed. Use the `Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass`
338338
class instead.
339-
339+
340340
* Using the `KERNEL_DIR` environment variable and the automatic guessing based
341-
on the `phpunit.xml` file location have been removed from the `KernelTestCase::getKernelClass()`
341+
on the `phpunit.xml` file location have been removed from the `KernelTestCase::getKernelClass()`
342342
method implementation. Set the `KERNEL_CLASS` environment variable to the
343-
fully-qualified class name of your Kernel or override the `KernelTestCase::createKernel()`
343+
fully-qualified class name of your Kernel or override the `KernelTestCase::createKernel()`
344344
or `KernelTestCase::getKernelClass()` method instead.
345-
345+
346346
* The methods `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()`
347347
have been removed.
348348

@@ -771,3 +771,21 @@ Yaml
771771

772772
* The behavior of the non-specific tag `!` is changed and now forces
773773
non-evaluating your values.
774+
775+
* The `!php/object:` tag was removed in favor of the `!php/object` tag (without
776+
the colon).
777+
778+
* The `!php/const:` tag was removed in favor of the `!php/const` tag (without
779+
the colon).
780+
781+
Before:
782+
783+
```yml
784+
!php/const:PHP_INT_MAX
785+
```
786+
787+
After:
788+
789+
```yml
790+
!php/const PHP_INT_MAX
791+
```

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ CHANGELOG
44
3.4.0
55
-----
66

7+
* Deprecated the tag`!php/object:` tag which will be replaced by the
8+
`!php/object` tag (without the colon) in 4.0.
9+
10+
* Deprecated the tag`!php/const:` tag which will be replaced by the
11+
`!php/const` tag (without the colon) in 4.0.
12+
713
* Support for the `!str` tag is deprecated, use the `!!str` tag instead.
814

915
* Deprecated using the non-specific tag `!` as its behavior will change in 4.0.

src/Symfony/Component/Yaml/Inline.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static function dump($value, $flags = 0)
170170
}
171171

172172
if (Yaml::DUMP_OBJECT & $flags) {
173-
return '!php/object:'.serialize($value);
173+
return '!php/object '.self::dump(serialize($value));
174174
}
175175

176176
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
@@ -620,6 +620,8 @@ private static function evaluateScalar($scalar, $flags, $references = array())
620620
return (int) self::parseScalar(substr($scalar, 2), $flags);
621621
case 0 === strpos($scalar, '!php/object:'):
622622
if (self::$objectSupport) {
623+
@trigger_error('The !php/object: tag to indicate dumped PHP objects is deprecated since version 3.4 and will be removed in 4.0. Use the !php/object (without the colon) tag instead.', E_USER_DEPRECATED);
624+
623625
return unserialize(substr($scalar, 12));
624626
}
625627

@@ -630,7 +632,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
630632
return;
631633
case 0 === strpos($scalar, '!!php/object:'):
632634
if (self::$objectSupport) {
633-
@trigger_error('The !!php/object tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object tag instead.', E_USER_DEPRECATED);
635+
@trigger_error('The !!php/object: tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object (without the colon) tag instead.', E_USER_DEPRECATED);
634636

635637
return unserialize(substr($scalar, 13));
636638
}
@@ -639,9 +641,21 @@ private static function evaluateScalar($scalar, $flags, $references = array())
639641
throw new ParseException('Object support when parsing a YAML file has been disabled.');
640642
}
641643

644+
return;
645+
case 0 === strpos($scalar, '!php/object'):
646+
if (self::$objectSupport) {
647+
return unserialize(self::parseScalar(substr($scalar, 12)));
648+
}
649+
650+
if (self::$exceptionOnInvalidType) {
651+
throw new ParseException('Object support when parsing a YAML file has been disabled.');
652+
}
653+
642654
return;
643655
case 0 === strpos($scalar, '!php/const:'):
644656
if (self::$constantSupport) {
657+
@trigger_error('The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.', E_USER_DEPRECATED);
658+
645659
if (defined($const = substr($scalar, 11))) {
646660
return constant($const);
647661
}
@@ -652,6 +666,20 @@ private static function evaluateScalar($scalar, $flags, $references = array())
652666
throw new ParseException(sprintf('The string "%s" could not be parsed as a constant. Have you forgotten to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar));
653667
}
654668

669+
return;
670+
671+
case 0 === strpos($scalar, '!php/const'):
672+
if (self::$constantSupport) {
673+
if (defined($const = self::parseScalar(substr($scalar, 11)))) {
674+
return constant($const);
675+
}
676+
677+
throw new ParseException(sprintf('The constant "%s" is not defined.', $const));
678+
}
679+
if (self::$exceptionOnInvalidType) {
680+
throw new ParseException(sprintf('The string "%s" could not be parsed as a constant. Have you forgotten to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar));
681+
}
682+
655683
return;
656684
case 0 === strpos($scalar, '!!float '):
657685
return (float) substr($scalar, 8);

src/Symfony/Component/Yaml/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private function doParse($value, $flags)
208208
$this->refs[$isRef] = end($data);
209209
}
210210
} elseif (
211-
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
211+
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
212212
&& (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))
213213
) {
214214
if ($context && 'sequence' == $context) {

src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testLintIncorrectFile()
5454
public function testConstantAsKey()
5555
{
5656
$yaml = <<<YAML
57-
!php/const:Symfony\Component\Yaml\Tests\Command\Foo::TEST: bar
57+
!php/const 'Symfony\Component\Yaml\Tests\Command\Foo::TEST': bar
5858
YAML;
5959
$ret = $this->createCommandTester()->execute(array('filename' => $this->createFile($yaml)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
6060
$this->assertSame(0, $ret, 'lint:yaml exits with code 0 in case of success');

src/Symfony/Component/Yaml/Tests/DumperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function testObjectSupportEnabled()
210210
{
211211
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
212212

213-
$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');
213+
$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');
214214
}
215215

216216
/**
@@ -220,7 +220,7 @@ public function testObjectSupportEnabledPassingTrue()
220220
{
221221
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
222222

223-
$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');
223+
$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');
224224
}
225225

226226
public function testObjectSupportDisabledButNoExceptions()

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public function testParsePhpConstants($yaml, $value)
4949
public function getTestsForParsePhpConstants()
5050
{
5151
return array(
52-
array('!php/const:Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
53-
array('!php/const:PHP_INT_MAX', PHP_INT_MAX),
54-
array('[!php/const:PHP_INT_MAX]', array(PHP_INT_MAX)),
55-
array('{ foo: !php/const:PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
52+
array('!php/const Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
53+
array('!php/const PHP_INT_MAX', PHP_INT_MAX),
54+
array('[!php/const PHP_INT_MAX]', array(PHP_INT_MAX)),
55+
array('{ foo: !php/const PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
5656
);
5757
}
5858

@@ -62,16 +62,25 @@ public function getTestsForParsePhpConstants()
6262
*/
6363
public function testParsePhpConstantThrowsExceptionWhenUndefined()
6464
{
65-
Inline::parse('!php/const:WRONG_CONSTANT', Yaml::PARSE_CONSTANT);
65+
Inline::parse('!php/const WRONG_CONSTANT', Yaml::PARSE_CONSTANT);
6666
}
6767

6868
/**
6969
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
70-
* @expectedExceptionMessageRegExp #The string "!php/const:PHP_INT_MAX" could not be parsed as a constant.*#
70+
* @expectedExceptionMessageRegExp #The string "!php/const PHP_INT_MAX" could not be parsed as a constant.*#
7171
*/
7272
public function testParsePhpConstantThrowsExceptionOnInvalidType()
7373
{
74-
Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
74+
Inline::parse('!php/const PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
75+
}
76+
77+
/**
78+
* @group legacy
79+
* @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.
80+
*/
81+
public function testDeprecatedConstantTag()
82+
{
83+
Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_CONSTANT);
7584
}
7685

7786
/**

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ public function testBlockLiteralWithLeadingNewlines()
469469
public function testObjectSupportEnabled()
470470
{
471471
$input = <<<'EOF'
472-
foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
472+
foo: !php/object O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
473473
bar: 1
474474
EOF;
475475
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
@@ -489,14 +489,29 @@ public function testObjectSupportEnabledPassingTrue()
489489

490490
/**
491491
* @group legacy
492+
* @dataProvider deprecatedObjectValueProvider
492493
*/
493-
public function testObjectSupportEnabledWithDeprecatedTag()
494+
public function testObjectSupportEnabledWithDeprecatedTag($yaml)
494495
{
495-
$input = <<<'EOF'
496+
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($yaml, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
497+
}
498+
499+
public function deprecatedObjectValueProvider()
500+
{
501+
return array(
502+
array(
503+
<<<YAML
496504
foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
497505
bar: 1
498-
EOF;
499-
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
506+
YAML
507+
),
508+
array(
509+
<<<YAML
510+
foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
511+
bar: 1
512+
YAML
513+
),
514+
);
500515
}
501516

502517
/**
@@ -1823,6 +1838,35 @@ public function testParserCleansUpReferencesBetweenRuns()
18231838
public function testPhpConstantTagMappingKey()
18241839
{
18251840
$yaml = <<<YAML
1841+
transitions:
1842+
!php/const 'Symfony\Component\Yaml\Tests\B::FOO':
1843+
from:
1844+
- !php/const 'Symfony\Component\Yaml\Tests\B::BAR'
1845+
to: !php/const 'Symfony\Component\Yaml\Tests\B::BAZ'
1846+
YAML;
1847+
$expected = array(
1848+
'transitions' => array(
1849+
'foo' => array(
1850+
'from' => array(
1851+
'bar',
1852+
),
1853+
'to' => 'baz',
1854+
),
1855+
),
1856+
);
1857+
1858+
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
1859+
}
1860+
1861+
/**
1862+
* @group legacy
1863+
* @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.
1864+
* @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.
1865+
* @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.
1866+
*/
1867+
public function testDeprecatedPhpConstantTagMappingKey()
1868+
{
1869+
$yaml = <<<YAML
18261870
transitions:
18271871
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
18281872
from:
@@ -1847,10 +1891,10 @@ public function testPhpConstantTagMappingKeyWithKeysCastToStrings()
18471891
{
18481892
$yaml = <<<YAML
18491893
transitions:
1850-
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
1894+
!php/const 'Symfony\Component\Yaml\Tests\B::FOO':
18511895
from:
1852-
- !php/const:Symfony\Component\Yaml\Tests\B::BAR
1853-
to: !php/const:Symfony\Component\Yaml\Tests\B::BAZ
1896+
- !php/const 'Symfony\Component\Yaml\Tests\B::BAR'
1897+
to: !php/const 'Symfony\Component\Yaml\Tests\B::BAZ'
18541898
YAML;
18551899
$expected = array(
18561900
'transitions' => array(

0 commit comments

Comments
 (0)
0