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