| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
* @param string $input Path of YAML file or string containing YAML |
|---|
| 34 |
* |
|---|
| 35 |
* @return array |
|---|
| 36 |
*/ |
|---|
| 37 |
public static function load($input) |
|---|
| 38 |
{ |
|---|
| 39 |
$file = ''; |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
if (strpos($input, "\n") === false && is_file($input)) |
|---|
| 43 |
{ |
|---|
| 44 |
$file = $input; |
|---|
| 45 |
|
|---|
| 46 |
ob_start(); |
|---|
| 47 |
$retval = include($input); |
|---|
| 48 |
$content = ob_get_clean(); |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
$input = is_array($retval) ? $retval : $content; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
if (is_array($input)) |
|---|
| 56 |
{ |
|---|
| 57 |
return $input; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
require_once dirname(__FILE__).'/sfYamlParser.class.php'; |
|---|
| 61 |
|
|---|
| 62 |
$yaml = new sfYamlParser(); |
|---|
| 63 |
|
|---|
| 64 |
try |
|---|
| 65 |
{ |
|---|
| 66 |
$ret = $yaml->parse($input); |
|---|
| 67 |
} |
|---|
| 68 |
catch (Exception $e) |
|---|
| 69 |
{ |
|---|
| 70 |
throw new InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ? sprintf('file "%s"', $file) : 'string', $e->getMessage())); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
return $ret; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
* Dump YAML from PHP array statically |
|---|
| 78 |
* |
|---|
| 79 |
* The dump method, when supplied with an array, will do its best |
|---|
| 80 |
* to convert the array into friendly YAML. |
|---|
| 81 |
* |
|---|
| 82 |
* @param array $array PHP array |
|---|
| 83 |
* @param integer $inline The level where you switch to inline YAML |
|---|
| 84 |
* |
|---|
| 85 |
* @return string |
|---|
| 86 |
*/ |
|---|
| 87 |
public static function dump($array, $inline = 2) |
|---|
| 88 |
{ |
|---|
| 89 |
require_once dirname(__FILE__).'/sfYamlDumper.class.php'; |
|---|
| 90 |
|
|---|
| 91 |
$yaml = new sfYamlDumper(); |
|---|
| 92 |
|
|---|
| 93 |
return $yaml->dump($array, $inline); |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
function echoln($string) |
|---|
| 103 |
{ |
|---|
| 104 |
echo $string."\n"; |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|