Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 136 lines (117 sloc) 3.318 kB
c4a6cd2 [yaml] added the YAML component
fabien authored
1 <?php
2
3 /*
4 * This file is part of the symfony package.
5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
a5fcfd0 [yaml] updated the PHPDoc
fabien authored
12 * sfYaml offers convenience methods to load and dump YAML.
c4a6cd2 [yaml] added the YAML component
fabien authored
13 *
14 * @package symfony
15 * @subpackage yaml
16 * @author Fabien Potencier <fabien.potencier@symfony-project.com>
17 * @version SVN: $Id: sfYaml.class.php 8988 2008-05-15 20:24:26Z fabien $
18 */
19 class sfYaml
20 {
11d815e [yaml] started support for the 1.2 YAML specification
fabien authored
21 static protected
22 $spec = '1.2';
23
24 /**
25 * Sets the YAML specification version to use.
26 *
27 * @param string $version The YAML specification version
28 */
29 static public function setSpecVersion($version)
30 {
31 if (!in_array($version, array('1.1', '1.2')))
32 {
33 throw new InvalidArgumentException(sprintf('Version %s of the YAML specifications is not supported', $version));
34 }
35
36 self::$spec = $version;
37 }
38
39 /**
40 * Gets the YAML specification version to use.
41 *
42 * @return string The YAML specification version
43 */
44 static public function getSpecVersion()
45 {
46 return self::$spec;
47 }
48
c4a6cd2 [yaml] added the YAML component
fabien authored
49 /**
57446a2 [yaml] updated the PHPDoc
fabien authored
50 * Loads YAML into a PHP array.
c4a6cd2 [yaml] added the YAML component
fabien authored
51 *
52 * The load method, when supplied with a YAML stream (string or file),
53 * will do its best to convert YAML in a file into a PHP array.
54 *
55 * Usage:
56 * <code>
2fef288 [yaml] another minor capitalization fix
Kris.Wallsmith authored
57 * $array = sfYaml::load('config.yml');
c4a6cd2 [yaml] added the YAML component
fabien authored
58 * print_r($array);
59 * </code>
60 *
61 * @param string $input Path of YAML file or string containing YAML
62 *
a5fcfd0 [yaml] updated the PHPDoc
fabien authored
63 * @return array The YAML converted to a PHP array
64 *
65 * @throws InvalidArgumentException If the YAML is not valid
c4a6cd2 [yaml] added the YAML component
fabien authored
66 */
67 public static function load($input)
68 {
69 $file = '';
70
71 // if input is a file, process it
72 if (strpos($input, "\n") === false && is_file($input))
73 {
74 $file = $input;
75
76 ob_start();
77 $retval = include($input);
78 $content = ob_get_clean();
79
fe5dd12 [yaml] standardized capitalization of sfYaml class in documentation
Kris.Wallsmith authored
80 // if an array is returned by the config file assume it's in plain php form else in YAML
c4a6cd2 [yaml] added the YAML component
fabien authored
81 $input = is_array($retval) ? $retval : $content;
82 }
83
fe5dd12 [yaml] standardized capitalization of sfYaml class in documentation
Kris.Wallsmith authored
84 // if an array is returned by the config file assume it's in plain php form else in YAML
c4a6cd2 [yaml] added the YAML component
fabien authored
85 if (is_array($input))
86 {
87 return $input;
88 }
89
90 require_once dirname(__FILE__).'/sfYamlParser.php';
91
92 $yaml = new sfYamlParser();
93
94 try
95 {
96 $ret = $yaml->parse($input);
97 }
98 catch (Exception $e)
99 {
100 throw new InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ? sprintf('file "%s"', $file) : 'string', $e->getMessage()));
101 }
102
103 return $ret;
104 }
105
106 /**
57446a2 [yaml] updated the PHPDoc
fabien authored
107 * Dumps a PHP array to a YAML string.
c4a6cd2 [yaml] added the YAML component
fabien authored
108 *
109 * The dump method, when supplied with an array, will do its best
110 * to convert the array into friendly YAML.
111 *
a5fcfd0 [yaml] updated the PHPDoc
fabien authored
112 * @param array $array PHP array
113 * @param integer $inline The level where you switch to inline YAML
c4a6cd2 [yaml] added the YAML component
fabien authored
114 *
a5fcfd0 [yaml] updated the PHPDoc
fabien authored
115 * @return string A YAML string representing the original PHP array
c4a6cd2 [yaml] added the YAML component
fabien authored
116 */
117 public static function dump($array, $inline = 2)
118 {
119 require_once dirname(__FILE__).'/sfYamlDumper.php';
120
121 $yaml = new sfYamlDumper();
122
123 return $yaml->dump($array, $inline);
124 }
125 }
126
127 /**
128 * Wraps echo to automatically provide a newline.
129 *
3ccad48 [yaml] fixed PHPdoc (refs #6371 - patch from gimler)
fabien authored
130 * @param string $string The string to echo with new line
c4a6cd2 [yaml] added the YAML component
fabien authored
131 */
132 function echoln($string)
133 {
134 echo $string."\n";
135 }
Something went wrong with that request. Please try again.