[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: ReplaceRegexp.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 require_once 'phing/filters/BaseFilterReader.php'; 24 include_once 'phing/filters/ChainableReader.php'; 25 include_once 'phing/types/RegularExpression.php'; 26 27 /** 28 * Performs a regexp find/replace on stream. 29 * <p> 30 * Example:<br> 31 * <pre> 32 * <replaceregexp> 33 * <regexp pattern="\r\n" replace="\n"/> 34 * <regexp pattern="(\w+)\.xml" replace="\1.php" ignoreCase="true"/> 35 * </replaceregexp> 36 * </pre> 37 * 38 * @author Hans Lellelid <hans@xmpl.org> 39 * @version $Revision: 1.5 $ 40 * @package phing.filters 41 */ 42 class ReplaceRegexp extends BaseFilterReader implements ChainableReader { 43 44 /** 45 * @var array RegularExpression[] 46 */ 47 private $regexps = array(); 48 49 /** 50 * Creator method handles nested <regexp> tags. 51 * @return RegularExpression 52 */ 53 function createRegexp() { 54 $num = array_push($this->regexps, new RegularExpression()); 55 return $this->regexps[$num-1]; 56 } 57 58 /** 59 * Sets the current regexps. 60 * (Used when, e.g., cloning/chaining the method.) 61 * @param array RegularExpression[] 62 */ 63 function setRegexps($regexps) { 64 $this->regexps = $regexps; 65 } 66 67 /** 68 * Gets the current regexps. 69 * (Used when, e.g., cloning/chaining the method.) 70 * @return array RegularExpression[] 71 */ 72 function getRegexps() { 73 return $this->regexps; 74 } 75 76 /** 77 * Returns the filtered stream. 78 * The original stream is first read in fully, and the regex replace is performed. 79 * 80 * @param int $len Required $len for Reader compliance. 81 * 82 * @return mixed The filtered stream, or -1 if the end of the resulting stream has been reached. 83 * 84 * @exception IOException if the underlying stream throws an IOException 85 * during reading 86 */ 87 function read($len = null) { 88 89 $buffer = $this->in->read($len); 90 91 if($buffer === -1) { 92 return -1; 93 } 94 95 // perform regex replace here ... 96 foreach($this->regexps as $exptype) { 97 $regexp = $exptype->getRegexp($this->project); 98 try { 99 $buffer = $regexp->replace($buffer); 100 $this->log("Performing regexp replace: /".$regexp->getPattern()."/".$regexp->getReplace()."/g".($regexp->getIgnoreCase() ? 'i' : ''), PROJECT_MSG_VERBOSE); 101 } catch (Exception $e) { 102 // perhaps mismatch in params (e.g. no replace or pattern specified) 103 $this->log("Error performing regexp replace: " . $e->getMessage(), PROJECT_MSG_WARN); 104 } 105 } 106 107 return $buffer; 108 } 109 110 /** 111 * Creates a new ReplaceRegExp filter using the passed in 112 * Reader for instantiation. 113 * 114 * @param Reader $reader A Reader object providing the underlying stream. 115 * Must not be <code>null</code>. 116 * 117 * @return ReplaceRegExp A new filter based on this configuration, but filtering 118 * the specified reader 119 */ 120 function chain(Reader $reader) { 121 $newFilter = new ReplaceRegExp($reader); 122 $newFilter->setProject($this->getProject()); 123 $newFilter->setRegexps($this->getRegexps()); 124 return $newFilter; 125 } 126 127 } 128 129 ?>
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 |