3 if (!defined("_ECRIRE_INC_VERSION")) return;
5 # textwheel fournit yaml_decode() aussi...
6 # include_spip('inc/yaml-mini');
8 # wrapper de la class sfYAML pour SPIP
10 # fournit deux fonctions pour YAML,
11 # analogues a json_encode() et json_decode
13 # Regle de dev: ne pas se rendre dependant de la lib sous-jacente
16 if (version_compare(PHP_VERSION
, '5.0.0', '<'))
17 define('_LIB_YAML','spyc-php4');
19 // temporaire le temps de tester spyc
20 define('_LIB_YAML','sfyaml');
21 #define('_LIB_YAML','spyc');
24 * Encode n'importe quelle structure en yaml
28 function yaml_encode($struct, $opt = array()) {
30 if (_LIB_YAML
== 'spyc-php4') {
31 require_once _DIR_PLUGIN_YAML
.'spyc/spyc-php4.php';
32 return Spyc
::YAMLDump($struct);
35 if (_LIB_YAML
== 'spyc') {
36 require_once _DIR_PLUGIN_YAML
.'spyc/spyc.php';
37 return Spyc
::YAMLDump($struct);
40 require_once _DIR_PLUGIN_YAML
.'inc/yaml_sfyaml.php';
41 return yaml_sfyaml_encode($struct, $opt);
45 * Decode un texte yaml, renvoie la structure
46 * @param string $input
48 if (!function_exists('yaml_decode')) {
49 function yaml_decode($input) {
51 if (_LIB_YAML
== 'spyc-php4') {
52 require_once _DIR_PLUGIN_YAML
.'spyc/spyc-php4.php';
53 return Spyc
::YAMLLoad($input);
56 if (_LIB_YAML
== 'spyc') {
57 require_once _DIR_PLUGIN_YAML
.'spyc/spyc.php';
58 return Spyc
::YAMLLoad($input);
61 require_once _DIR_PLUGIN_YAML
.'inc/yaml_sfyaml.php';
62 return yaml_sfyaml_decode($input);
67 * Decode un fichier en utilisant yaml_decode
68 * @param string $fichier
70 function yaml_decode_file($fichier){
74 lire_fichier($fichier, $yaml);
75 // Si on recupere bien quelque chose
77 $retour = yaml_decode($yaml);
84 * Charge les inclusions de YAML dans un tableau
85 * Les inclusions sont indiquees dans le tableau via la valeur 'inclure:rep/fichier.yaml' ou rep indique le chemin relatif.
86 * On passe donc par find_in_path() pour trouver le fichier
87 * @param array $tableau
90 function yaml_charger_inclusions($tableau){
91 if (is_array($tableau)){
93 foreach($tableau as $cle => $valeur) {
94 if (is_string($valeur) && substr($valeur,0,8)=='inclure:' && substr($valeur,-5)=='.yaml')
95 $retour = array_merge($retour,yaml_charger_inclusions(yaml_decode_file(find_in_path(substr($valeur,8)))));
96 elseif (is_array($valeur))
97 $retour = array_merge($retour,array($cle => yaml_charger_inclusions($valeur)));
99 $retour = array_merge($retour,array($cle => $valeur));
103 elseif (is_string($tableau) && substr($tableau,0,8)=='inclure:' && substr($tableau,-5)=='.yaml')
104 return yaml_charger_inclusions(yaml_decode_file(find_in_path(substr($tableau,8))));