home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / phing / tasks / ext / PhpLintTask.php < prev    next >
Encoding:
PHP Script  |  2007-10-07  |  3.9 KB  |  138 lines

  1. <?php
  2. /*
  3.  *    $Id: PhpLintTask.php 244 2007-10-07 18:59:52Z mrook $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information please see
  19.  * <http://phing.info>.
  20.  */
  21.  
  22. require_once 'phing/Task.php';
  23.  
  24. /**
  25.  * A PHP lint task. Checking syntax of one or more PHP source file.
  26.  *
  27.  * @author     Knut Urdalen <knut.urdalen@telio.no>
  28.  * @author     Stefan Priebsch <stefan.priebsch@e-novative.de>
  29.  * @package    phing.tasks.ext
  30.  */
  31. class PhpLintTask extends Task {
  32.  
  33.     protected $file;    // the source file (from xml attribute)
  34.     protected $filesets = array(); // all fileset objects assigned to this task
  35.  
  36.     protected $errorProperty;
  37.     protected $haltOnFailure = false;
  38.     protected $hasErrors = false;
  39.     private $badFiles = array();
  40.  
  41.     /**
  42.      * The haltonfailure property
  43.      * @param boolean $aValue
  44.      */
  45.     public function setHaltOnFailure($aValue) {
  46.         $this->haltOnFailure = $aValue;
  47.     }
  48.  
  49.     /**
  50.      * File to be performed syntax check on
  51.      * @param PhingFile $file
  52.      */
  53.     public function setFile(PhingFile $file) {
  54.         $this->file = $file;
  55.     }
  56.  
  57.     /**
  58.      * Set an property name in which to put any errors.
  59.      * @param string $propname 
  60.      */
  61.     public function setErrorproperty($propname)
  62.     {
  63.         $this->errorProperty = $propname;
  64.     }
  65.  
  66.     /**
  67.      * Nested creator, creates a FileSet for this task
  68.      *
  69.      * @return FileSet The created fileset object
  70.      */
  71.     function createFileSet() {
  72.         $num = array_push($this->filesets, new FileSet());
  73.         return $this->filesets[$num-1];
  74.     }
  75.  
  76.     /**
  77.      * Execute lint check against PhingFile or a FileSet
  78.      */
  79.     public function main() {
  80.         if(!isset($this->file) and count($this->filesets) == 0) {
  81.             throw new BuildException("Missing either a nested fileset or attribute 'file' set");
  82.         }
  83.  
  84.         if($this->file instanceof PhingFile) {
  85.             $this->lint($this->file->getPath());
  86.         } else { // process filesets
  87.             $project = $this->getProject();
  88.             foreach($this->filesets as $fs) {
  89.                 $ds = $fs->getDirectoryScanner($project);
  90.                 $files = $ds->getIncludedFiles();
  91.                 $dir = $fs->getDir($this->project)->getPath();
  92.                 foreach($files as $file) {
  93.                     $this->lint($dir.DIRECTORY_SEPARATOR.$file);
  94.                 }
  95.             }
  96.         }
  97.  
  98.         if ($this->haltOnFailure && $this->hasErrors) throw new BuildException('Syntax error(s) in PHP files: '.implode(', ',$this->badFiles));
  99.     }
  100.  
  101.     /**
  102.      * Performs the actual syntax check
  103.      *
  104.      * @param string $file
  105.      * @return void
  106.      */
  107.     protected function lint($file) {
  108.         $command = 'php -l ';
  109.         if(file_exists($file)) {
  110.             if(is_readable($file)) {
  111.                 $messages = array();
  112.                 exec($command.'"'.$file.'"', $messages);
  113.                 if(!preg_match('/^No syntax errors detected/', $messages[0])) {
  114.                     if (count($messages) > 1) {
  115.                         if ($this->errorProperty) {
  116.                             $this->project->setProperty($this->errorProperty, $messages[1]);
  117.                         }
  118.                         $this->log($messages[1], Project::MSG_ERR);
  119.                     } else {
  120.                         $this->log("Could not parse file", Project::MSG_ERR);
  121.                     }
  122.                     $this->badFiles[] = $file;    
  123.                     $this->hasErrors = true;
  124.                     
  125.                 } else {
  126.                     $this->log($file.': No syntax errors detected', Project::MSG_INFO);
  127.                 }
  128.             } else {
  129.                 throw new BuildException('Permission denied: '.$file);
  130.             }
  131.         } else {
  132.             throw new BuildException('File not found: '.$file);
  133.         }
  134.     }
  135. }
  136.  
  137. ?>
  138.