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
47 * boolean $show_error true: arrete le parsing et retourne erreur en cas d'echec - false: retourne un simple false en cas d'erreur de parsing
49 if (!function_exists('yaml_decode')) {
50 function yaml_decode($input,$show_error=true) {
52 if (_LIB_YAML
== 'spyc-php4') {
53 require_once _DIR_PLUGIN_YAML
.'spyc/spyc-php4.php';
54 return Spyc
::YAMLLoad($input);
57 if (_LIB_YAML
== 'spyc') {
58 require_once _DIR_PLUGIN_YAML
.'spyc/spyc.php';
59 return Spyc
::YAMLLoad($input);
62 require_once _DIR_PLUGIN_YAML
.'inc/yaml_sfyaml.php';
63 return yaml_sfyaml_decode($input,$show_error);
68 * Decode un fichier en utilisant yaml_decode
69 * @param string $fichier
71 function yaml_decode_file($fichier){
75 lire_fichier($fichier, $yaml);
76 // Si on recupere bien quelque chose
78 $retour = yaml_decode($yaml);
85 * Charge les inclusions de YAML dans un tableau
86 * Les inclusions sont indiquees dans le tableau via la valeur 'inclure:rep/fichier.yaml' ou rep indique le chemin relatif.
87 * On passe donc par find_in_path() pour trouver le fichier
88 * @param array $tableau
91 function yaml_charger_inclusions($tableau){
92 if (is_array($tableau)){
94 foreach($tableau as $cle => $valeur) {
95 if (is_string($valeur) && substr($valeur,0,8)=='inclure:' && substr($valeur,-5)=='.yaml')
96 $retour = array_merge($retour,yaml_charger_inclusions(yaml_decode_file(find_in_path(substr($valeur,8)))));
97 elseif (is_array($valeur))
98 $retour = array_merge($retour,array($cle => yaml_charger_inclusions($valeur)));
100 $retour = array_merge($retour,array($cle => $valeur));
104 elseif (is_string($tableau) && substr($tableau,0,8)=='inclure:' && substr($tableau,-5)=='.yaml')
105 return yaml_charger_inclusions(yaml_decode_file(find_in_path(substr($tableau,8))));