|
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\Task\System; |
|
22
|
|
|
|
|
23
|
|
|
use Phing\Exception\BuildException; |
|
24
|
|
|
use Phing\Io\File; |
|
25
|
|
|
use Phing\Task; |
|
26
|
|
|
use Phing\Util\StringHelper; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Task that changes the permissions on a file/directory. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Siad Ardroumli <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class Basename extends Task |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var File |
|
37
|
|
|
*/ |
|
38
|
|
|
private $file; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $property; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $suffix; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* file or directory to get base name from. |
|
52
|
|
|
* |
|
53
|
|
|
* @param File $file file or directory to get base name from |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setFile(File $file): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->file = $file; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Property to set base name to. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $property name of property |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setProperty(string $property): void |
|
66
|
|
|
{ |
|
67
|
|
|
$this->property = $property; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Optional suffix to remove from base name. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $suffix suffix to remove from base name |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setSuffix(string $suffix): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->suffix = $suffix; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* do the work. |
|
82
|
|
|
* |
|
83
|
|
|
* @throws BuildException if required attributes are not supplied |
|
84
|
|
|
* property and attribute are required attributes |
|
85
|
|
|
*/ |
|
86
|
|
|
public function main() |
|
87
|
|
|
{ |
|
88
|
|
|
if (null === $this->property) { |
|
89
|
|
|
throw new BuildException('property attribute required', $this->getLocation()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (null === $this->file) { |
|
93
|
|
|
throw new BuildException('file attribute required', $this->getLocation()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->getProject()->setNewProperty( |
|
97
|
|
|
$this->property, |
|
98
|
|
|
$this->removeExtension($this->file->getName(), $this->suffix) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function removeExtension(?string $s, ?string $ext) |
|
103
|
|
|
{ |
|
104
|
|
|
if (null === $ext || !StringHelper::endsWith($ext, $s)) { |
|
105
|
|
|
return $s; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return rtrim(substr($s, 0, -strlen($ext)), '.'); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|