8000 [Yaml] added support for parsing PHP constants · symfony/symfony@23ff82b · GitHub
[go: up one dir, main page]

Skip to content

Commit 23ff82b

Browse files
committed
[Yaml] added support for parsing PHP constants
1 parent b44abe3 commit 23ff82b

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

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.2.0
5+
-----
6+
7+
* Added support for parsing PHP constants:
8+
9+
```php
10+
Yaml::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_CONSTANT);
11+
```
12+
413
3.1.0
514
-----
615

src/Symfony/Component/Yaml/Inline.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Inline
2828
private static $exceptionOnInvalidType = false;
2929
private static $objectSupport = false;
3030
private static $objectForMap = false;
31+
private static $constantSupport = false;
3132

3233
/**
3334
* Converts a YAML string to a PHP array.
@@ -77,6 +78,7 @@ public static function parse($value, $flags = 0, $references = array())
7778
self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
7879
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
7980
self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
81+
self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
8082

8183
$value = trim($value);
8284

@@ -561,6 +563,15 @@ private static function evaluateScalar($scalar, $flags, $references = array())
561563
throw new ParseException('Object support when parsing a YAML file has been disabled.');
562564
}
563565

566+
return;
567+
case 0 === strpos($scalar, '!php/const:'):
568+
if (self::$constantSupport) {
569+
return @constant(substr($scalar, 11));
570+
}
571+
if (self::$exceptionOnInvalidType) {
572+
throw new ParseException(sprintf('The string "%s" could not be parsed as constant. Have you forgotten to pass "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar));
573+
}
574+
564575
return;
565576
case 0 === strpos($scalar, '!!float '):
566577
return (float) substr($scalar, 8);

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ public function testParseWithMapObjects($yaml, $value)
3434
$this->assertSame(serialize($value), serialize($actual));
3535
}
3636

37+
/**
38+
* @dataProvider getTestsForParsePhpConstants
39+
*/
40+
public function testParsePhpConstants($yaml, $value)
41+
{
42+
$actual = Inline::parse($yaml, Yaml::PARSE_CONSTANT);
43+
44+
$this->assertSame($value, $actual);
45+
}
46+
47+
public function testParsePhpConstantThrowsExceptionOnInvalidType()
48+
{
49+
$expectedMessage = '#The string "!php/const:PHP_INT_MAX" could not be parsed as constant.*#';
50+
51+
$this->setExpectedExceptionRegExp('\Symfony\Component\Yaml\Exception\ParseException', $expectedMessage);
52+
53+
Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
54+
}
55+
3756
/**
3857
* @group legacy
3958
* @dataProvider getTestsForParseWithMapObjects
@@ -431,6 +450,17 @@ public function getTestsForParseWithMapObjects()
431450
);
432451
}
433452

453+
public function getTestsForParsePhpConstants()
454+
{
455+
return array(
456+
array('!php/const:WRONG_CONSTANT', null),
457+
array('!php/const:Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
458+
array('!php/const:PHP_INT_MAX', PHP_INT_MAX),
459+
array('[!php/const:PHP_INT_MAX]', array(PHP_INT_MAX)),
460+
array('{ foo: !php/const:PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
461+
);
462+
}
463+
434464
public function getTestsForDump()
435465
{
436466
return array(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,22 @@ public function getInvalidBinaryData()
12011201
),
12021202
);
12031203
}
1204+
1205+
public function testParsePhpConstant()
1206+
{
1207+
$yaml = 'foo: !php/const:PHP_INT_MAX';
1208+
$php = array('foo' => PHP_INT_MAX);
1209+
1210+
$this->assertSame($php, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
1211+
}
1212+
1213+
public function testParsePhpConstantThrowsExceptionOnInvalidType()
1214+
{
1215+
$expectedMessage = '#The string "!php/const:PHP_INT_MAX" could not be parsed as constant.*#';
1216+
$this->setExpectedExceptionRegExp('\Symfony\Component\Yaml\Exception\ParseException', $expectedMessage);
1217+
1218+
$this->parser->parse('foo: !php/const:PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
1219+
}
12041220
}
12051221

12061222
class B

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Yaml
2828
const PARSE_DATETIME = 32;
2929
const DUMP_OBJECT_AS_MAP = 64;
3030
const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
31+
const PARSE_CONSTANT = 256;
3132

3233
/**
3334
* Parses YAML into a PHP value.

0 commit comments

Comments
 (0)
0