Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 offers convenience methods to load and dump YAML.
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
{
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
 
49
  /**
50
   * Loads YAML into a PHP array.
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>
57
   *   $array = sfYaml::load('config.yml');
58
   *   print_r($array);
59
   *  </code>
60
   *
61
   * @param string $input Path of YAML file or string containing YAML
62
   *
63
   * @return array The YAML converted to a PHP array
64
   *
65
   * @throws InvalidArgumentException If the YAML is not valid
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
 
80
      // if an array is returned by the config file assume it's in plain php form else in YAML
81
      $input = is_array($retval) ? $retval : $content;
82
    }
83
 
84
    // if an array is returned by the config file assume it's in plain php form else in YAML
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
  /**
107
   * Dumps a PHP array to a YAML string.
108
   *
109
   * The dump method, when supplied with an array, will do its best
110
   * to convert the array into friendly YAML.
111
   *
112
   * @param array   $array PHP array
113
   * @param integer $inline The level where you switch to inline YAML
114
   *
115
   * @return string A YAML string representing the original PHP array
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
 *
130
 * @param string $string The string to echo with new line
131
 */
132
function echoln($string)
133
{
134
  echo $string."\n";
135
}