[SPIP] +spip v3.0.17
[lhc/web/clavette_www.git] / www / plugins-dist / textwheel / engine / textwheelruleset.php
1 <?php
2
3 /*
4 * TextWheel 0.1
5 *
6 * let's reinvent the wheel one last time
7 *
8 * This library of code is meant to be a fast and universal replacement
9 * for any and all text-processing systems written in PHP
10 *
11 * It is dual-licensed for any use under the GNU/GPL2 and MIT licenses,
12 * as suits you best
13 *
14 * (c) 2009 Fil - fil@rezo.net
15 * Documentation & http://zzz.rezo.net/-TextWheel-
16 *
17 * Usage: $wheel = new TextWheel(); echo $wheel->text($text);
18 *
19 */
20
21 if (!defined('_ECRIRE_INC_VERSION')) return;
22
23 require_once dirname(__FILE__)."/textwheelrule.php";
24
25 abstract class TextWheelDataSet {
26 # list of data
27 protected $data = array();
28
29 /**
30 * file finder : can be overloaded in order to use application dependant
31 * path find method
32 *
33 * @param string $file
34 * @param string $path
35 * @return string
36 */
37 protected function findFile(&$file, $path=''){
38 static $default_path;
39
40 // absolute file path ?
41 if (file_exists($file))
42 return $file;
43
44 // file embed with texwheels, relative to calling ruleset
45 if ($path AND file_exists($f = $path.$file))
46 return $f;
47
48 // textwheel default path ?
49 if (!$default_path)
50 $default_path = dirname(__FILE__).'/../wheels/';
51 if (file_exists($f = $default_path.$file))
52 return $f;
53
54 return false;
55 }
56
57 /**
58 * Load a yaml file describing data
59 * @param string $file
60 * @param string $default_path
61 * @return array
62 */
63 protected function loadFile(&$file, $default_path='') {
64 if (!preg_match(',[.]yaml$,i',$file)
65 // external rules
66 OR !$file = $this->findFile($file,$default_path))
67 return array();
68
69 defined('_YAML_EVAL_PHP') || define('_YAML_EVAL_PHP', false);
70 if (!function_exists('yaml_decode')) {
71 if (function_exists('include_spip'))
72 include_spip('inc/yaml-mini');
73 else
74 require_once dirname(__FILE__).'/../inc/yaml.php';
75 }
76 $dataset = yaml_decode(file_get_contents($file));
77
78 if (is_null($dataset))
79 $dataset = array();
80 # throw new DomainException('yaml file is empty, unreadable or badly formed: '.$file.var_export($dataset,true));
81
82 // if a php file with same name exists
83 // include it as it contains callback functions
84 if ($f = preg_replace(',[.]yaml$,i','.php',$file)
85 AND file_exists($f)) {
86 $dataset[] = array('require' => $f, 'priority' => -1000);
87 }
88 return $dataset;
89 }
90
91 }
92
93 class TextWheelRuleSet extends TextWheelDataSet {
94 # sort flag
95 protected $sorted = true;
96
97 /**
98 * Constructor
99 *
100 * @param array|string $ruleset
101 * @param string $filepath
102 */
103 public function TextWheelRuleSet($ruleset = array(), $filepath='') {
104 if ($ruleset)
105 $this->addRules($ruleset, $filepath);
106 }
107
108 /**
109 * public static loader
110 * can be overloaded to use memoization
111 *
112 * @param array $ruleset
113 * @param string $callback
114 * @param string $class
115 * @return class
116 */
117 public static function &loader($ruleset, $callback='', $class='TextWheelRuleSet'){
118
119 $ruleset = new $class($ruleset);
120 if ($callback)
121 $callback($ruleset);
122
123 return $ruleset;
124 }
125 /**
126 * Get an existing named rule in order to override it
127 *
128 * @param string $name
129 * @return string
130 */
131 public function &getRule($name){
132 if (isset($this->data[$name]))
133 return $this->data[$name];
134 $result = null;
135 return $result;
136 }
137
138 /**
139 * get sorted Rules
140 * @return array
141 */
142 public function &getRules(){
143 $this->sort();
144 return $this->data;
145 }
146
147 /**
148 * add a rule
149 *
150 * @param TextWheelRule $rule
151 */
152 public function addRule($rule) {
153 # cast array-rule to object
154 if (is_array($rule))
155 $rule = new TextWheelRule($rule);
156 $this->data[] = $rule;
157 $this->sorted = false;
158 }
159
160 /**
161 * add an list of rules
162 * can be
163 * - an array of rules
164 * - a string filename
165 * - an array of string filename
166 *
167 * @param array|string $rules
168 * @param string $filepath
169 */
170 public function addRules($rules, $filepath='') {
171 // rules can be an array of filename
172 if (is_array($rules) AND is_string(reset($rules))) {
173 foreach($rules as $i=>$filename)
174 $this->addRules($filename);
175 return;
176 }
177
178 // rules can be a string : yaml filename
179 if (is_string($rules)) {
180 $file = $rules; // keep the real filename
181 $rules = $this->loadFile($file, $filepath);
182 $filepath = dirname($file).'/';
183 }
184
185 // rules can be an array of rules
186 if (is_array($rules) AND count($rules)){
187 # cast array-rules to objects
188 foreach ($rules as $i => $rule) {
189 if (is_array($rule))
190 $rules[$i] = new TextWheelRule($rule);
191 // load subwheels when necessary
192 if ($rules[$i]->is_wheel){
193 // subruleset is of the same class as current ruleset
194 $class = get_class($this);
195 $rules[$i]->replace = new $class($rules[$i]->replace, $filepath);
196 }
197 }
198 $this->data = array_merge($this->data, $rules);
199 $this->sorted = false;
200 }
201 }
202
203 /**
204 * Sort rules according to priority and
205 * purge disabled rules
206 *
207 */
208 protected function sort() {
209 if (!$this->sorted) {
210 $rulz = array();
211 foreach($this->data as $index => $rule)
212 if (!$rule->disabled)
213 $rulz[intval($rule->priority)][$index] = $rule;
214 ksort($rulz);
215 $this->data = array();
216 foreach($rulz as $rules)
217 $this->data += $rules;
218
219 $this->sorted = true;
220 }
221 }
222 }