1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\Yaml;
13:
14: use Symfony\Component\Yaml\Exception\ParseException;
15:
16: /**
17: * Yaml offers convenience methods to load and dump YAML.
18: *
19: * @author Fabien Potencier <fabien@symfony.com>
20: *
21: * @api
22: */
23: class Yaml
24: {
25: /**
26: * Be warned that PHP support will be removed in Symfony 2.3.
27: *
28: * @deprecated Deprecated since version 2.0, to be removed in 2.3.
29: */
30: public static $enablePhpParsing = false;
31:
32: /**
33: * Enables PHP support when parsing YAML files.
34: *
35: * Be warned that PHP support will be removed in Symfony 2.3.
36: *
37: * @deprecated Deprecated since version 2.0, to be removed in 2.3.
38: */
39: public static function enablePhpParsing()
40: {
41: self::$enablePhpParsing = true;
42: }
43:
44: /**
45: * Sets the PHP support flag when parsing YAML files.
46: *
47: * Be warned that PHP support will be removed in Symfony 2.3.
48: *
49: * @param Boolean $boolean true if PHP parsing support is enabled, false otherwise
50: *
51: * @deprecated Deprecated since version 2.0, to be removed in 2.3.
52: */
53: public static function setPhpParsing($boolean)
54: {
55: self::$enablePhpParsing = (Boolean) $boolean;
56: }
57:
58: /**
59: * Checks if PHP support is enabled when parsing YAML files.
60: *
61: * Be warned that PHP support will be removed in Symfony 2.3.
62: *
63: * @return Boolean true if PHP parsing support is enabled, false otherwise
64: *
65: * @deprecated Deprecated since version 2.0, to be removed in 2.3.
66: */
67: public static function supportsPhpParsing()
68: {
69: return self::$enablePhpParsing;
70: }
71:
72: /**
73: * Parses YAML into a PHP array.
74: *
75: * The parse method, when supplied with a YAML stream (string or file),
76: * will do its best to convert YAML in a file into a PHP array.
77: *
78: * Usage:
79: * <code>
80: * $array = Yaml::parse('config.yml');
81: * print_r($array);
82: * </code>
83: *
84: * As this method accepts both plain strings and file names as an input,
85: * you must validate the input before calling this method. Passing a file
86: * as an input is a deprecated feature and will be removed in 3.0.
87: *
88: * @param string $input Path to a YAML file or a string containing YAML
89: *
90: * @return array The YAML converted to a PHP array
91: *
92: * @throws ParseException If the YAML is not valid
93: *
94: * @api
95: */
96: public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false)
97: {
98: // if input is a file, process it
99: $file = '';
100: if (strpos($input, "\n") === false && is_file($input)) {
101: if (false === is_readable($input)) {
102: throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
103: }
104:
105: $file = $input;
106: if (self::$enablePhpParsing) {
107: ob_start();
108: $retval = include($file);
109: $content = ob_get_clean();
110:
111: // if an array is returned by the config file assume it's in plain php form else in YAML
112: $input = is_array($retval) ? $retval : $content;
113:
114: // if an array is returned by the config file assume it's in plain php form else in YAML
115: if (is_array($input)) {
116: return $input;
117: }
118: } else {
119: $input = file_get_contents($file);
120: }
121: }
122:
123: $yaml = new Parser();
124:
125: try {
126: return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport);
127: } catch (ParseException $e) {
128: if ($file) {
129: $e->setParsedFile($file);
130: }
131:
132: throw $e;
133: }
134: }
135:
136: /**
137: * Dumps a PHP array to a YAML string.
138: *
139: * The dump method, when supplied with an array, will do its best
140: * to convert the array into friendly YAML.
141: *
142: * @param array $array PHP array
143: * @param integer $inline The level where you switch to inline YAML
144: * @param integer $indent The amount of spaces to use for indentation of nested nodes.
145: * @param Boolean $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
146: * @param Boolean $objectSupport true if object support is enabled, false otherwise
147: *
148: * @return string A YAML string representing the original PHP array
149: *
150: * @api
151: */
152: public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
153: {
154: $yaml = new Dumper();
155: $yaml->setIndentation($indent);
156:
157: return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport);
158: }
159: }
160: