[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/phing/filters/ -> HeadFilter.php (source)

   1  <?php
   2  
   3  /*
   4   *  $Id: HeadFilter.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/filters/ChainableReader.php';
  25  
  26  /**
  27   * Reads the first <code>n</code> lines of a stream.
  28   * (Default is first 10 lines.)
  29   * <p>
  30   * Example:
  31   * <pre><headfilter lines="3"/></pre>
  32   * Or:
  33   * <pre><filterreader classname="phing.filters.HeadFilter">
  34   *    <param name="lines" value="3"/>
  35   * </filterreader></pre>
  36   *
  37   * @author    <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
  38   * @author    hans lellelid, hans@velum.net
  39   * @version   $Revision: 1.6 $ $Date: 2004/03/15 14:45:06 $
  40   * @access    public
  41   * @see       FilterReader
  42   * @package   phing.filters
  43   */
  44  class HeadFilter extends BaseParamFilterReader implements ChainableReader {
  45  
  46      /**
  47       * Parameter name for the number of lines to be returned.
  48       */ 
  49      const LINES_KEY = "lines";
  50      
  51      /**
  52       * Number of lines currently read in.
  53       * @var integer
  54       */ 
  55      private $_linesRead = 0;
  56      
  57      /**
  58       * Number of lines to be returned in the filtered stream.
  59       * @var integer
  60       */ 
  61      private $_lines     = 10;
  62  
  63      /**
  64       * Returns first n lines of stream.
  65       * @return the resulting stream, or -1
  66       * if the end of the resulting stream has been reached
  67       * 
  68       * @exception IOException if the underlying stream throws an IOException
  69       * during reading     
  70       */
  71      function read($len = null) {
  72      
  73          if ( !$this->getInitialized() ) {
  74              $this->_initialize();
  75              $this->setInitialized(true);
  76          }
  77          
  78          // note, if buffer contains fewer lines than
  79          // $this->_lines this code will not work.
  80          
  81          if($this->_linesRead < $this->_lines) {
  82          
  83              $buffer = $this->in->read($len);
  84              
  85              if($buffer === -1) {
  86                  return -1;
  87              }
  88              
  89              // now grab first X lines from buffer
  90              
  91              $lines = explode("\n", $buffer);
  92              
  93              $linesCount = count($lines);
  94              
  95              // must account for possibility that the num lines requested could 
  96              // involve more than one buffer read.            
  97              $len = ($linesCount > $this->_lines ? $this->_lines - $this->_linesRead : $linesCount);
  98              $filtered_buffer = implode("\n", array_slice($lines, 0, $len) );
  99              $this->_linesRead += $len;
 100              
 101              return $filtered_buffer;
 102          
 103          }
 104          
 105          return -1; // EOF, since the file is "finished" as far as subsequent filters are concerned.
 106      }    
 107  
 108      /**
 109       * Sets the number of lines to be returned in the filtered stream.
 110       * 
 111       * @param integer $lines the number of lines to be returned in the filtered stream.
 112       */
 113      function setLines($lines) {
 114          $this->_lines = (int) $lines;
 115      }
 116  
 117      /**
 118       * Returns the number of lines to be returned in the filtered stream.
 119       * 
 120       * @return integer The number of lines to be returned in the filtered stream.
 121       */
 122      function getLines() {
 123          return $this->_lines;
 124      }
 125  
 126      /**
 127       * Creates a new HeadFilter using the passed in
 128       * Reader for instantiation.
 129       * 
 130       * @param object A Reader object providing the underlying stream.
 131       *            Must not be <code>null</code>.
 132       * 
 133       * @return object A new filter based on this configuration, but filtering
 134       *         the specified reader.
 135       */
 136      function chain(Reader $reader) {
 137          $newFilter = new HeadFilter($reader);
 138          $newFilter->setLines($this->getLines());
 139          $newFilter->setInitialized(true);
 140          $newFilter->setProject($this->getProject());        
 141          return $newFilter;
 142      }
 143  
 144      /**
 145       * Scans the parameters list for the "lines" parameter and uses
 146       * it to set the number of lines to be returned in the filtered stream.
 147       */
 148      private function _initialize() {
 149          $params = $this->getParameters();
 150          if ( $params !== null ) {
 151              for($i = 0, $_i=count($params) ; $i < $_i; $i++) {
 152                  if ( self::LINES_KEY == $params[$i]->getName() ) {
 153                      $this->_lines = (int) $params[$i]->getValue();
 154                      break;
 155                  }
 156              }
 157          }
 158      }
 159  }
 160  
 161  ?>


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7