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

Skip to content

Commit a1b744f

Browse files
committed
[Yaml] Deprecate tags using colon
1 parent a6d173c commit a1b744f

File tree

7 files changed

+118
-18
lines changed

7 files changed

+118
-18
lines changed

UPGRADE-3.4.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,32 @@ Finder
55
------
66

77
* The `Symfony\Component\Finder\Iterator\FilterIterator` class has been
8-
deprecated and will be removed in 4.0 as it used to fix a bug which existed
8+
deprecated and will be removed in 4.0 as it used to fix a bug which existed
99
before version 5.5.23/5.6.7
1010

1111
Validator
1212
---------
1313

1414
* not setting the `strict` option of the `Choice` constraint to `true` is
1515
deprecated and will throw an exception in Symfony 4.0
16+
17+
Yaml
18+
----
19+
20+
* using the `!php/object:` tag is deprecated and won't be supported in 4.0. Use
21+
the `!php/object` tag (without the colon) instead.
22+
23+
* using the `!php/const:` tag is deprecated and won't be supported in 4.0. Use
24+
the `!php/const` tag (without the colon) instead.
25+
26+
Before:
27+
28+
```yml
29+
!php/const:PHP_INT_MAX
30+
```
31+
32+
After:
33+
34+
```yml
35+
!php/const PHP_INT_MAX
36+
```

UPGRADE-4.0.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ FrameworkBundle
301301

302302
* Extending `ConstraintValidatorFactory` is not supported anymore.
303303

304-
* Passing an array of validators or validator aliases as the second argument of
304+
* Passing an array of validators or validator aliases as the second argument of
305305
`ConstraintValidatorFactory::__construct()` has been removed.
306306
Use the service locator instead.
307307

@@ -689,3 +689,21 @@ Yaml
689689

690690
* The constructor arguments `$offset`, `$totalNumberOfLines` and
691691
`$skippedLineNumbers` of the `Parser` class were removed.
692+
693+
* The `!php/object:` tag was removed in favor of the `!php/object` tag (without
694+
the colon).
695+
696+
* The `!php/const:` tag was removed in favor of the `!php/const` tag (without
697+
the colon).
698+
699+
Before:
700+
701+
```yml
702+
!php/const:PHP_INT_MAX
703+
```
704+
705+
After:
706+
707+
```yml
708+
!php/const PHP_INT_MAX
709+
```

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
CHANGELOG
22
=========
33

4+
3.4.0
5+
-----
6+
7+
* using the `!php/object:` tag is deprecated and won't be supported in 4.0. Use
8+
the `!php/object` tag (without the colon) instead.
9+
10+
* using the `!php/const:` tag is deprecated and won't be supported in 4.0. Use
11+
the `!php/const` tag (without the colon) instead.
12+
413
3.3.0
514
-----
615

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)) {
@@ -613,6 +613,8 @@ private static function evaluateScalar($scalar, $flags, $references = array())
613613
return (int) self::parseScalar(substr($scalar, 2), $flags);
614614
case 0 === strpos($scalar, '!php/object:'):
615615
if (self::$objectSupport) {
616+
@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);
617+
616618
return unserialize(substr($scalar, 12));
617619
}
618620

@@ -623,7 +625,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
623625
return;
624626
case 0 === strpos($scalar, '!!php/object:'):
625627
if (self::$objectSupport) {
626-
@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);
628+
@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);
627629

628630
return unserialize(substr($scalar, 13));
629631
}
@@ -632,9 +634,21 @@ private static function evaluateScalar($scalar, $flags, $references = array())
632634
throw new ParseException('Object support when parsing a YAML file has been disabled.');
633635
}
634636

637+
return;
638+
case 0 === strpos($scalar, '!php/object'):
639+
if (self::$objectSupport) {
640+
return unserialize(self::parseScalar(substr($scalar, 12)));
641+
}
642+
643+
if (self::$exceptionOnInvalidType) {
644+
throw new ParseException('Object support when parsing a YAML file has been disabled.');
645+
}
646+
635647
return;
636648
case 0 === strpos($scalar, '!php/const:'):
637649
if (self::$constantSupport) {
650+
@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);
651+
638652
if (defined($const = substr($scalar, 11))) {
639653
return constant($const);
640654
}
@@ -645,6 +659,20 @@ private static function evaluateScalar($scalar, $flags, $references = array())
645659
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));
646660
}
647661

662+
return;
663+
664+
case 0 === strpos($scalar, '!php/const'):
665+
if (self::$constantSupport) {
666+
if (defined($const = self::parseScalar(substr($scalar, 11)))) {
667+
return constant($const);
668+
}
669+
670+
throw new ParseException(sprintf('The constant "%s" is not defined.', $const));
671+
}
672+
if (self::$exceptionOnInvalidType) {
673+
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));
674+
}
675+
648676
return;
649677
case 0 === strpos($scalar, '!!float '):
650678
return (float) substr($scalar, 8);

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: 20 additions & 5 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
/**

0 commit comments

Comments
 (0)
0