parse($line); } return $loader->toArray(); } /** * Load a file containing YAML and parse it into a PHP array. * * If the file cannot be opened, an exception is thrown. If the * file is read but parsing fails, an empty array is returned. * * @param string $filename Filename to load * @return array PHP array representation of YAML content * @throws IllegalArgumentException If $filename is invalid * @throws Horde_Yaml_Exception If the file cannot be opened. */ public static function loadFile($filename) { if (!is_string($filename) || !strlen($filename)) { $msg = 'Filename must be a string and cannot be empty'; throw new InvalidArgumentException($msg); } $stream = @fopen($filename, 'rb'); if (!$stream) { throw new Horde_Yaml_Exception('Failed to open file: ', error_get_last()); } return self::loadStream($stream); } /** * Load YAML from a PHP stream resource. * * @param resource $stream PHP stream resource * @return array PHP array representation of YAML content */ public static function loadStream($stream) { if (! is_resource($stream) || get_resource_type($stream) != 'stream') { throw new InvalidArgumentException('Stream must be a stream resource'); } if (is_callable(self::$loadfunc)) { $array = call_user_func(self::$loadfunc, stream_get_contents($stream)); return is_array($array) ? $array : array(); } $loader = new Horde_Yaml_Loader; while (!feof($stream)) { $loader->parse(stream_get_line($stream, 100000, "\n")); } return $loader->toArray(); } /** * Dump a PHP array to YAML. * * The dump method, when supplied with an array, will do its best * to convert the array into friendly YAML. * * @param array|Traversable $array PHP array or traversable object * @param integer $options Options to pass to dumper * @return string YAML representation of $value */ public static function dump($value, $options = array()) { $dumper = new Horde_Yaml_Dumper; return $dumper->dump($value, $options); } }