Skip to content

Commit ab951a4

Browse files
committed
feature #17462 [Yaml] deprecate parsing the !!php/object tag (xabbuh)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Yaml] deprecate parsing the !!php/object tag | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #16877 | License | MIT | Doc PR | Commits ------- 3380346 [Yaml] deprecate parsing the !!php/object tag f146c8d tag for dumped PHP objects must be a local one
2 parents 09f92ba + 3380346 commit ab951a4

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/Symfony/Component/Yaml/Inline.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
105105
return 'null';
106106
case is_object($value):
107107
if ($objectSupport) {
108-
return '!!php/object:'.serialize($value);
108+
return '!php/object:'.serialize($value);
109109
}
110110

111111
if ($exceptionOnInvalidType) {
@@ -476,8 +476,20 @@ private static function evaluateScalar($scalar, $references = array())
476476
return (string) substr($scalar, 5);
477477
case 0 === strpos($scalar, '! '):
478478
return (int) self::parseScalar(substr($scalar, 2));
479+
case 0 === strpos($scalar, '!php/object:'):
480+
if (self::$objectSupport) {
481+
return unserialize(substr($scalar, 12));
482+
}
483+
484+
if (self::$exceptionOnInvalidType) {
485+
throw new ParseException('Object support when parsing a YAML file has been disabled.');
486+
}
487+
488+
return;
479489
case 0 === strpos($scalar, '!!php/object:'):
480490
if (self::$objectSupport) {
491+
@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);
492+
481493
return unserialize(substr($scalar, 13));
482494
}
483495

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function testObjectSupportEnabled()
180180
{
181181
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
182182

183-
$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');
183+
$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');
184184
}
185185

186186
public function testObjectSupportDisabledButNoExceptions()

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

+33-5
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,29 @@ public function testBlockLiteralWithLeadingNewlines()
422422
public function testObjectSupportEnabled()
423423
{
424424
$input = <<<EOF
425-
foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
425+
foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
426426
bar: 1
427427
EOF;
428428
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
429429
}
430430

431-
public function testObjectSupportDisabledButNoExceptions()
431+
/**
432+
* @group legacy
433+
*/
434+
public function testObjectSupportEnabledWithDeprecatedTag()
432435
{
433436
$input = <<<EOF
434-
foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
437+
foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
435438
bar: 1
436439
EOF;
440+
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
441+
}
437442

443+
/**
444+
* @dataProvider invalidDumpedObjectProvider
445+
*/
446+
public function testObjectSupportDisabledButNoExceptions($input)
447+
{
438448
$this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
439449
}
440450

@@ -470,11 +480,29 @@ public function testObjectForMapIsAppliedAfterParsing()
470480
}
471481

472482
/**
483+
* @dataProvider invalidDumpedObjectProvider
473484
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
474485
*/
475-
public function testObjectsSupportDisabledWithExceptions()
486+
public function testObjectsSupportDisabledWithExceptions($yaml)
476487
{
477-
$this->parser->parse('foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}', true, false);
488+
$this->parser->parse($yaml, true, false);
489+
}
490+
491+
public function invalidDumpedObjectProvider()
492+
{
493+
$yamlTag = <<<EOF
494+
foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
495+
bar: 1
496+
EOF;
497+
$localTag = <<<EOF
498+
foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
499+
bar: 1
500+
EOF;
501+
502+
return array(
503+
'yaml-tag' => array($yamlTag),
504+
'local-tag' => array($localTag),
505+
);
478506
}
479507

480508
/**

0 commit comments

Comments
 (0)