Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | /** | |
3 | * Definition of class PHP_Beautifier_Filter | |
4 | * | |
5 | * PHP version 5 | |
6 | * | |
7 | * LICENSE: This source file is subject to version 3.0 of the PHP license | |
8 | * that is available through the world-wide-web at the following URI: | |
9 | * http://www.php.net/license/3_0.txt. If you did not receive a copy of | |
10 | * the PHP License and are unable to obtain it through the web, please | |
11 | * send a note to license@php.net so we can mail you a copy immediately. | |
12 | * @category PHP | |
13 | * @package PHP_Beautifier | |
14 | * @subpackage Filter | |
15 | * @author Claudio Bustos <cdx@users.sourceforge.com> | |
16 | * @copyright 2004-2006 Claudio Bustos | |
17 | * @link http://pear.php.net/package/PHP_Beautifier | |
18 | * @link http://beautifyphp.sourceforge.net | |
19 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 | |
20 | * @version CVS: $Id:$ | |
21 | */ | |
22 | /** | |
23 | * PHP_Beautifier_Filter | |
24 | * | |
25 | * Definition for creation of Filters | |
26 | * For concrete details, please see {@link PHP_Beautifier_Filter_Default} | |
27 | * @category PHP | |
28 | * @package PHP_Beautifier | |
29 | * @subpackage Filter | |
30 | * @tutorial PHP_Beautifier/Filter/Filter.create.pkg | |
31 | * @author Claudio Bustos <cdx@users.sourceforge.com> | |
32 | * @copyright 2004-2006 Claudio Bustos | |
33 | * @link http://pear.php.net/package/PHP_Beautifier | |
34 | * @link http://beautifyphp.sourceforge.net | |
35 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 | |
36 | * @version Release: 0.1.14 | |
37 | */ | |
38 | abstract class PHP_Beautifier_Filter | |
39 | { | |
40 | /** | |
41 | * Stores a reference to main PHP_Beautifier | |
42 | * @var PHP_Beautifier | |
43 | */ | |
44 | protected $oBeaut; | |
45 | /** | |
46 | * Associative array of functions to use when some token are found | |
47 | * @var array | |
48 | */ | |
49 | protected $aFilterTokenFunctions = array(); | |
50 | /** | |
51 | * Settings for the Filter | |
52 | * @var array | |
53 | */ | |
54 | protected $aSettings = array(); | |
55 | /** | |
56 | * Definition of the settings | |
57 | * Should be an associative array. The keys are the names of settings | |
58 | * and the values are an array with the keys 'type' and ' | |
59 | * @var array | |
60 | */ | |
61 | protected $aSettingsDefinition = array(); | |
62 | /** | |
63 | * Description of the Filter | |
64 | * @var string | |
65 | */ | |
66 | protected $sDescription = 'Filter for PHP_Beautifier'; | |
67 | /** | |
68 | * If a method for parse Tokens of a Filter returns this, the control of the process | |
69 | * is handle by the next Filter | |
70 | */ | |
71 | const BYPASS = 'BYPASS'; | |
72 | /** | |
73 | * Switch to 'turn' on and off the filter | |
74 | * @var bool | |
75 | */ | |
76 | protected $bOn = true; | |
77 | /** | |
78 | * Current token | |
79 | */ | |
80 | protected $aToken = false; | |
81 | /** | |
82 | * Constructor | |
83 | * If you need to overload this (for example, to create a | |
84 | * definition for setting with {@link addSettingDefinition()} | |
85 | * remember call the parent constructor. | |
86 | * <code> | |
87 | * parent::__construct($oBeaut, $aSettings) | |
88 | * </code> | |
89 | * @param PHP_Beautifier | |
90 | * @param array settings for the Filter | |
91 | */ | |
92 | public function __construct(PHP_Beautifier $oBeaut, $aSettings = array()) | |
93 | { | |
94 | $this->oBeaut = $oBeaut; | |
95 | if ($aSettings) { | |
96 | $this->aSettings = $aSettings; | |
97 | } | |
98 | } | |
99 | /** | |
100 | * Add a setting definition | |
101 | * @param string | |
102 | */ | |
103 | protected function addSettingDefinition($sSetting, $sType, $sDescription) | |
104 | { | |
105 | $this->aSettingsDefinition[$sSetting] = array( | |
106 | 'type' => $sType, | |
107 | 'description' => $sDescription | |
108 | ); | |
109 | } | |
110 | /** | |
111 | * return @string | |
112 | */ | |
113 | public function getName() | |
114 | { | |
115 | return str_ireplace('PHP_Beautifier_Filter_', '', get_class($this)); | |
116 | } | |
117 | /** | |
118 | * Turn on the Filter | |
119 | * Use inside the code to beautify | |
120 | * Ex. | |
121 | * <code> | |
122 | * ...some code... | |
123 | * // ArrayNested->on() | |
124 | * ...other code ... | |
125 | * </code> | |
126 | */ | |
127 | final public function on() | |
128 | { | |
129 | $this->bOn = true; | |
130 | } | |
131 | /** | |
132 | * Turn off the Filter | |
133 | * Use inside the code to beautify | |
134 | * Ex. | |
135 | * <code> | |
136 | * ...some code... | |
137 | * // ArrayNested->off() | |
138 | * ...other code ... | |
139 | * </code> | |
140 | */ | |
141 | public function off() | |
142 | { | |
143 | $this->bOn = false; | |
144 | } | |
145 | /** | |
146 | * Get a setting of the Filter | |
147 | * @param string name of setting | |
148 | * @return mixed value of setting or false | |
149 | */ | |
150 | final public function getSetting($sSetting) | |
151 | { | |
152 | return (array_key_exists($sSetting, $this->aSettings)) ? $this->aSettings[$sSetting] : false; | |
153 | } | |
154 | /** | |
155 | * Set a value of a Setting | |
156 | * @param string name of setting | |
157 | * @param mixed value of setting | |
158 | */ | |
159 | final public function setSetting($sSetting, $sValue) | |
160 | { | |
161 | if (array_key_exists($sSetting, $this->aSettings)) { | |
162 | $this->aSettings[$sSetting] = $sValue; | |
163 | } | |
164 | } | |
165 | /** | |
166 | * Function called from {@link PHP_Beautifier::process()} to process the tokens. | |
167 | * | |
168 | * If the received token is one of the keys of {@link $aFilterTokenFunctions} | |
169 | * a function with the same name of the value of that key is called. | |
170 | * If the method doesn't exists, {@link __call()} is called, and return | |
171 | * {@link PHP_Beautifier_Filter::BYPASS}. PHP_Beautifier, now, call the next Filter is its list. | |
172 | * If the method exists, it can return true or {@link PHP_Beautifier_Filter::BYPASS}. | |
173 | * @param array token | |
174 | * @return bool true if the token is processed, false bypass to the next Filter | |
175 | * @see PHP_Beautifier::process() | |
176 | */ | |
177 | public function handleToken($token) | |
178 | { | |
179 | $this->aToken = $token; | |
180 | if (!$this->bOn) { | |
181 | return false; | |
182 | } | |
183 | $sMethod = $sValue = false; | |
184 | if (array_key_exists($token[0], $this->aFilterTokenFunctions)) { | |
185 | $sMethod = $this->aFilterTokenFunctions[$token[0]]; | |
186 | $sValue = $token[1]; | |
187 | } elseif ($this->oBeaut->getTokenFunction($token[0])) { | |
188 | $sMethod = $this->oBeaut->getTokenFunction($token[0]); | |
189 | } | |
190 | $sValue = $token[1]; | |
191 | if ($sMethod) { | |
192 | if ($this->oBeaut->iVerbose > 5) { | |
193 | echo $sMethod . ":" . trim($sValue) . "\n"; | |
194 | } | |
195 | // return false if PHP_Beautifier_Filter::BYPASS | |
196 | return ($this->$sMethod($sValue) !== PHP_Beautifier_Filter::BYPASS); | |
197 | } else { // WEIRD!!! -> Add the same received | |
198 | $this->oBeaut->add($token[1]); | |
199 | PHP_Beautifier_Common::getLog()->log("Add same received:" . trim($token[1]) , PEAR_LOG_DEBUG); | |
200 | return true; | |
201 | } | |
202 | // never go here | |
203 | return false; | |
204 | } | |
205 | /** | |
206 | * @param string metodo | |
207 | * @param array arguments | |
208 | * @return mixed null or {@link PHP_Beautifier_Filter::BYPASS} | |
209 | */ | |
210 | public function __call($sMethod, $aArgs) | |
211 | { | |
212 | return PHP_Beautifier_Filter::BYPASS; | |
213 | } | |
214 | /** | |
215 | * Called from {@link PHP_Beautifier::process()} at the beginning | |
216 | * of the processing | |
217 | * @return void | |
218 | */ | |
219 | public function preProcess() | |
220 | { | |
221 | } | |
222 | /** | |
223 | * Called from {@link PHP_Beautifier::process()} at the end of processing | |
224 | * The post-process must be made in {@link PHP_Beautifier::$aOut} | |
225 | * @return void | |
226 | */ | |
227 | public function postProcess() | |
228 | { | |
229 | } | |
230 | public function __sleep() | |
231 | { | |
232 | return array( | |
233 | 'aSettings' | |
234 | ); | |
235 | } | |
236 | public function getDescription() | |
237 | { | |
238 | return $this->sDescription; | |
239 | } | |
240 | public function __toString() | |
241 | { | |
242 | // php_beautifier->setBeautify(false); | |
243 | $sOut='Filter: '.$this->getName()."\n". | |
244 | "Description: ".$this->getDescription()."\n"; | |
245 | if (!$this->aSettingsDefinition) { | |
246 | $sOut.= "Settings: No declared settings"; | |
247 | } else { | |
248 | $sOut.="Settings:\n"; | |
249 | foreach($this->aSettingsDefinition as $sSetting=>$aSettings) { | |
250 | $sOut.=sprintf("- %s : %s (type %s)\n",$sSetting, $aSettings['description'], $aSettings['type']); | |
251 | } | |
252 | } | |
253 | // php_beautifier->setBeautify(true); | |
254 | return $sOut; | |
255 | } | |
256 | } | |
257 | ?> |