source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/Symfony/Component/Yaml/Yaml.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 2.4 KB
Line 
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
12namespace Symfony\Component\Yaml;
13
14use 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 */
23class Yaml
24{
25    /**
26     * Parses YAML into a PHP array.
27     *
28     * The parse method, when supplied with a YAML stream (string or file),
29     * will do its best to convert YAML in a file into a PHP array.
30     *
31     *  Usage:
32     *  <code>
33     *   $array = Yaml::parse('config.yml');
34     *   print_r($array);
35     *  </code>
36     *
37     * @param string $input Path to a YAML file or a string containing YAML
38     *
39     * @return array The YAML converted to a PHP array
40     *
41     * @throws \InvalidArgumentException If the YAML is not valid
42     *
43     * @api
44     */
45    static public function parse($input)
46    {
47        $file = '';
48
49        // if input is a file, process it
50        if (strpos($input, "\n") === false && is_file($input) && is_readable($input)) {
51            $file = $input;
52
53            ob_start();
54            $retval = include($input);
55            $content = ob_get_clean();
56
57            // if an array is returned by the config file assume it's in plain php form else in YAML
58            $input = is_array($retval) ? $retval : $content;
59        }
60
61        // if an array is returned by the config file assume it's in plain php form else in YAML
62        if (is_array($input)) {
63            return $input;
64        }
65
66        $yaml = new Parser();
67
68        try {
69            return $yaml->parse($input);
70        } catch (ParseException $e) {
71            if ($file) {
72                $e->setParsedFile($file);
73            }
74
75            throw $e;
76        }
77    }
78
79    /**
80     * Dumps a PHP array to a YAML string.
81     *
82     * The dump method, when supplied with an array, will do its best
83     * to convert the array into friendly YAML.
84     *
85     * @param array   $array PHP array
86     * @param integer $inline The level where you switch to inline YAML
87     *
88     * @return string A YAML string representing the original PHP array
89     *
90     * @api
91     */
92    static public function dump($array, $inline = 2)
93    {
94        $yaml = new Dumper();
95
96        return $yaml->dump($array, $inline);
97    }
98}
Note: See TracBrowser for help on using the repository browser.