1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 *  Utility class that collects the functionality of the various
22
 *  scanDir methods that have been scattered in several tasks before.
23
 *
24
 *  The only method returns an array of source files. The array is a
25
 *  subset of the files given as a parameter and holds only those that
26
 *  are newer than their corresponding target files.
27
 *
28
 * @package phing.util
29
 */
30
class SourceFileScanner
31
{
32

33
    /**
34
     * Instance of FileUtils
35
     */
36
    private $fileUtils;
37

38
    /**
39
     * Task this class is working for -- for logging purposes.
40
     */
41
    private $task;
42

43
    /**
44
     * @param Task $task The task we should log messages through
45
     */
46 1
    public function __construct($task)
47
    {
48 1
        $this->task = $task;
49 1
        $this->fileUtils = new FileUtils();
50
    }
51

52
    /**
53
     * Restrict the given set of files to those that are newer than
54
     * their corresponding target files.
55
     *
56
     * @param  iterable $files the original set of files
57
     * @param  PhingFile $srcDir all files are relative to this directory
58
     * @param  PhingFile $destDir target files live here. if null file names
59
     *                                returned by the mapper are assumed to be
60
     *                                absolute.
61
     * @param  FilenameMapper $mapper knows how to construct a target file names from
62
     *                source file names.
63
     * @param  bool $force Boolean that determines if the files should be
64
     *                                forced to be copied.
65
     * @return array
66
     */
67 1
    public function restrict(&$files, $srcDir, $destDir, $mapper, $force = false)
68
    {
69 1
        $now = time();
70 1
        $targetList = "";
71

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

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

85 1
        $v = [];
86

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

94 1
            $src = null;
95
            try {
96 1
                if ($srcDir === null) {
97 0
                    $src = new PhingFile($files[$i]);
98
                } else {
99 1
                    $src = $this->fileUtils->resolveFile($srcDir, $files[$i]);
100
                }
101

102 1
                if ($src->lastModified() > $now) {
103 0
                    $this->task->log(
104 0
                        "Warning: " . $files[$i] . " modified in the future (" . $src->lastModified() . " > " . $now . ")",
105 0
                        Project::MSG_WARN
106
                    );
107
                }
108 0
            } catch (IOException $ioe) {
109 0
                $this->task->log("Unable to read file " . $files[$i] . " (skipping): " . $ioe->getMessage());
110 0
                continue;
111
            }
112

113 1
            $added = false;
114 1
            $targetList = "";
115

116 1
            for ($j = 0, $_j = count($targets); (!$added && $j < $_j); $j++) {
117 1
                $dest = null;
118 1
                if ($destDir === null) {
119 1
                    $dest = new PhingFile($targets[$j]);
120
                } else {
121 1
                    $dest = $this->fileUtils->resolveFile($destDir, $targets[$j]);
122
                }
123

124 1
                if (!$dest->exists()) {
125 1
                    $this->task->log(
126 1
                        ($files[$i] ?: ".") . " added as " . $dest->__toString() . " doesn't exist.",
127 1
                        Project::MSG_VERBOSE
128
                    );
129 1
                    $v[] = $files[$i];
130 1
                    $added = true;
131 1
                } elseif ($src->lastModified() > $dest->lastModified()) {
132 0
                    $this->task->log(
133 0
                        $files[$i] . " added as " . $dest->__toString() . " is outdated.",
134 0
                        Project::MSG_VERBOSE
135
                    );
136 0
                    $v[] = $files[$i];
137 0
                    $added = true;
138 1
                } elseif ($force === true) {
139 0
                    $this->task->log(
140 0
                        $files[$i] . " added as " . $dest->__toString() . " is forced to be overwritten.",
141 0
                        Project::MSG_VERBOSE
142
                    );
143 0
                    $v[] = $files[$i];
144 0
                    $added = true;
145
                } else {
146 1
                    if (strlen($targetList) > 0) {
147 0
                        $targetList .= ", ";
148
                    }
149 1
                    $targetList .= $dest->getAbsolutePath();
150
                }
151
            }
152

153 1
            if (!$added) {
154 1
                $this->task->log(
155 1
                    $files[$i] . " omitted as " . $targetList . " " . (count(
156 0
                        $targets
157 1
                    ) === 1 ? " is " : " are ") . "up to date.",
158 1
                    Project::MSG_VERBOSE
159
                );
160
            }
161
        }
162

163 1
        return $v;
164
    }
165

166
    /**
167
     * Convenience layer on top of restrict that returns the source
168
     * files as PhingFile objects (containing absolute paths if srcDir is
169
     * absolute).
170
     *
171
     * @param  $files
172
     * @param  $srcDir
173
     * @param  $destDir
174
     * @param  $mapper
175
     * @return array
176
     */
177 0
    public function restrictAsFiles(&$files, &$srcDir, &$destDir, &$mapper)
178
    {
179 0
        $res = $this->restrict($files, $srcDir, $destDir, $mapper);
180 0
        $result = [];
181 0
        for ($i = 0, $resultsCount = count($res); $i < $resultsCount; $i++) {
182 0
            $result[$i] = new PhingFile($srcDir, $res[$i]);
183
        }
184

185 0
        return $result;
186
    }
187
}

Read our documentation on viewing source code .

Loading