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