final move of files
[web.mtrack] / MTrack / CommitCheck / PhpLint.php
1 <?php 
2 require_once 'Interface/CommitListener.php';
3
4 class MTrackCommitCheck_PhpLint implements IMTrackCommitListener 
5 {
6   
7     function vetoCommit($msg, $files, $actions, $checker)
8     {
9         // should only have a list of files which have been updated/added. (not deleted)
10         $ret = array();
11         
12         if (count($files) > 30) {
13             // to many files.. we can not check that amount without causing serious delays in commits
14             return true;
15         }
16         foreach ($files as $filename) {
17             $pi = pathinfo($filename);
18
19             if ( $pi['extension'] == 'php') {
20                 $fp = $checker->bridge->getFileStream($filename);
21                 $res = $this->checkPHP($filename, $fp);
22                 if ($res !== true) {
23                     $ret[] = $res;
24                 }
25                 $fp = null; // remove stream.
26             }
27           
28         }
29          
30         return $ret ? implode("\n", $ret) : true;
31     }
32
33     function postCommit($msg, $files, $actions)
34     {
35         return true;
36     }
37     function checkPHP($filename, $fp)
38     {
39         $pipes = null;
40         $proc = proc_open(MTrackConfig::get('tools', 'php') . " -l", array(
41             0 => array('pipe', 'r'),
42             1 => array('pipe', 'w'),
43             2 => array('pipe', 'w')
44           ), $pipes);
45
46         // send in data
47         stream_copy_to_stream($fp, $pipes[0]);
48         $fp = null;
49         $pipes[0] = null;
50
51         $output = stream_get_contents($pipes[1]);
52         $output .= stream_get_contents($pipes[2]);
53         $st = proc_get_status($proc);
54         if ($st['running']) {
55           proc_terminate($proc);
56           sleep(1);
57           $st = proc_get_status($proc);
58         }
59         if ($st['exitcode'] != 0) {
60           return "$filename: $output";
61         }
62         return true;
63     }
64 }