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
|
|
* Uses glob patterns to perform filename transformations.
|
22
|
|
*
|
23
|
|
* @author Andreas Aderhold, andi@binarycloud.com
|
24
|
|
* @package phing.mappers
|
25
|
|
*/
|
26
|
|
class GlobMapper implements FileNameMapper
|
27
|
|
{
|
28
|
|
/**
|
29
|
|
* Part of "from" pattern before the <code>.*</code>.
|
30
|
|
*
|
31
|
|
* @var string $fromPrefix
|
32
|
|
*/
|
33
|
|
private $fromPrefix = null;
|
34
|
|
|
35
|
|
/**
|
36
|
|
* Part of "from" pattern after the <code>.*</code>.
|
37
|
|
*
|
38
|
|
* @var string $fromPostfix
|
39
|
|
*/
|
40
|
|
private $fromPostfix = null;
|
41
|
|
|
42
|
|
/**
|
43
|
|
* Length of the prefix ("from" pattern).
|
44
|
|
*
|
45
|
|
* @var int $prefixLength
|
46
|
|
*/
|
47
|
|
private $prefixLength;
|
48
|
|
|
49
|
|
/**
|
50
|
|
* Length of the postfix ("from" pattern).
|
51
|
|
*
|
52
|
|
* @var int $postfixLength
|
53
|
|
*/
|
54
|
|
private $postfixLength;
|
55
|
|
|
56
|
|
/**
|
57
|
|
* Part of "to" pattern before the <code>*.</code>.
|
58
|
|
*
|
59
|
|
* @var string $toPrefix
|
60
|
|
*/
|
61
|
|
private $toPrefix = null;
|
62
|
|
|
63
|
|
/**
|
64
|
|
* Part of "to" pattern after the <code>*.</code>.
|
65
|
|
*
|
66
|
|
* @var string $toPostfix
|
67
|
|
*/
|
68
|
|
private $toPostfix = null;
|
69
|
|
|
70
|
|
private $fromContainsStar = false;
|
71
|
|
private $toContainsStar = false;
|
72
|
|
private $handleDirSep = false;
|
73
|
|
private $caseSensitive = true;
|
74
|
|
|
75
|
|
/**
|
76
|
|
* Attribute specifying whether to ignore the difference
|
77
|
|
* between / and \ (the two common directory characters).
|
78
|
|
*
|
79
|
|
* @param boolean $handleDirSep a boolean, default is false.
|
80
|
|
*/
|
81
|
0
|
public function setHandleDirSep($handleDirSep)
|
82
|
|
{
|
83
|
0
|
$this->handleDirSep = $handleDirSep;
|
84
|
|
}
|
85
|
|
|
86
|
|
/**
|
87
|
|
* Attribute specifying whether to ignore the difference
|
88
|
|
* between / and \ (the two common directory characters).
|
89
|
|
*/
|
90
|
0
|
public function getHandleDirSep()
|
91
|
|
{
|
92
|
0
|
return $this->handleDirSep;
|
93
|
|
}
|
94
|
|
|
95
|
|
/**
|
96
|
|
* Attribute specifying whether to ignore the case difference
|
97
|
|
* in the names.
|
98
|
|
*
|
99
|
|
* @param boolean $caseSensitive a boolean, default is false.
|
100
|
|
*/
|
101
|
0
|
public function setCaseSensitive($caseSensitive)
|
102
|
|
{
|
103
|
0
|
$this->caseSensitive = $caseSensitive;
|
104
|
|
}
|
105
|
|
|
106
|
|
/**
|
107
|
|
* {@inheritdoc}
|
108
|
|
*
|
109
|
|
* @param mixed $sourceFileName
|
110
|
|
* @return array|null
|
111
|
|
*/
|
112
|
1
|
public function main($sourceFileName)
|
113
|
|
{
|
114
|
1
|
$modName = $this->modifyName($sourceFileName);
|
115
|
|
if (
|
116
|
1
|
$this->fromPrefix === null
|
117
|
1
|
|| (strlen($sourceFileName) < ($this->prefixLength + $this->postfixLength)
|
118
|
1
|
|| (!$this->fromContainsStar
|
119
|
0
|
&& !$modName === $this->modifyName($this->fromPrefix)))
|
120
|
1
|
|| ($this->fromContainsStar
|
121
|
1
|
&& (!StringHelper::startsWith($this->modifyName($this->fromPrefix), $modName)
|
122
|
1
|
|| !StringHelper::endsWith($this->modifyName($this->fromPostfix), $modName)))
|
123
|
|
) {
|
124
|
1
|
return null;
|
125
|
|
}
|
126
|
|
return [
|
127
|
1
|
$this->toPrefix . (
|
128
|
1
|
$this->toContainsStar
|
129
|
1
|
? $this->extractVariablePart($sourceFileName) . $this->toPostfix
|
130
|
1
|
: ''
|
131
|
|
)
|
132
|
|
];
|
133
|
|
}
|
134
|
|
|
135
|
|
/**
|
136
|
|
* {@inheritdoc}
|
137
|
|
*
|
138
|
|
* @param string $from
|
139
|
|
* @return void
|
140
|
|
*/
|
141
|
1
|
public function setFrom($from)
|
142
|
|
{
|
143
|
1
|
if ($from === null) {
|
144
|
0
|
throw new BuildException("this mapper requires a 'from' attribute");
|
145
|
|
}
|
146
|
|
|
147
|
1
|
$index = strrpos($from, '*');
|
148
|
|
|
149
|
1
|
if ($index === false) {
|
150
|
0
|
$this->fromPrefix = $from;
|
151
|
0
|
$this->fromPostfix = "";
|
152
|
|
} else {
|
153
|
1
|
$this->fromPrefix = substr($from, 0, $index);
|
154
|
1
|
$this->fromPostfix = substr($from, $index + 1);
|
155
|
1
|
$this->fromContainsStar = true;
|
156
|
|
}
|
157
|
1
|
$this->prefixLength = strlen($this->fromPrefix);
|
158
|
1
|
$this->postfixLength = strlen($this->fromPostfix);
|
159
|
|
}
|
160
|
|
|
161
|
|
/**
|
162
|
|
* Sets the "to" pattern. Required.
|
163
|
|
* {@inheritdoc}
|
164
|
|
*
|
165
|
|
* @param string $to
|
166
|
|
* @return void
|
167
|
|
*/
|
168
|
1
|
public function setTo($to)
|
169
|
|
{
|
170
|
1
|
if ($to === null) {
|
171
|
0
|
throw new BuildException("this mapper requires a 'to' attribute");
|
172
|
|
}
|
173
|
|
|
174
|
1
|
$index = strrpos($to, '*');
|
175
|
1
|
if ($index === false) {
|
176
|
0
|
$this->toPrefix = $to;
|
177
|
0
|
$this->toPostfix = "";
|
178
|
|
} else {
|
179
|
1
|
$this->toPrefix = substr($to, 0, $index);
|
180
|
1
|
$this->toPostfix = substr($to, $index + 1);
|
181
|
1
|
$this->toContainsStar = true;
|
182
|
|
}
|
183
|
|
}
|
184
|
|
|
185
|
|
/**
|
186
|
|
* Extracts the variable part.
|
187
|
|
*
|
188
|
|
* @param string $name
|
189
|
|
* @return string
|
190
|
|
*/
|
191
|
1
|
private function extractVariablePart($name)
|
192
|
|
{
|
193
|
1
|
return StringHelper::substring($name, $this->prefixLength, strlen($name) - $this->postfixLength - 1);
|
194
|
|
}
|
195
|
|
|
196
|
|
/**
|
197
|
|
* modify string based on dir char mapping and case sensitivity
|
198
|
|
*
|
199
|
|
* @param string $name the name to convert
|
200
|
|
* @return string the converted name
|
201
|
|
*/
|
202
|
1
|
private function modifyName($name)
|
203
|
|
{
|
204
|
1
|
if (!$this->caseSensitive) {
|
205
|
0
|
$name = strtolower($name);
|
206
|
|
}
|
207
|
1
|
if ($this->handleDirSep) {
|
208
|
0
|
if (strpos('\\', $name) !== false) {
|
209
|
0
|
$name = str_replace('\\', '/', $name);
|
210
|
|
}
|
211
|
|
}
|
212
|
|
|
213
|
1
|
return $name;
|
214
|
|
}
|
215
|
|
}
|