Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *	$Id: PhpLintTask.php 342 2008-01-21 14:49:48Z 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
	protected $interpreter = ''; // php interpreter to use for linting
41
 
42
    /**
43
     * Initialize the interpreter with the Phing property
44
     */
45
    public function __construct() {
46
        $this->setInterpreter(Phing::getProperty('php.interpreter'));
47
    }
48
 
49
	/**
50
	 * Override default php interpreter
51
	 * @todo	Do some sort of checking if the path is correct but would
52
	 *			require traversing the systems executeable path too
53
	 * @param	string	$sPhp
54
	 */
55
	public function setInterpreter($sPhp) {
56
		$this->Interpreter = $sPhp;
57
	}
58
 
59
	/**
60
	 * The haltonfailure property
61
	 * @param boolean $aValue
62
	 */
63
	public function setHaltOnFailure($aValue) {
64
		$this->haltOnFailure = $aValue;
65
	}
66
 
67
	/**
68
	 * File to be performed syntax check on
69
	 * @param PhingFile $file
70
	 */
71
	public function setFile(PhingFile $file) {
72
		$this->file = $file;
73
	}
74
 
75
	/**
76
	 * Set an property name in which to put any errors.
77
	 * @param string $propname
78
	 */
79
	public function setErrorproperty($propname)
80
	{
81
		$this->errorProperty = $propname;
82
	}
83
 
84
	/**
85
	 * Nested creator, creates a FileSet for this task
86
	 *
87
	 * @return FileSet The created fileset object
88
	 */
89
	function createFileSet() {
90
		$num = array_push($this->filesets, new FileSet());
91
		return $this->filesets[$num-1];
92
	}
93
 
94
	/**
95
	 * Execute lint check against PhingFile or a FileSet
96
	 */
97
	public function main() {
98
		if(!isset($this->file) and count($this->filesets) == 0) {
99
			throw new BuildException("Missing either a nested fileset or attribute 'file' set");
100
		}
101
 
102
		if($this->file instanceof PhingFile) {
103
			$this->lint($this->file->getPath());
104
		} else { // process filesets
105
			$project = $this->getProject();
106
			foreach($this->filesets as $fs) {
107
				$ds = $fs->getDirectoryScanner($project);
108
				$files = $ds->getIncludedFiles();
109
				$dir = $fs->getDir($this->project)->getPath();
110
				foreach($files as $file) {
111
					$this->lint($dir.DIRECTORY_SEPARATOR.$file);
112
				}
113
			}
114
		}
115
 
116
		if ($this->haltOnFailure && $this->hasErrors) throw new BuildException('Syntax error(s) in PHP files: '.implode(', ',$this->badFiles));
117
	}
118
 
119
	/**
120
	 * Performs the actual syntax check
121
	 *
122
	 * @param string $file
123
	 * @return void
124
	 */
125
	protected function lint($file) {
126
        $command = $this->Interpreter == ''
127
            ? 'php'
128
            : $this->Interpreter;
129
        $command .= ' -l ';
130
		if(file_exists($file)) {
131
			if(is_readable($file)) {
132
				$messages = array();
133
				exec($command.'"'.$file.'"', $messages);
134
				if(!preg_match('/^No syntax errors detected/', $messages[0])) {
135
					if (count($messages) > 1) {
136
						if ($this->errorProperty) {
137
							$this->project->setProperty($this->errorProperty, $messages[1]);
138
						}
139
						$this->log($messages[1], Project::MSG_ERR);
140
					} else {
141
						$this->log("Could not parse file", Project::MSG_ERR);
142
					}
143
					$this->badFiles[] = $file;
144
					$this->hasErrors = true;
145
 
146
				} else {
147
					$this->log($file.': No syntax errors detected', Project::MSG_INFO);
148
				}
149
			} else {
150
				throw new BuildException('Permission denied: '.$file);
151
			}
152
		} else {
153
			throw new BuildException('File not found: '.$file);
154
		}
155
	}
156
}
157
 
158
 
159