[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / 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 */
48 if (!function_exists('yaml_decode')) {
49 function yaml_decode($input) {
50 // Si PHP4
51 if (_LIB_YAML == 'spyc-php4') {
52 require_once _DIR_PLUGIN_YAML.'spyc/spyc-php4.php';
53 return Spyc::YAMLLoad($input);
54 }
55 // test temporaire
56 if (_LIB_YAML == 'spyc') {
57 require_once _DIR_PLUGIN_YAML.'spyc/spyc.php';
58 return Spyc::YAMLLoad($input);
59 }
60
61 require_once _DIR_PLUGIN_YAML.'inc/yaml_sfyaml.php';
62 return yaml_sfyaml_decode($input);
63 }
64 }
65
66 /*
67 * Decode un fichier en utilisant yaml_decode
68 * @param string $fichier
69 */
70 function yaml_decode_file($fichier){
71 $yaml = '';
72 $retour = false;
73
74 lire_fichier($fichier, $yaml);
75 // Si on recupere bien quelque chose
76 if ($yaml){
77 $retour = yaml_decode($yaml);
78 }
79
80 return $retour;
81 }
82
83 /*
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
88 */
89
90 function yaml_charger_inclusions($tableau){
91 if (is_array($tableau)){
92 $retour = array();
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)));
98 else
99 $retour = array_merge($retour,array($cle => $valeur));
100 }
101 return $retour;
102 }
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))));
105 else
106 return $tableau;
107 }
108
109 ?>