| 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 |
* @return array |
|---|
| 34 |
* @param string $input Path of YAML file or string containing YAML |
|---|
| 35 |
*/ |
|---|
| 36 |
public static function load($input) |
|---|
| 37 |
{ |
|---|
| 38 |
$input = self::getIncludeContents($input); |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
if (is_array($input)) |
|---|
| 42 |
{ |
|---|
| 43 |
return $input; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
if (function_exists('syck_load')) |
|---|
| 48 |
{ |
|---|
| 49 |
$retval = syck_load($input); |
|---|
| 50 |
|
|---|
| 51 |
return is_array($retval) ? $retval : array(); |
|---|
| 52 |
} |
|---|
| 53 |
else |
|---|
| 54 |
{ |
|---|
| 55 |
require_once(dirname(__FILE__).'/Spyc.class.php'); |
|---|
| 56 |
|
|---|
| 57 |
$spyc = new Spyc(); |
|---|
| 58 |
|
|---|
| 59 |
return $spyc->load($input); |
|---|
| 60 |
} |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
* Dump YAML from PHP array statically |
|---|
| 65 |
* |
|---|
| 66 |
* The dump method, when supplied with an array, will do its best |
|---|
| 67 |
* to convert the array into friendly YAML. |
|---|
| 68 |
* |
|---|
| 69 |
* @return string |
|---|
| 70 |
* @param array $array PHP array |
|---|
| 71 |
*/ |
|---|
| 72 |
public static function dump($array) |
|---|
| 73 |
{ |
|---|
| 74 |
require_once(dirname(__FILE__).'/Spyc.class.php'); |
|---|
| 75 |
|
|---|
| 76 |
$spyc = new Spyc(); |
|---|
| 77 |
|
|---|
| 78 |
return $spyc->dump($array, false, 0); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
protected static function getIncludeContents($input) |
|---|
| 82 |
{ |
|---|
| 83 |
|
|---|
| 84 |
if (strpos($input, "\n") === false && is_file($input)) |
|---|
| 85 |
{ |
|---|
| 86 |
ob_start(); |
|---|
| 87 |
$retval = include($input); |
|---|
| 88 |
$contents = ob_get_clean(); |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
return is_array($retval) ? $retval : $contents; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
return $input; |
|---|
| 96 |
} |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
function echoln($string) |
|---|
| 103 |
{ |
|---|
| 104 |
echo $string."\n"; |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|