Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7e8dce

Browse files
committed
[Yaml] support parsing files
1 parent 55a7691 commit c7e8dce

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

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

7+
* added support for passing filenames to `Yaml::parse()` and `Parser::parse()` when using the
8+
`Yaml::PARSE_FILE` flag
9+
710
* the `Dumper`, `Parser`, and `Yaml` classes are marked as final
811

912
* Deprecated the `!php/object:` tag which will be replaced by the

src/Symfony/Component/Yaml/Parser.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ public function parse($value, $flags = 0)
9292
@trigger_error('Using the Yaml::PARSE_KEYS_AS_STRINGS flag is deprecated since version 3.4 as it will be removed in 4.0. Quote your keys when they are evaluable instead.', E_USER_DEPRECATED);
9393
}
9494

95+
if ((bool) (Yaml::PARSE_FILE & $flags)) {
96+
if (!is_file($value)) {
97+
throw new ParseException(sprintf('File "%s" does not exist.', $value));
98+
}
99+
100+
if (!is_readable($value)) {
101+
throw new ParseException(sprintf('File "%s" cannot be read.', $value));
102+
}
103+
104+
$value = file_get_contents($value);
105+
}
106+
95107
if (false === preg_match('//u', $value)) {
96108
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
97109
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
- escapedCharacters
2+
- sfComments
3+
- sfCompact
4+
- sfTests
5+
- sfObjects
6+
- sfMergeKey
7+
- sfQuotes
8+
- YtsAnchorAlias
9+
- YtsBasicTests
10+
- YtsBlockMapping
11+
- YtsDocumentSeparator
12+
- YtsErrorTests
13+
- YtsFlowCollections
14+
- YtsFoldedScalars
15+
- YtsNullsAndEmpties
16+
- YtsSpecificationExamples
17+
- YtsTypeTransfers
18+
- unindentedCollections

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ protected function setUp()
3030
protected function tearDown()
3131
{
3232
$this->parser = null;
33+
34+
chmod(__DIR__.'/Fixtures/not_readable.yml', 0600);
3335
}
3436

3537
/**
@@ -1903,6 +1905,43 @@ public function testPhpConstantTagMappingKeyWithKeysCastToStrings()
19031905

19041906
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT | Yaml::PARSE_KEYS_AS_STRINGS));
19051907
}
1908+
1909+
public function testFilenamesAreParsedAsStringsWithoutFlag()
1910+
{
1911+
$file = __DIR__.'/Fixtures/index.yml';
1912+
1913+
$this->assertSame($file, $this->parser->parse($file));
1914+
}
1915+
1916+
public function testFilesAreParsedWhenPassingTheFlag()
1917+
{
1918+
$this->assertInternalType('array', $this->parser->parse(__DIR__.'/Fixtures/index.yml', Yaml::PARSE_FILE));
1919+
}
1920+
1921+
/**
1922+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1923+
* @expectedExceptionMessageRegExp #^File ".+/Fixtures/nonexistent.yml" does not exist\.$#
1924+
*/
1925+
public function testParsingNonExistentFilesThrowsException()
1926+
{
1927+
$this->parser->parse(__DIR__.'/Fixtures/nonexistent.yml', Yaml::PARSE_FILE);
1928+
}
1929+
1930+
/**
1931+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1932+
* @expectedExceptionMessageRegExp #^File ".+/Fixtures/not_readable.yml" cannot be read\.$#
1933+
*/
1934+
public function testParsingNotReadableFilesThrowsException()
1935+
{
1936+
if ('\\' === DIRECTORY_SEPARATOR) {
1937+
$this->markTestSkipped('chmod is not supported on Windows');
1938+
}
1939+
1940+
$file = __DIR__.'/Fixtures/not_readable.yml';
1941+
chmod($file, 0200);
1942+
1943+
$this->parser->parse($file, Yaml::PARSE_FILE);
1944+
}
19061945
}
19071946

19081947
class B

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Yaml
3333
const PARSE_CONSTANT = 256;
3434
const PARSE_CUSTOM_TAGS = 512;
3535
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
36+
const PARSE_FILE = 4096;
3637

3738
/**
3839
* @deprecated since version 3.4, to be removed in 4.0. Quote your evaluable keys instead.

0 commit comments

Comments
 (0)