[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: TailFilter.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/BaseParamFilterReader.php'; 24 25 /** 26 * Reads the last <code>n</code> lines of a stream. (Default is last10 lines.) 27 * 28 * Example: 29 * 30 * <pre><tailfilter lines="3" /></pre> 31 * 32 * Or: 33 * 34 * <pre><filterreader classname="phing.filters.TailFilter"> 35 * <param name="lines" value="3"> 36 * </filterreader></pre> 37 * 38 * @author <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a> 39 * @author hans lellelid, hans@velum.net 40 * @copyright © 2003 seasonfive. All rights reserved 41 * @version $Revision: 1.7 $ 42 * @see BaseParamFilterReader 43 * @package phing.filters 44 */ 45 class TailFilter extends BaseParamFilterReader implements ChainableReader { 46 47 /** 48 * Parameter name for the number of lines to be returned. 49 * @var string 50 */ 51 const LINES_KEY = "lines"; 52 53 54 /** 55 * Number of lines to be returned in the filtered stream. 56 * @var integer 57 */ 58 private $_lines = 10; 59 60 /** 61 * Array to hold lines. 62 * @var array 63 */ 64 private $_lineBuffer = array(); 65 66 /** 67 * Returns the last n lines of a file. 68 * @param int $len Num chars to read. 69 * @return mixed The filtered buffer or -1 if EOF. 70 */ 71 function read($len = null) { 72 73 while ( ($buffer = $this->in->read($len)) !== -1 ) { 74 // Remove the last "\n" from buffer for 75 // prevent explode to add an empty cell at 76 // the end of array 77 $buffer= trim($buffer, "\n"); 78 79 $lines = explode("\n", $buffer); 80 81 if ( count($lines) >= $this->_lines ) { 82 // Buffer have more (or same) number of lines than needed. 83 // Fill lineBuffer with the last "$this->_lines" lasts ones. 84 $off = count($lines)-$this->_lines; 85 $this->_lineBuffer = array_slice($lines, $off); 86 } else { 87 // Some new lines ... 88 // Prepare space for insert these new ones 89 $this->_lineBuffer = array_slice($this->_lineBuffer, count($lines)-1); 90 $this->_lineBuffer = array_merge($this->_lineBuffer, $lines); 91 } 92 } 93 94 if ( empty($this->_lineBuffer) ) 95 $ret = -1; 96 else { 97 $ret = implode("\n", $this->_lineBuffer); 98 $this->_lineBuffer = array(); 99 } 100 101 return $ret; 102 } 103 104 /** 105 * Sets the number of lines to be returned in the filtered stream. 106 * 107 * @param integer $lines the number of lines to be returned in the filtered stream. 108 */ 109 function setLines($lines) { 110 $this->_lines = (int) $lines; 111 } 112 113 /** 114 * Returns the number of lines to be returned in the filtered stream. 115 * 116 * @return integer The number of lines to be returned in the filtered stream. 117 */ 118 function getLines() { 119 return $this->_lines; 120 } 121 122 /** 123 * Creates a new TailFilter using the passed in 124 * Reader for instantiation. 125 * 126 * @param object A Reader object providing the underlying stream. 127 * Must not be <code>null</code>. 128 * 129 * @return object A new filter based on this configuration, but filtering 130 * the specified reader. 131 */ 132 function chain(Reader $reader) { 133 $newFilter = new TailFilter($reader); 134 $newFilter->setLines($this->getLines()); 135 $newFilter->setInitialized(true); 136 $newFilter->setProject($this->getProject()); 137 return $newFilter; 138 } 139 140 /** 141 * Scans the parameters list for the "lines" parameter and uses 142 * it to set the number of lines to be returned in the filtered stream. 143 */ 144 private function _initialize() { 145 $params = $this->getParameters(); 146 if ( $params !== null ) { 147 for($i=0, $_i=count($params); $i < $_i; $i++) { 148 if ( self::LINES_KEY == $params[$i]->getName() ) { 149 $this->_lines = (int) $params[$i]->getValue(); 150 break; 151 } 152 } 153 } 154 } 155 } 156 157 ?>
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 |