Development

/branches/1.0/lib/util/sfYaml.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/util/sfYaml.class.php

Revision 7888, 2.3 kB (checked in by fabien, 7 years ago)

removed wordwrap option when dumping YAML as it can cause some problems (closes #3083, #2514)

  • 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    * @return array
34    * @param string $input Path of YAML file or string containing YAML
35    */
36   public static function load($input)
37   {
38     $input = self::getIncludeContents($input);
39
40     // if an array is returned by the config file assume it's in plain php form else in yaml
41     if (is_array($input))
42     {
43       return $input;
44     }
45
46     // syck is prefered over spyc
47     if (function_exists('syck_load'))
48     {
49       $retval = syck_load($input);
50
51       return is_array($retval) ? $retval : array();
52     }
53     else
54     {
55       require_once(dirname(__FILE__).'/Spyc.class.php');
56
57       $spyc = new Spyc();
58
59       return $spyc->load($input);
60     }
61   }
62
63   /**
64    * Dump YAML from PHP array statically
65    *
66    * The dump method, when supplied with an array, will do its best
67    * to convert the array into friendly YAML.
68    *
69    * @return string
70    * @param array $array PHP array
71    */
72   public static function dump($array)
73   {
74     require_once(dirname(__FILE__).'/Spyc.class.php');
75
76     $spyc = new Spyc();
77
78     return $spyc->dump($array, false, 0);
79   }
80
81   protected static function getIncludeContents($input)
82   {
83     // if input is a file, process it
84     if (strpos($input, "\n") === false && is_file($input))
85     {
86       ob_start();
87       $retval = include($input);
88       $contents = ob_get_clean();
89
90       // if an array is returned by the config file assume it's in plain php form else in yaml
91       return is_array($retval) ? $retval : $contents;
92     }
93
94     // else return original input
95     return $input;
96   }
97 }
98
99 /**
100  * Wraps echo to automatically provide a newline
101  */
102 function echoln($string)
103 {
104   echo $string."\n";
105 }
106
Note: See TracBrowser for help on using the browser.