|
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
|
|
|
* FileSizeTask |
|
22
|
|
|
* |
|
23
|
|
|
* Returns the size of a file |
|
24
|
|
|
* |
|
25
|
|
|
* @author Johan Persson <[email protected]> |
|
26
|
|
|
* @package phing.tasks.ext |
|
27
|
|
|
*/ |
|
28
|
|
|
class FileSizeTask extends Task |
|
29
|
|
|
{ |
|
30
|
|
|
const UNITS = ['B', 'K', 'M', 'G', 'T', 'P']; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Property for File |
|
34
|
|
|
* |
|
35
|
|
|
* @var PhingFile file |
|
36
|
|
|
*/ |
|
37
|
|
|
private $file; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Property where the file size will be stored |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $propertyName = "filesize"; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Return size in this unit |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $unit = self::UNITS[0]; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Which file to calculate the file size of |
|
55
|
|
|
* |
|
56
|
|
|
* @param PhingFile $file |
|
57
|
|
|
*/ |
|
58
|
22 |
|
public function setFile(PhingFile $file) |
|
59
|
|
|
{ |
|
60
|
22 |
|
if (!$file->canRead()) { |
|
61
|
1 |
|
throw new BuildException(sprintf('Input file does not exist or is not readable: %s', $file->getName())); |
|
62
|
|
|
} |
|
63
|
21 |
|
$this->file = $file; |
|
64
|
21 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set the name of the property to store the file size |
|
68
|
|
|
* |
|
69
|
|
|
* @param $property |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
9 |
|
public function setPropertyName(string $property) |
|
73
|
|
|
{ |
|
74
|
9 |
|
if (empty($property)) { |
|
75
|
1 |
|
throw new BuildException('Property name cannot be empty'); |
|
76
|
|
|
} |
|
77
|
8 |
|
$this->propertyName = $property; |
|
78
|
8 |
|
} |
|
79
|
|
|
|
|
80
|
11 |
|
public function setUnit(string $originalUnit) |
|
81
|
|
|
{ |
|
82
|
11 |
|
$unit = strtoupper($originalUnit); |
|
83
|
11 |
|
if (!in_array($unit, self::UNITS)) { |
|
84
|
2 |
|
throw new BuildException(sprintf('Invalid unit: %s', $originalUnit)); |
|
85
|
|
|
} |
|
86
|
9 |
|
$this->unit = $unit; |
|
87
|
9 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Main-Method for the Task |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
* @throws BuildException |
|
94
|
|
|
*/ |
|
95
|
19 |
|
public function main() |
|
96
|
|
|
{ |
|
97
|
19 |
|
if (!($this->file instanceof PhingFile)) { |
|
|
|
|
|
|
98
|
1 |
|
throw new BuildException('Input file not specified'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
18 |
|
$size = filesize($this->file); |
|
102
|
|
|
|
|
103
|
18 |
|
if ($size === false) { |
|
104
|
|
|
throw new BuildException(sprintf('Cannot determine size of file: %s', $this->file)); |
|
105
|
|
|
} |
|
106
|
18 |
|
$this->log(sprintf('%s %s', $size, self::UNITS[0]), Project::MSG_VERBOSE); |
|
107
|
|
|
|
|
108
|
18 |
|
$size = $size / pow(1024, array_search($this->unit, self::UNITS, true)); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
18 |
|
if (is_float($size)) { |
|
|
|
|
|
|
111
|
6 |
|
$size = round($size, 2); |
|
112
|
|
|
} |
|
113
|
18 |
|
$this->log(sprintf('%s %s', $size, $this->unit), Project::MSG_INFO); |
|
114
|
|
|
|
|
115
|
|
|
// publish hash value |
|
116
|
18 |
|
$this->project->setProperty($this->propertyName, $size); |
|
117
|
18 |
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|