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
 * fileHash
22
 *
23
 * Calculate either MD5 or SHA hash value of a specified file and retun the
24
 * value in a property
25
 *
26
 * @author  Johan Persson <johan162@gmail.com>
27
 * @package phing.tasks.ext
28
 */
29
class FileSizeTask extends Task
30
{
31
    /**
32
     * Property for File
33
     *
34
     * @var PhingFile file
35
     */
36
    private $file;
37

38
    /**
39
     * Property where the file size will be stored
40
     *
41
     * @var string $property
42
     */
43
    private $propertyName = "filesize";
44

45
    /**
46
     * Which file to calculate the file size of
47
     *
48
     * @param PhingFile $file
49
     */
50 1
    public function setFile($file)
51
    {
52 1
        $this->file = $file;
53
    }
54

55
    /**
56
     * Set the name of the property to store the file size
57
     *
58
     * @param  $property
59
     * @return void
60
     */
61 1
    public function setPropertyName($property)
62
    {
63 1
        $this->propertyName = $property;
64
    }
65

66
    /**
67
     * Main-Method for the Task
68
     *
69
     * @return void
70
     * @throws BuildException
71
     */
72 1
    public function main()
73
    {
74 1
        $this->checkFile();
75 1
        $this->checkPropertyName();
76

77 1
        $size = filesize($this->file);
78

79 1
        if ($size === false) {
80 0
            throw new BuildException(sprintf('[FileSize] Cannot determine size of file: %s', $this->file));
81
        }
82

83
        // publish hash value
84 1
        $this->project->setProperty($this->propertyName, $size);
85
    }
86

87
    /**
88
     * checks file attribute
89
     *
90
     * @return void
91
     * @throws BuildException
92
     */
93 1
    private function checkFile()
94
    {
95
        // check File
96
        if (
97 1
            $this->file === null
98 1
            || strlen($this->file) == 0
99
        ) {
100 0
            throw new BuildException('[FileSize] You must specify an input file.', $this->file);
101
        }
102

103 1
        if (!is_readable($this->file)) {
104 0
            throw new BuildException(
105 0
                sprintf(
106 0
                    '[FileSize] Input file does not exist or is not readable: %s',
107 0
                    $this->file
108
                )
109
            );
110
        }
111
    }
112

113
    /**
114
     * checks property attribute
115
     *
116
     * @return void
117
     * @throws BuildException
118
     */
119 1
    private function checkPropertyName()
120
    {
121
        if (
122 1
            null === $this->propertyName
123 1
            || strlen($this->propertyName) === 0
124
        ) {
125 0
            throw new BuildException('[FileSize] Property name for publishing file size is not set');
126
        }
127
    }
128
}

Read our documentation on viewing source code .

Loading