[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: ReplaceTokens.php 3076 2006-12-18 08:52:12Z fabien $ 5 * 6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 17 * 18 * This software consists of voluntary contributions made by many individuals 19 * and is licensed under the LGPL. For more information please see 20 * <http://phing.info>. 21 */ 22 23 include_once 'phing/filters/BaseParamFilterReader.php'; 24 include_once 'phing/types/TokenSource.php'; 25 include_once 'phing/filters/ChainableReader.php'; 26 27 /* 28 * Replaces tokens in the original input with user-supplied values. 29 * 30 * Example: 31 * 32 * <pre><replacetokens begintoken="#" endtoken="#">; 33 * <token key="DATE" value="${TODAY}"/> 34 * </replacetokens></pre> 35 * 36 * Or: 37 * 38 * <pre><filterreader classname="phing.filters.ReplaceTokens"> 39 * <param type="tokenchar" name="begintoken" value="#"/> 40 * <param type="tokenchar" name="endtoken" value="#"/> 41 * <param type="token" name="DATE" value="${TODAY}"/> 42 * </filterreader></pre> 43 * 44 * @author <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a> 45 * @author hans lellelid, hans@velum.net 46 * @version $Revision: 1.14 $ $Date: 2005/06/16 15:09:10 $ 47 * @access public 48 * @see BaseParamFilterReader 49 * @package phing.filters 50 */ 51 class ReplaceTokens extends BaseParamFilterReader implements ChainableReader { 52 53 /** 54 * Default "begin token" character. 55 * @var string 56 */ 57 const DEFAULT_BEGIN_TOKEN = "@"; 58 59 /** 60 * Default "end token" character. 61 * @var string 62 */ 63 const DEFAULT_END_TOKEN = "@"; 64 65 /** 66 * [Deprecated] Data that must be read from, if not null. 67 * @var string 68 */ 69 private $_queuedData = null; 70 71 /** 72 * Array to hold the replacee-replacer pairs (String to String). 73 * @var array 74 */ 75 private $_tokens = array(); 76 77 /** 78 * Array to hold the token sources that make tokens from 79 * different sources available 80 * @var array 81 */ 82 private $_tokensources = array(); 83 84 /** 85 * Array holding all tokens given directly to the Filter and 86 * those passed via a TokenSource. 87 * @var array 88 */ 89 private $_alltokens = null; 90 91 /** 92 * Character marking the beginning of a token. 93 * @var string 94 */ 95 private $_beginToken = "@"; // self::DEFAULT_BEGIN_TOKEN; 96 97 /** 98 * Character marking the end of a token. 99 * @var string 100 */ 101 private $_endToken = "@"; //self::DEFAULT_END_TOKEN; 102 103 /** 104 * Performs lookup on key and returns appropriate replacement string. 105 * @param array $matches Array of 1 el containing key to search for. 106 * @return string Text with which to replace key or value of key if none is found. 107 * @access private 108 */ 109 private function replaceTokenCallback($matches) { 110 111 $key = $matches[1]; 112 113 /* Get tokens from tokensource and merge them with the 114 * tokens given directly via build file. This should be 115 * done a bit more elegantly 116 */ 117 if ($this->_alltokens === null) { 118 $this->_alltokens = array(); 119 120 $count = count($this->_tokensources); 121 for ($i = 0; $i < $count; $i++) { 122 $source = $this->_tokensources[$i]; 123 $this->_alltokens = array_merge($this->_alltokens, $source->getTokens()); 124 } 125 126 127 $this->_alltokens = array_merge($this->_tokens, $this->_alltokens); 128 } 129 130 $tokens = $this->_alltokens; 131 132 $replaceWith = null; 133 $count = count($tokens); 134 135 for ($i = 0; $i < $count; $i++) { 136 if ($tokens[$i]->getKey() === $key) { 137 $replaceWith = $tokens[$i]->getValue(); 138 } 139 } 140 141 if ($replaceWith === null) { 142 $replaceWith = $this->_beginToken . $key . $this->_endToken; 143 $this->log("No token defined for key \"".$this->_beginToken . $key . $this->_endToken."\""); 144 } else { 145 $this->log("Replaced \"".$this->_beginToken . $key . $this->_endToken ."\" with \"".$replaceWith."\""); 146 } 147 148 return $replaceWith; 149 } 150 151 /** 152 * Returns stream with tokens having been replaced with appropriate values. 153 * If a replacement value is not found for a token, the token is left in the stream. 154 * 155 * @return mixed filtered stream, -1 on EOF. 156 */ 157 function read($len = null) { 158 if ( !$this->getInitialized() ) { 159 $this->_initialize(); 160 $this->setInitialized(true); 161 } 162 163 // read from next filter up the chain 164 $buffer = $this->in->read($len); 165 166 if($buffer === -1) { 167 return -1; 168 } 169 170 // filter buffer 171 $buffer = preg_replace_callback( 172 "/".preg_quote($this->_beginToken)."([\w\.\-:]+?)".preg_quote($this->_endToken)."/", 173 array($this, 'replaceTokenCallback'), $buffer); 174 175 return $buffer; 176 } 177 178 /** 179 * Sets the "begin token" character. 180 * 181 * @param string $beginToken the character used to denote the beginning of a token. 182 */ 183 function setBeginToken($beginToken) { 184 $this->_beginToken = (string) $beginToken; 185 } 186 187 /** 188 * Returns the "begin token" character. 189 * 190 * @return string The character used to denote the beginning of a token. 191 */ 192 function getBeginToken() { 193 return $this->_beginToken; 194 } 195 196 /** 197 * Sets the "end token" character. 198 * 199 * @param string $endToken the character used to denote the end of a token 200 */ 201 function setEndToken($endToken) { 202 $this->_endToken = (string) $endToken; 203 } 204 205 /** 206 * Returns the "end token" character. 207 * 208 * @return the character used to denote the beginning of a token 209 */ 210 function getEndToken() { 211 return $this->_endToken; 212 } 213 214 /** 215 * Adds a token element to the map of tokens to replace. 216 * 217 * @return object The token added to the map of replacements. 218 * Must not be <code>null</code>. 219 */ 220 function createToken() { 221 $num = array_push($this->_tokens, new Token()); 222 return $this->_tokens[$num-1]; 223 } 224 225 /** 226 * Adds a token source to the sources of this filter. 227 * 228 * @return object A Reference to the source just added. 229 */ 230 function createTokensource() { 231 $num = array_push($this->_tokensources, new TokenSource()); 232 return $this->_tokensources[$num-1]; 233 } 234 235 /** 236 * Sets the map of tokens to replace. 237 * ; used by ReplaceTokens::chain() 238 * 239 * @param array A map (String->String) of token keys to replacement 240 * values. Must not be <code>null</code>. 241 */ 242 function setTokens($tokens) { 243 // type check, error must never occur, bad code of it does 244 if ( !is_array($tokens) ) { 245 throw new Exception("Excpected 'array', got something else"); 246 } 247 248 $this->_tokens = $tokens; 249 } 250 251 /** 252 * Returns the map of tokens which will be replaced. 253 * ; used by ReplaceTokens::chain() 254 * 255 * @return array A map (String->String) of token keys to replacement values. 256 */ 257 function getTokens() { 258 return $this->_tokens; 259 } 260 261 /** 262 * Sets the tokensources to use; used by ReplaceTokens::chain() 263 * 264 * @param array An array of token sources. 265 */ 266 function setTokensources($sources) { 267 // type check 268 if ( !is_array($sources)) { 269 throw new Exception("Exspected 'array', got something else"); 270 } 271 $this->_tokensources = $sources; 272 } 273 274 /** 275 * Returns the token sources used by this filter; used by ReplaceTokens::chain() 276 * 277 * @return array 278 */ 279 function getTokensources() { 280 return $this->_tokensources; 281 } 282 283 /** 284 * Creates a new ReplaceTokens using the passed in 285 * Reader for instantiation. 286 * 287 * @param object A Reader object providing the underlying stream. 288 * Must not be <code>null</code>. 289 * 290 * @return object A new filter based on this configuration, but filtering 291 * the specified reader 292 */ 293 function chain(Reader $reader) { 294 $newFilter = new ReplaceTokens($reader); 295 $newFilter->setProject($this->getProject()); 296 $newFilter->setBeginToken($this->getBeginToken()); 297 $newFilter->setEndToken($this->getEndToken()); 298 $newFilter->setTokens($this->getTokens()); 299 $newFilter->setTokensources($this->getTokensources()); 300 $newFilter->setInitialized(true); 301 return $newFilter; 302 } 303 304 /** 305 * Initializes tokens and loads the replacee-replacer hashtable. 306 * This method is only called when this filter is used through 307 * a <filterreader> tag in build file. 308 */ 309 private function _initialize() { 310 $params = $this->getParameters(); 311 if ( $params !== null ) { 312 for($i = 0 ; $i<count($params) ; $i++) { 313 if ( $params[$i] !== null ) { 314 $type = $params[$i]->getType(); 315 if ( $type === "tokenchar" ) { 316 $name = $params[$i]->getName(); 317 if ( $name === "begintoken" ) { 318 $this->_beginToken = substr($params[$i]->getValue(), 0, 1); 319 } else if ( $name === "endtoken" ) { 320 $this->_endToken = substr($params[$i]->getValue(), 0, 1); 321 } 322 } else if ( $type === "token" ) { 323 $name = $params[$i]->getName(); 324 $value = $params[$i]->getValue(); 325 326 $tok = new Token(); 327 $tok->setKey($name); 328 $tok->setValue($value); 329 330 array_push($this->_tokens, $tok); 331 } else if ( $type === "tokensource" ) { 332 // Store data from nested tags in local array 333 $arr = array(); $subparams = $params[$i]->getParams(); 334 $count = count($subparams); 335 for ($i = 0; $i < $count; $i++) { 336 $arr[$subparams[$i]->getName()] = $subparams[$i]->getValue(); 337 } 338 339 // Create TokenSource 340 $tokensource = new TokenSource(); 341 if (isset($arr["classname"])) 342 $tokensource->setClassname($arr["classname"]); 343 344 // Copy other parameters 1:1 to freshly created TokenSource 345 foreach ($arr as $key => $value) { 346 if (strtolower($key) === "classname") 347 continue; 348 $param = $tokensource->createParam(); 349 $param->setName($key); 350 $param->setValue($value); 351 } 352 353 $this->_tokensources[] = $tokensource; 354 } 355 } 356 } 357 } 358 } 359 } 360 361 /** 362 * Holds a token. 363 */ 364 class Token { 365 366 /** 367 * Token key. 368 * @var string 369 */ 370 private $_key; 371 372 /** 373 * Token value. 374 * @var string 375 */ 376 private $_value; 377 378 /** 379 * Sets the token key. 380 * 381 * @param string $key The key for this token. Must not be <code>null</code>. 382 */ 383 function setKey($key) { 384 $this->_key = (string) $key; 385 } 386 387 /** 388 * Sets the token value. 389 * 390 * @param string $value The value for this token. Must not be <code>null</code>. 391 */ 392 function setValue($value) { 393 $this->_value = (string) $value; 394 } 395 396 /** 397 * Returns the key for this token. 398 * 399 * @return string The key for this token. 400 */ 401 function getKey() { 402 return $this->_key; 403 } 404 405 /** 406 * Returns the value for this token. 407 * 408 * @return string The value for this token. 409 */ 410 function getValue() { 411 return $this->_value; 412 } 413 } 414 415 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |