Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 114 lines (99 sloc) 3.102 kb
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
1d31820 @blue-eyes normalized license messages in PHP files
blue-eyes authored
5 *
687f8a7 @fabpot replaced symfony-project.org by symfony.com
fabpot authored
6 * (c) Fabien Potencier <fabien@symfony.com>
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
1d31820 @blue-eyes normalized license messages in PHP files
blue-eyes authored
12 namespace Symfony\Component\Yaml;
13
c3e1d03 @fabpot [Yaml] moved exceptions to their own sub-namespace (added a specific exc...
fabpot authored
14 use Symfony\Component\Yaml\Exception\ParseException;
15
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
16 /**
17 * Yaml offers convenience methods to load and dump YAML.
18 *
687f8a7 @fabpot replaced symfony-project.org by symfony.com
fabpot authored
19 * @author Fabien Potencier <fabien@symfony.com>
a474fe2 @fabpot [Yaml] tagged the guaranteed BC API
fabpot authored
20 *
21 * @api
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
22 */
23 class Yaml
24 {
711eb7f @fabpot fixed CS
fabpot authored
25 public static $enablePhpParsing = false;
797098c @fabpot [Yaml] Yaml::parse() does not evaluate loaded files as PHP files by defa...
fabpot authored
26
711eb7f @fabpot fixed CS
fabpot authored
27 public static function enablePhpParsing()
797098c @fabpot [Yaml] Yaml::parse() does not evaluate loaded files as PHP files by defa...
fabpot authored
28 {
29 self::$enablePhpParsing = true;
30 }
31
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
32 /**
e993626 @fabpot [Yaml] renamed load() to parse()
fabpot authored
33 * Parses YAML into a PHP array.
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
34 *
e993626 @fabpot [Yaml] renamed load() to parse()
fabpot authored
35 * The parse method, when supplied with a YAML stream (string or file),
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
36 * will do its best to convert YAML in a file into a PHP array.
37 *
38 * Usage:
39 * <code>
e993626 @fabpot [Yaml] renamed load() to parse()
fabpot authored
40 * $array = Yaml::parse('config.yml');
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
41 * print_r($array);
42 * </code>
43 *
9b3d27c @hhamon [Yaml] fix some php documentation
hhamon authored
44 * @param string $input Path to a YAML file or a string containing YAML
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
45 *
46 * @return array The YAML converted to a PHP array
47 *
a4d4995 @lanthaler [Yaml] fixed phpdoc (closes symfony/Yaml#3)
lanthaler authored
48 * @throws ParseException If the YAML is not valid
a474fe2 @fabpot [Yaml] tagged the guaranteed BC API
fabpot authored
49 *
50 * @api
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
51 */
711eb7f @fabpot fixed CS
fabpot authored
52 public static function parse($input)
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
53 {
54 // if input is a file, process it
797098c @fabpot [Yaml] Yaml::parse() does not evaluate loaded files as PHP files by defa...
fabpot authored
55 $file = '';
7555587 @makasim [Yaml][Parser] throw an exception if not readable
makasim authored
56 if (strpos($input, "\n") === false && is_file($input)) {
57 if (false === is_readable($input)) {
5a83afd @makasim [Yaml][Parser] changes according review
makasim authored
58 throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
7555587 @makasim [Yaml][Parser] throw an exception if not readable
makasim authored
59 }
60
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
61 $file = $input;
797098c @fabpot [Yaml] Yaml::parse() does not evaluate loaded files as PHP files by defa...
fabpot authored
62 if (self::$enablePhpParsing) {
63 ob_start();
64 $retval = include($file);
65 $content = ob_get_clean();
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
66
797098c @fabpot [Yaml] Yaml::parse() does not evaluate loaded files as PHP files by defa...
fabpot authored
67 // if an array is returned by the config file assume it's in plain php form else in YAML
68 $input = is_array($retval) ? $retval : $content;
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
69
797098c @fabpot [Yaml] Yaml::parse() does not evaluate loaded files as PHP files by defa...
fabpot authored
70 // if an array is returned by the config file assume it's in plain php form else in YAML
71 if (is_array($input)) {
72 return $input;
73 }
74 } else {
75 $input = file_get_contents($file);
76 }
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
77 }
78
79 $yaml = new Parser();
80
81 try {
c3e1d03 @fabpot [Yaml] moved exceptions to their own sub-namespace (added a specific exc...
fabpot authored
82 return $yaml->parse($input);
83 } catch (ParseException $e) {
84 if ($file) {
85 $e->setParsedFile($file);
86 }
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
87
c3e1d03 @fabpot [Yaml] moved exceptions to their own sub-namespace (added a specific exc...
fabpot authored
88 throw $e;
89 }
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
90 }
91
92 /**
93 * Dumps a PHP array to a YAML string.
94 *
95 * The dump method, when supplied with an array, will do its best
96 * to convert the array into friendly YAML.
97 *
e84e637 @fabpot fixed phpdoc @param alignment
fabpot authored
98 * @param array $array PHP array
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
99 * @param integer $inline The level where you switch to inline YAML
a129755 @sun Added Yaml\Dumper::setIndentation() method to allow a custom indentation...
sun authored
100 * @param integer $indent The amount of spaces to use for indentation of nested nodes.
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
101 *
102 * @return string A YAML string representing the original PHP array
a474fe2 @fabpot [Yaml] tagged the guaranteed BC API
fabpot authored
103 *
104 * @api
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
105 */
a129755 @sun Added Yaml\Dumper::setIndentation() method to allow a custom indentation...
sun authored
106 public static function dump($array, $inline = 2, $indent = 4)
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
107 {
108 $yaml = new Dumper();
a129755 @sun Added Yaml\Dumper::setIndentation() method to allow a custom indentation...
sun authored
109 $yaml->setIndentation($indent);
88d5c1b @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
110
111 return $yaml->dump($array, $inline);
112 }
113 }
Something went wrong with that request. Please try again.