Overview

Packages

  • Yaml

Classes

  • Horde_Yaml
  • Horde_Yaml_Dumper
  • Horde_Yaml_Exception
  • Horde_Yaml_Loader
  • Horde_Yaml_Node
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Horde YAML package
  4:  *
  5:  * This package is heavily inspired by the Spyc PHP YAML
  6:  * implementation (http://spyc.sourceforge.net/), and portions are
  7:  * copyright 2005-2006 Chris Wanstrath.
  8:  *
  9:  * @author   Chris Wanstrath <chris@ozmm.org>
 10:  * @author   Chuck Hagenbuch <chuck@horde.org>
 11:  * @author   Mike Naberezny <mike@maintainable.com>
 12:  * @license  http://www.horde.org/licenses/bsd BSD
 13:  * @category Horde
 14:  * @package  Yaml
 15:  */
 16: 
 17: /**
 18:  * Horde YAML parser.
 19:  *
 20:  * This class can be used to read a YAML file and convert its contents
 21:  * into a PHP array. The native PHP parser supports a limited
 22:  * subsection of the YAML spec, but if the syck extension is present,
 23:  * that will be used for parsing.
 24:  *
 25:  * @category Horde
 26:  * @package  Yaml
 27:  */
 28: class Horde_Yaml
 29: {
 30:     /**
 31:      * Callback used for alternate YAML loader, typically exported
 32:      * by a faster PHP extension.  This function's first argument
 33:      * must accept a string with YAML content.
 34:      *
 35:      * @var callback
 36:      */
 37:     public static $loadfunc = 'syck_load';
 38: 
 39:     /**
 40:      * Callback used for alternate YAML dumper, typically exported
 41:      * by a faster PHP extension.  This function's first argument
 42:      * must accept a mixed variable to be dumped.
 43:      *
 44:      * @var callback
 45:      */
 46:     public static $dumpfunc = 'syck_dump';
 47: 
 48:     /**
 49:      * Whitelist of classes that can be instantiated automatically
 50:      * when loading YAML docs that include serialized PHP objects.
 51:      *
 52:      * @var array
 53:      */
 54:     public static $allowedClasses = array('ArrayObject');
 55: 
 56:     /**
 57:      * Load a string containing YAML and parse it into a PHP array.
 58:      * Returns an empty array on failure.
 59:      *
 60:      * @param  string  $yaml   String containing YAML
 61:      * @return array           PHP array representation of YAML content
 62:      */
 63:     public static function load($yaml)
 64:     {
 65:         if (!is_string($yaml) || !strlen($yaml)) {
 66:             $msg = 'YAML to parse must be a string and cannot be empty.';
 67:             throw new InvalidArgumentException($msg);
 68:         }
 69: 
 70:         if (is_callable(self::$loadfunc)) {
 71:             return call_user_func(self::$loadfunc, $yaml);
 72:             return is_array($array) ? $array : array();
 73:         }
 74: 
 75:         if (strpos($yaml, "\r") !== false) {
 76:             $yaml = str_replace(array("\r\n", "\r"), array("\n", "\n"), $yaml);
 77:         }
 78:         $lines = explode("\n", $yaml);
 79:         $loader = new Horde_Yaml_Loader;
 80: 
 81:         while (list(,$line) = each($lines)) {
 82:             $loader->parse($line);
 83:         }
 84: 
 85:         return $loader->toArray();
 86:     }
 87: 
 88:     /**
 89:      * Load a file containing YAML and parse it into a PHP array.
 90:      *
 91:      * If the file cannot be opened, an exception is thrown.  If the
 92:      * file is read but parsing fails, an empty array is returned.
 93:      *
 94:      * @param  string  $filename     Filename to load
 95:      * @return array                 PHP array representation of YAML content
 96:      * @throws IllegalArgumentException  If $filename is invalid
 97:      * @throws Horde_Yaml_Exception  If the file cannot be opened.
 98:      */
 99:     public static function loadFile($filename)
100:     {
101:         if (!is_string($filename) || !strlen($filename)) {
102:             $msg = 'Filename must be a string and cannot be empty';
103:             throw new InvalidArgumentException($msg);
104:         }
105: 
106:         $stream = @fopen($filename, 'rb');
107:         if (!$stream) {
108:             throw new Horde_Yaml_Exception('Failed to open file: ', error_get_last());
109:         }
110: 
111:         return self::loadStream($stream);
112:     }
113: 
114:     /**
115:      * Load YAML from a PHP stream resource.
116:      *
117:      * @param  resource  $stream     PHP stream resource
118:      * @return array                 PHP array representation of YAML content
119:      */
120:     public static function loadStream($stream)
121:     {
122:         if (! is_resource($stream) || get_resource_type($stream) != 'stream') {
123:             throw new InvalidArgumentException('Stream must be a stream resource');
124:         }
125: 
126:         if (is_callable(self::$loadfunc)) {
127:             return call_user_func(self::$loadfunc, stream_get_contents($stream));
128:         }
129: 
130:         $loader = new Horde_Yaml_Loader;
131:         while (!feof($stream)) {
132:             $loader->parse(stream_get_line($stream, 100000, "\n"));
133:         }
134: 
135:         return $loader->toArray();
136:     }
137: 
138:     /**
139:      * Dump a PHP array to YAML.
140:      *
141:      * The dump method, when supplied with an array, will do its best
142:      * to convert the array into friendly YAML.
143:      *
144:      * @param  array|Traversable  $array     PHP array or traversable object
145:      * @param  integer            $options   Options to pass to dumper
146:      * @return string                        YAML representation of $value
147:      */
148:     public static function dump($value, $options = array())
149:     {
150:         if (is_callable(self::$dumpfunc)) {
151:             return call_user_func(self::$dumpfunc, $value);
152:         }
153: 
154:         $dumper = new Horde_Yaml_Dumper();
155:         return $dumper->dump($value, $options);
156:     }
157: 
158: }
159: 
API documentation generated by ApiGen