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
|
|
* A mapping selector is an abstract class adding mapping support to the
|
22
|
|
* base selector
|
23
|
|
*
|
24
|
|
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
|
25
|
|
* @package phing.types.selectors
|
26
|
|
*/
|
27
|
|
abstract class MappingSelector extends BaseSelector
|
28
|
|
{
|
29
|
|
/**
|
30
|
|
* @var PhingFile $targetdir
|
31
|
|
*/
|
32
|
|
protected $targetdir;
|
33
|
|
|
34
|
|
/**
|
35
|
|
* @var Mapper $mapperElement
|
36
|
|
*/
|
37
|
|
protected $mapperElement;
|
38
|
|
|
39
|
|
/**
|
40
|
|
* @var FileNameMapper $map
|
41
|
|
*/
|
42
|
|
protected $map;
|
43
|
|
|
44
|
|
/**
|
45
|
|
* The name of the file or directory which is checked for out-of-date
|
46
|
|
* files.
|
47
|
|
*
|
48
|
|
* @param PhingFile $targetdir the directory to scan looking for files.
|
49
|
|
*/
|
50
|
1
|
public function setTargetdir(PhingFile $targetdir)
|
51
|
|
{
|
52
|
1
|
$this->targetdir = $targetdir;
|
53
|
|
}
|
54
|
|
|
55
|
|
/**
|
56
|
|
* Defines the FileNameMapper to use (nested mapper element).
|
57
|
|
*
|
58
|
|
* @return Mapper a mapper to be configured
|
59
|
|
* @throws BuildException if more than one mapper defined
|
60
|
|
*/
|
61
|
0
|
public function createMapper()
|
62
|
|
{
|
63
|
0
|
if ($this->map !== null || $this->mapperElement !== null) {
|
64
|
0
|
throw new BuildException('Cannot define more than one mapper');
|
65
|
|
}
|
66
|
0
|
$this->mapperElement = new Mapper($this->getProject());
|
67
|
0
|
return $this->mapperElement;
|
68
|
|
}
|
69
|
|
|
70
|
|
/**
|
71
|
|
* Add a configured FileNameMapper instance.
|
72
|
|
*
|
73
|
|
* @param FileNameMapper $fileNameMapper the FileNameMapper to add
|
74
|
|
*
|
75
|
|
* @throws BuildException if more than one mapper defined
|
76
|
|
*/
|
77
|
0
|
public function addConfigured(FileNameMapper $fileNameMapper)
|
78
|
|
{
|
79
|
0
|
if ($this->map !== null || $this->mapperElement !== null) {
|
80
|
0
|
throw new BuildException('Cannot define more than one mapper');
|
81
|
|
}
|
82
|
0
|
$this->map = $fileNameMapper;
|
83
|
|
}
|
84
|
|
|
85
|
|
/**
|
86
|
|
* Checks to make sure all settings are kosher. In this case, it
|
87
|
|
* means that the dest attribute has been set and we have a mapper.
|
88
|
|
*/
|
89
|
1
|
public function verifySettings()
|
90
|
|
{
|
91
|
1
|
if ($this->targetdir === null) {
|
92
|
0
|
$this->setError("The targetdir attribute is required.");
|
93
|
|
}
|
94
|
1
|
if ($this->map === null) {
|
95
|
1
|
if ($this->mapperElement === null) {
|
96
|
1
|
$this->map = new IdentityMapper();
|
97
|
|
} else {
|
98
|
0
|
$this->map = $this->mapperElement->getImplementation();
|
99
|
0
|
if ($this->map === null) {
|
100
|
0
|
$this->setError("Could not set <mapper> element.");
|
101
|
|
}
|
102
|
|
}
|
103
|
|
}
|
104
|
|
}
|
105
|
|
|
106
|
|
/**
|
107
|
|
* The heart of the matter. This is where the selector gets to decide
|
108
|
|
* on the inclusion of a file in a particular fileset.
|
109
|
|
*
|
110
|
|
* @param PhingFile $basedir the base directory the scan is being done from
|
111
|
|
* @param string $filename is the name of the file to check
|
112
|
|
* @param PhingFile $file is a java.io.File object the selector can use
|
113
|
|
*
|
114
|
|
* @return bool whether the file should be selected or not
|
115
|
|
*
|
116
|
|
* @throws BuildException
|
117
|
|
*/
|
118
|
1
|
public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
|
119
|
|
{
|
120
|
|
|
121
|
|
// throw BuildException on error
|
122
|
1
|
$this->validate();
|
123
|
|
|
124
|
|
// Determine file whose out-of-dateness is to be checked
|
125
|
1
|
$destfiles = $this->map->main($filename);
|
126
|
|
// If filename does not match the To attribute of the mapper
|
127
|
|
// then filter it out of the files we are considering
|
128
|
1
|
if (empty($destfiles)) {
|
129
|
0
|
return false;
|
130
|
|
}
|
131
|
|
// Sanity check
|
132
|
1
|
if (count($destfiles) !== 1 || $destfiles[0] === null) {
|
133
|
0
|
throw new BuildException(
|
134
|
|
'Invalid destination file results for '
|
135
|
0
|
. $this->targetdir->getName() . ' with filename ' . $filename
|
136
|
|
);
|
137
|
|
}
|
138
|
1
|
$destname = $destfiles[0];
|
139
|
1
|
$fu = new FileUtils();
|
140
|
1
|
$destfile = $fu->resolveFile($this->targetdir, $destname);
|
141
|
|
|
142
|
1
|
return $this->selectionTest($file, $destfile);
|
143
|
|
}
|
144
|
|
|
145
|
|
/**
|
146
|
|
* this test is our selection test that compared the file with the destfile
|
147
|
|
*
|
148
|
|
* @param PhingFile $srcfile file to test; may be null
|
149
|
|
* @param PhingFile $destfile destination file
|
150
|
|
* @return true if source file compares with destination file
|
151
|
|
*/
|
152
|
|
abstract protected function selectionTest(PhingFile $srcfile, PhingFile $destfile);
|
153
|
|
}
|