Development

/branches/1.1/lib/yaml/sfYaml.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/yaml/sfYaml.class.php

Revision 18492, 2.4 kB (checked in by nicolas, 6 years ago)

fixed exception page should execute html/javascript code when using html format - closes #5743

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
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 /**
12  * sfYaml class.
13  *
14  * @package    symfony
15  * @subpackage util
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfYaml
20 {
21   /**
22    * Load YAML into a PHP array statically
23    *
24    * The load method, when supplied with a YAML stream (string or file),
25    * will do its best to convert YAML in a file into a PHP array.
26    *
27    *  Usage:
28    *  <code>
29    *   $array = sfYaml::load('config.yml');
30    *   print_r($array);
31    *  </code>
32    *
33    * @param string $input Path of YAML file or string containing YAML
34    *
35    * @return array
36    */
37   public static function load($input)
38   {
39     $file = '';
40
41     // if input is a file, process it
42     if (strpos($input, "\n") === false && is_file($input))
43     {
44       $file = $input;
45
46       ob_start();
47       $retval = include($input);
48       $content = ob_get_clean();
49
50       // if an array is returned by the config file assume it's in plain php form else in yaml
51       $input = is_array($retval) ? $retval : $content;
52     }
53
54     // if an array is returned by the config file assume it's in plain php form else in yaml
55     if (is_array($input))
56     {
57       return $input;
58     }
59
60     require_once dirname(__FILE__).'/sfYamlParser.class.php';
61
62     $yaml = new sfYamlParser();
63
64     try
65     {
66       $ret = $yaml->parse($input);
67     }
68     catch (Exception $e)
69     {
70       throw new InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ? sprintf('file "%s"', $file) : 'string', $e->getMessage()));
71     }
72
73     return $ret;
74   }
75
76   /**
77    * Dump YAML from PHP array statically
78    *
79    * The dump method, when supplied with an array, will do its best
80    * to convert the array into friendly YAML.
81    *
82    * @param  array    $array   PHP array
83    * @param  integer  $inline  The level where you switch to inline YAML
84    *
85    * @return string
86    */
87   public static function dump($array, $inline = 2)
88   {
89     require_once dirname(__FILE__).'/sfYamlDumper.class.php';
90
91     $yaml = new sfYamlDumper();
92
93     return $yaml->dump($array, $inline);
94   }
95 }
96
97 /**
98  * Wraps echo to automatically provide a newline.
99  *
100  * @param string The string to echo with new line
101  */
102 function echoln($string)
103 {
104   echo $string."\n";
105 }
106
Note: See TracBrowser for help on using the browser.