Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit02d1dea

Browse files
committed
[Yaml] added support for parsing PHP constants
1 parent4223997 commit02d1dea

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Inline
2828
privatestatic$exceptionOnInvalidType =false;
2929
privatestatic$objectSupport =false;
3030
privatestatic$objectForMap =false;
31+
privatestatic$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

@@ -580,6 +582,19 @@ private static function evaluateScalar($scalar, $flags, $references = array())
580582
thrownewParseException('Object support when parsing a YAML file has been disabled.');
581583
}
582584

585+
return;
586+
case0 ===strpos($scalar,'!php/const:'):
587+
if (self::$constantSupport) {
588+
if (defined($const =substr($scalar,11))) {
589+
returnconstant($const);
590+
}
591+
592+
thrownewParseException(sprintf('The constant "%s" is not defined.',$const));
593+
}
594+
if (self::$exceptionOnInvalidType) {
595+
thrownewParseException(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));
596+
}
597+
583598
return;
584599
case0 ===strpos($scalar,'!!float'):
585600
return (float)substr($scalar,8);

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

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

38+
/**
39+
* @dataProvider getTestsForParsePhpConstants
40+
*/
41+
publicfunctiontestParsePhpConstants($yaml,$value)
42+
{
43+
$actual = Inline::parse($yaml, Yaml::PARSE_CONSTANT);
44+
45+
$this->assertSame($value,$actual);
46+
}
47+
48+
publicfunctiongetTestsForParsePhpConstants()
49+
{
50+
returnarray(
51+
array('!php/const:Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
52+
array('!php/const:PHP_INT_MAX',PHP_INT_MAX),
53+
array('[!php/const:PHP_INT_MAX]',array(PHP_INT_MAX)),
54+
array('{ foo: !php/const:PHP_INT_MAX }',array('foo' =>PHP_INT_MAX)),
55+
);
56+
}
57+
58+
/**
59+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
60+
* @expectedExceptionMessage The constant "WRONG_CONSTANT" is not defined
61+
*/
62+
publicfunctiontestParsePhpConstantThrowsExceptionWhenUndefined()
63+
{
64+
Inline::parse('!php/const:WRONG_CONSTANT', Yaml::PARSE_CONSTANT);
65+
}
66+
67+
/**
68+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
69+
* @expectedExceptionMessageRegExp #The string "!php/const:PHP_INT_MAX" could not be parsed as a constant.*#
70+
*/
71+
publicfunctiontestParsePhpConstantThrowsExceptionOnInvalidType()
72+
{
73+
Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
74+
}
75+
3876
/**
3977
* @group legacy
4078
* @dataProvider getTestsForParseWithMapObjects

‎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
constPARSE_DATETIME =32;
2929
constDUMP_OBJECT_AS_MAP =64;
3030
constDUMP_MULTI_LINE_LITERAL_BLOCK =128;
31+
constPARSE_CONSTANT =256;
3132

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp