1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Phing\Io; |
22
|
|
|
|
23
|
|
|
use Phing\Mapper\FileNameMapper; |
24
|
|
|
use Phing\Phing; |
25
|
|
|
use Phing\Project; |
26
|
|
|
use Phing\Task; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Utility class that collects the functionality of the various |
30
|
|
|
* scanDir methods that have been scattered in several tasks before. |
31
|
|
|
* |
32
|
|
|
* The only method returns an array of source files. The array is a |
33
|
|
|
* subset of the files given as a parameter and holds only those that |
34
|
|
|
* are newer than their corresponding target files. |
35
|
|
|
*/ |
36
|
|
|
class SourceFileScanner |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Instance of FileUtils. |
40
|
|
|
*/ |
41
|
|
|
private $fileUtils; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Task this class is working for -- for logging purposes. |
45
|
|
|
*/ |
46
|
|
|
private $task; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Task $task The task we should log messages through |
50
|
|
|
*/ |
51
|
|
|
public function __construct($task) |
52
|
|
|
{ |
53
|
|
|
$this->task = $task; |
54
|
|
|
$this->fileUtils = new FileUtils(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Restrict the given set of files to those that are newer than |
59
|
|
|
* their corresponding target files. |
60
|
|
|
* |
61
|
|
|
* @param iterable $files the original set of files |
62
|
|
|
* @param File $srcDir all files are relative to this directory |
63
|
|
|
* @param File $destDir target files live here. if null file names |
64
|
|
|
* returned by the mapper are assumed to be |
65
|
|
|
* absolute. |
66
|
|
|
* @param FilenameMapper $mapper knows how to construct a target file names from |
67
|
|
|
* source file names |
68
|
|
|
* @param bool $force bool that determines if the files should be |
69
|
|
|
* forced to be copied |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function restrict(&$files, $srcDir, $destDir, $mapper, $force = false) |
74
|
|
|
{ |
75
|
|
|
$now = time(); |
76
|
|
|
$targetList = ''; |
|
|
|
|
77
|
|
|
|
78
|
|
|
/* |
79
|
|
|
If we're on Windows, we have to munge the time up to 2 secs to |
80
|
|
|
be able to check file modification times. |
81
|
|
|
(Windows has a max resolution of two secs for modification times) |
82
|
|
|
*/ |
83
|
|
|
$osname = strtolower(Phing::getProperty('os.name')); |
84
|
|
|
|
85
|
|
|
// indexOf() |
86
|
|
|
$index = ((($res = strpos($osname, 'win')) === false) ? -1 : $res); |
87
|
|
|
if ($index >= 0) { |
88
|
|
|
$now += 2000; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$v = []; |
92
|
|
|
|
93
|
|
|
for ($i = 0, $size = count($files); $i < $size; ++$i) { |
94
|
|
|
$targets = $mapper->main($files[$i]); |
95
|
|
|
if (empty($targets)) { |
96
|
|
|
$this->task->log($files[$i] . " skipped - don't know how to handle it", Project::MSG_VERBOSE); |
97
|
|
|
|
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$src = null; |
102
|
|
|
|
103
|
|
|
try { |
104
|
|
|
if (null === $srcDir) { |
105
|
|
|
$src = new File($files[$i]); |
106
|
|
|
} else { |
107
|
|
|
$src = $this->fileUtils->resolveFile($srcDir, $files[$i]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($src->lastModified() > $now) { |
111
|
|
|
$this->task->log( |
112
|
|
|
'Warning: ' . $files[$i] . ' modified in the future (' . $src->lastModified() . ' > ' . $now . ')', |
113
|
|
|
Project::MSG_WARN |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} catch (IOException $ioe) { |
117
|
|
|
$this->task->log('Unable to read file ' . $files[$i] . ' (skipping): ' . $ioe->getMessage()); |
118
|
|
|
|
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$added = false; |
123
|
|
|
$targetList = ''; |
124
|
|
|
|
125
|
|
|
for ($j = 0, $_j = count($targets); (!$added && $j < $_j); ++$j) { |
126
|
|
|
$dest = null; |
127
|
|
|
if (null === $destDir) { |
128
|
|
|
$dest = new File($targets[$j]); |
129
|
|
|
} else { |
130
|
|
|
$dest = $this->fileUtils->resolveFile($destDir, $targets[$j]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!$dest->exists()) { |
134
|
|
|
$this->task->log( |
135
|
|
|
($files[$i] ?: '.') . ' added as ' . $dest->__toString() . " doesn't exist.", |
136
|
|
|
Project::MSG_VERBOSE |
137
|
|
|
); |
138
|
|
|
$v[] = $files[$i]; |
139
|
|
|
$added = true; |
140
|
|
|
} elseif ($src->lastModified() > $dest->lastModified()) { |
141
|
|
|
$this->task->log( |
142
|
|
|
$files[$i] . ' added as ' . $dest->__toString() . ' is outdated.', |
143
|
|
|
Project::MSG_VERBOSE |
144
|
|
|
); |
145
|
|
|
$v[] = $files[$i]; |
146
|
|
|
$added = true; |
147
|
|
|
} elseif (true === $force) { |
148
|
|
|
$this->task->log( |
149
|
|
|
$files[$i] . ' added as ' . $dest->__toString() . ' is forced to be overwritten.', |
150
|
|
|
Project::MSG_VERBOSE |
151
|
|
|
); |
152
|
|
|
$v[] = $files[$i]; |
153
|
|
|
$added = true; |
154
|
|
|
} else { |
155
|
|
|
if (strlen($targetList) > 0) { |
156
|
|
|
$targetList .= ', '; |
157
|
|
|
} |
158
|
|
|
$targetList .= $dest->getAbsolutePath(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!$added) { |
163
|
|
|
$this->task->log( |
164
|
|
|
$files[$i] . ' omitted as ' . $targetList . ' ' . (1 === count( |
165
|
|
|
$targets |
166
|
|
|
) ? ' is ' : ' are ') . 'up to date.', |
167
|
|
|
Project::MSG_VERBOSE |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $v; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Convenience layer on top of restrict that returns the source |
177
|
|
|
* files as PhingFile objects (containing absolute paths if srcDir is |
178
|
|
|
* absolute). |
179
|
|
|
* |
180
|
|
|
* @param $files |
181
|
|
|
* @param $srcDir |
182
|
|
|
* @param $destDir |
183
|
|
|
* @param $mapper |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
public function restrictAsFiles(&$files, &$srcDir, &$destDir, &$mapper) |
188
|
|
|
{ |
189
|
|
|
$res = $this->restrict($files, $srcDir, $destDir, $mapper); |
190
|
|
|
$result = []; |
191
|
|
|
for ($i = 0, $resultsCount = count($res); $i < $resultsCount; ++$i) { |
192
|
|
|
$result[$i] = new File($srcDir, $res[$i]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $result; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|