3 if (!defined('_ECRIRE_INC_VERSION')) {
7 # textwheel fournit yaml_decode() aussi...
8 # include_spip('inc/yaml-mini');
10 # wrapper de la class sfYAML pour SPIP
12 # fournit deux fonctions pour YAML,
13 # analogues a json_encode() et json_decode
15 # Regle de dev: ne pas se rendre dependant de la lib sous-jacente
18 if (version_compare(PHP_VERSION
, '5.0.0', '<')) {
19 define('_LIB_YAML', 'spyc-php4');
21 // temporaire le temps de tester spyc
22 define('_LIB_YAML', 'sfyaml');
23 #define('_LIB_YAML','spyc');
27 * Encode n'importe quelle structure en yaml
31 function yaml_encode($struct, $opt = array()) {
33 if (_LIB_YAML
== 'spyc-php4') {
34 require_once _DIR_PLUGIN_YAML
.'spyc/spyc-php4.php';
35 return Spyc
::YAMLDump($struct);
38 if (_LIB_YAML
== 'spyc') {
39 require_once _DIR_PLUGIN_YAML
.'spyc/spyc.php';
40 return Spyc
::YAMLDump($struct);
43 require_once _DIR_PLUGIN_YAML
.'inc/yaml_sfyaml.php';
44 return yaml_sfyaml_encode($struct, $opt);
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
52 if (!function_exists('yaml_decode')) {
53 function yaml_decode($input, $show_error = true) {
55 if (_LIB_YAML
== 'spyc-php4') {
56 require_once _DIR_PLUGIN_YAML
.'spyc/spyc-php4.php';
57 return Spyc
::YAMLLoad($input);
60 if (_LIB_YAML
== 'spyc') {
61 require_once _DIR_PLUGIN_YAML
.'spyc/spyc.php';
62 return Spyc
::YAMLLoad($input);
65 require_once _DIR_PLUGIN_YAML
.'inc/yaml_sfyaml.php';
66 return yaml_sfyaml_decode($input, $show_error);
71 * Decode un fichier en utilisant yaml_decode
72 * @param string $fichier
74 function yaml_decode_file($fichier) {
78 lire_fichier($fichier, $yaml);
79 // Si on recupere bien quelque chose
81 $retour = yaml_decode($yaml);
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
93 function yaml_charger_inclusions($tableau) {
94 if (is_array($tableau)) {
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)));
102 $retour = array_merge($retour, array($cle => $valeur));
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))));