summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/util/SourceFileScanner.php
blob: d958a15fb26b8188d35b5cfdcfbd4202b8bf0a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
 *  $Id: 0555ae8b4e7418c211bd69a066877ab8cf95fbb8 $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>. 
 */

/**
 *  Utility class that collects the functionality of the various
 *  scanDir methods that have been scattered in several tasks before.
 *
 *  The only method returns an array of source files. The array is a
 *  subset of the files given as a parameter and holds only those that
 *  are newer than their corresponding target files.
 *  @package   phing.util
 */
class SourceFileScanner {

    /** Instance of FileUtils */
    private $fileUtils;
    
    /** Task this class is working for -- for logging purposes. */
    private $task;

    /**
     * @param task The task we should log messages through
     */
    function __construct($task) {
        $this->task = $task;
        $this->fileUtils = new FileUtils();
    }

    /**
     * Restrict the given set of files to those that are newer than
     * their corresponding target files.
     *
     * @param files   the original set of files
     * @param srcDir  all files are relative to this directory
     * @param destDir target files live here. if null file names
     *                returned by the mapper are assumed to be absolute.
     * @param FilenameMapper  knows how to construct a target file names from
     *                source file names.
     * @param force   Boolean that determines if the files should be
     *                forced to be copied.
     */
    function restrict(&$files, $srcDir, $destDir, $mapper, $force = false) {
        $now = time();
        $targetList = "";

        /*
          If we're on Windows, we have to munge the time up to 2 secs to
          be able to check file modification times.
          (Windows has a max resolution of two secs for modification times)
        */
        $osname = strtolower(Phing::getProperty('os.name'));

        // indexOf()
        $index = ((($res = strpos($osname, 'win')) === false) ? -1 : $res);
        if ($index  >= 0 ) {
            $now += 2000;
        }

        $v = array();

        for ($i=0, $size=count($files); $i < $size; $i++) {
        
            $targets = $mapper->main($files[$i]);
            if (empty($targets)) {
                $this->task->log($files[$i]." skipped - don't know how to handle it", Project::MSG_VERBOSE);
                continue;
            }

            $src = null;
            try {
                if ($srcDir === null) {
                    $src = new PhingFile($files[$i]);
                } else {
                    $src = $this->fileUtils->resolveFile($srcDir, $files[$i]);
                }
    
                if ($src->lastModified() > $now) {
                    $this->task->log("Warning: ".$files[$i]." modified in the future (".$src->lastModified()." > ".$now.")", Project::MSG_WARN);
                }
            } catch (IOException $ioe) {
                $this->task->log("Unable to read file ".$files[$i]." (skipping): " . $ioe->getMessage());
                continue;
            }
            
            $added = false;
            $targetList = "";

            for ($j=0,$_j=count($targets); (!$added && $j < $_j); $j++) {

                $dest = null;
                if ($destDir === null) {
                    $dest = new PhingFile($targets[$j]);
                } else {
                    $dest = $this->fileUtils->resolveFile($destDir, $targets[$j]);
                }

                if (!$dest->exists()) {
                    $this->task->log($files[$i]." added as " . $dest->__toString() . " doesn't exist.", Project::MSG_VERBOSE);
                    $v[] =$files[$i];
                    $added = true;
                } elseif ($src->lastModified() > $dest->lastModified()) {
                    $this->task->log($files[$i]." added as " . $dest->__toString() . " is outdated.", Project::MSG_VERBOSE );
                    $v[]=$files[$i];
                    $added = true;
                } elseif ($force === true) {
                    $this->task->log($files[$i]." added as " . $dest->__toString() . " is forced to be overwritten.", Project::MSG_VERBOSE );
                    $v[]=$files[$i];
                    $added = true;
                } else {
                    if (strlen($targetList) > 0) {
                        $targetList .= ", ";
                    }
                    $targetList .= $dest->getAbsolutePath();
                }
            }

            if (!$added) {
                $this->task->log($files[$i]." omitted as ".$targetList." ".(count($targets) === 1 ? " is " : " are ")."up to date.",  Project::MSG_VERBOSE);
            }

        }
        $result = array();
        $result = $v;
        return $result;
    }

    /**
     * Convenience layer on top of restrict that returns the source
     * files as PhingFile objects (containing absolute paths if srcDir is
     * absolute).
     */
    function restrictAsFiles(&$files, &$srcDir, &$destDir, &$mapper) {
        $res = $this->restrict($files, $srcDir, $destDir, $mapper);
        $result = array();
        for ($i=0; $i<count($res); $i++) {
            $result[$i] = new PhingFile($srcDir, $res[$i]);
        }
        return $result;
    }
}