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, [email protected] |
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
|
|
|
public function setHandleDirSep($handleDirSep) |
82
|
|
|
{ |
83
|
|
|
$this->handleDirSep = $handleDirSep; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Attribute specifying whether to ignore the difference |
88
|
|
|
* between / and \ (the two common directory characters). |
89
|
|
|
*/ |
90
|
|
|
public function getHandleDirSep() |
91
|
|
|
{ |
92
|
|
|
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
|
|
|
public function setCaseSensitive($caseSensitive) |
102
|
|
|
{ |
103
|
|
|
$this->caseSensitive = $caseSensitive; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
* |
109
|
|
|
* @param mixed $sourceFileName |
110
|
|
|
* @return array|null |
111
|
|
|
*/ |
112
|
6 |
|
public function main($sourceFileName) |
113
|
|
|
{ |
114
|
6 |
|
$modName = $this->modifyName($sourceFileName); |
115
|
|
|
if ( |
116
|
6 |
|
$this->fromPrefix === null |
117
|
6 |
|
|| (strlen($sourceFileName) < ($this->prefixLength + $this->postfixLength) |
118
|
6 |
|
|| (!$this->fromContainsStar |
119
|
6 |
|
&& !$modName === $this->modifyName($this->fromPrefix))) |
120
|
6 |
|
|| ($this->fromContainsStar |
121
|
6 |
|
&& (!StringHelper::startsWith($this->modifyName($this->fromPrefix), $modName) |
122
|
6 |
|
|| !StringHelper::endsWith($this->modifyName($this->fromPostfix), $modName))) |
123
|
|
|
) { |
124
|
2 |
|
return null; |
125
|
|
|
} |
126
|
|
|
return [ |
127
|
6 |
|
$this->toPrefix . ( |
128
|
6 |
|
$this->toContainsStar |
129
|
6 |
|
? $this->extractVariablePart($sourceFileName) . $this->toPostfix |
130
|
6 |
|
: '' |
131
|
|
|
) |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
* |
138
|
|
|
* @param string $from |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
6 |
|
public function setFrom($from) |
142
|
|
|
{ |
143
|
6 |
|
if ($from === null) { |
|
|
|
|
144
|
|
|
throw new BuildException("this mapper requires a 'from' attribute"); |
145
|
|
|
} |
146
|
|
|
|
147
|
6 |
|
$index = strrpos($from, '*'); |
148
|
|
|
|
149
|
6 |
|
if ($index === false) { |
150
|
|
|
$this->fromPrefix = $from; |
151
|
|
|
$this->fromPostfix = ""; |
152
|
|
|
} else { |
153
|
6 |
|
$this->fromPrefix = substr($from, 0, $index); |
154
|
6 |
|
$this->fromPostfix = substr($from, $index + 1); |
155
|
6 |
|
$this->fromContainsStar = true; |
156
|
|
|
} |
157
|
6 |
|
$this->prefixLength = strlen($this->fromPrefix); |
158
|
6 |
|
$this->postfixLength = strlen($this->fromPostfix); |
159
|
6 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Sets the "to" pattern. Required. |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
* |
165
|
|
|
* @param string $to |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
6 |
|
public function setTo($to) |
169
|
|
|
{ |
170
|
6 |
|
if ($to === null) { |
|
|
|
|
171
|
|
|
throw new BuildException("this mapper requires a 'to' attribute"); |
172
|
|
|
} |
173
|
|
|
|
174
|
6 |
|
$index = strrpos($to, '*'); |
175
|
6 |
|
if ($index === false) { |
176
|
|
|
$this->toPrefix = $to; |
177
|
|
|
$this->toPostfix = ""; |
178
|
|
|
} else { |
179
|
6 |
|
$this->toPrefix = substr($to, 0, $index); |
180
|
6 |
|
$this->toPostfix = substr($to, $index + 1); |
181
|
6 |
|
$this->toContainsStar = true; |
182
|
|
|
} |
183
|
6 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Extracts the variable part. |
187
|
|
|
* |
188
|
|
|
* @param string $name |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
6 |
|
private function extractVariablePart($name) |
192
|
|
|
{ |
193
|
6 |
|
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
|
6 |
|
private function modifyName($name) |
203
|
|
|
{ |
204
|
6 |
|
if (!$this->caseSensitive) { |
205
|
|
|
$name = strtolower($name); |
206
|
|
|
} |
207
|
6 |
|
if ($this->handleDirSep) { |
208
|
|
|
if (strpos('\\', $name) !== false) { |
209
|
|
|
$name = str_replace('\\', '/', $name); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
6 |
|
return $name; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|