[PLUGINS] +yaml
[ptitvelo/web/www.git] / www / plugins / yaml / inc / yaml.php
1 <?php
2
3 if (!defined("_ECRIRE_INC_VERSION")) return;
4
5 # textwheel fournit yaml_decode() aussi...
6 # include_spip('inc/yaml-mini');
7
8 # wrapper de la class sfYAML pour SPIP
9 #
10 # fournit deux fonctions pour YAML,
11 # analogues a json_encode() et json_decode
12 #
13 # Regle de dev: ne pas se rendre dependant de la lib sous-jacente
14
15 // Si on est en PHP4
16 if (version_compare(PHP_VERSION, '5.0.0', '<'))
17 define('_LIB_YAML','spyc-php4');
18 else {
19 // temporaire le temps de tester spyc
20 define('_LIB_YAML','sfyaml');
21 #define('_LIB_YAML','spyc');
22 }
23 /*
24 * Encode n'importe quelle structure en yaml
25 * @param $struct
26 * @return string
27 */
28 function yaml_encode($struct, $opt = array()) {
29 // Si PHP4
30 if (_LIB_YAML == 'spyc-php4') {
31 require_once _DIR_PLUGIN_YAML.'spyc/spyc-php4.php';
32 return Spyc::YAMLDump($struct);
33 }
34 // test temporaire
35 if (_LIB_YAML == 'spyc') {
36 require_once _DIR_PLUGIN_YAML.'spyc/spyc.php';
37 return Spyc::YAMLDump($struct);
38 }
39
40 require_once _DIR_PLUGIN_YAML.'inc/yaml_sfyaml.php';
41 return yaml_sfyaml_encode($struct, $opt);
42 }
43
44 /*
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
48 */
49 if (!function_exists('yaml_decode')) {
50 function yaml_decode($input,$show_error=true) {
51 // Si PHP4
52 if (_LIB_YAML == 'spyc-php4') {
53 require_once _DIR_PLUGIN_YAML.'spyc/spyc-php4.php';
54 return Spyc::YAMLLoad($input);
55 }
56 // test temporaire
57 if (_LIB_YAML == 'spyc') {
58 require_once _DIR_PLUGIN_YAML.'spyc/spyc.php';
59 return Spyc::YAMLLoad($input);
60 }
61
62 require_once _DIR_PLUGIN_YAML.'inc/yaml_sfyaml.php';
63 return yaml_sfyaml_decode($input,$show_error);
64 }
65 }
66
67 /*
68 * Decode un fichier en utilisant yaml_decode
69 * @param string $fichier
70 */
71 function yaml_decode_file($fichier){
72 $yaml = '';
73 $retour = false;
74
75 lire_fichier($fichier, $yaml);
76 // Si on recupere bien quelque chose
77 if ($yaml){
78 $retour = yaml_decode($yaml);
79 }
80
81 return $retour;
82 }
83
84 /*
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
89 */
90
91 function yaml_charger_inclusions($tableau){
92 if (is_array($tableau)){
93 $retour = array();
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)));
99 else
100 $retour = array_merge($retour,array($cle => $valeur));
101 }
102 return $retour;
103 }
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))));
106 else
107 return $tableau;
108 }
109
110 ?>