1<?php
2/*
3 * $Id: 2a59c1a9b46f3fd71df0fd3b50908eff268fd630 $
4 *
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 *
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the LGPL. For more information please see
19 * <http://phing.info>.
20 */
21require_once 'phing/Task.php';
22
23/**
24 * fileHash
25 *
26 * Calculate either MD5 or SHA hash value of a specified file and retun the
27 * value in a property
28 *
29 * @author      Johan Persson <johan162@gmail.com>
30 * @version     $Id: 2a59c1a9b46f3fd71df0fd3b50908eff268fd630 $
31 * @package     phing.tasks.ext
32 */
33class FileSizeTask extends Task
34{
35    /**
36     * Property for File
37     * @var PhingFile file
38     */
39    private $file;
40
41    /**
42     * Property where the file size will be stored
43     * @var string $property
44     */
45    private $propertyName = "filesize";
46
47    /**
48     * Which file to calculate the file size of
49     * @param PhingFile $file
50     */
51    public function setFile($file)
52    {
53        $this->file = $file;
54    }
55
56    /**
57     * Set the name of the property to store the file size
58     * @param $property
59     * @return void
60     */
61    public function setPropertyName($property)
62    {
63        $this->propertyName = $property;
64    }
65
66    /**
67     * Main-Method for the Task
68     *
69     * @return  void
70     * @throws  BuildException
71     */
72    public function main()
73    {
74        $this->checkFile();
75        $this->checkPropertyName();
76
77        $size = filesize($this->file);
78
79        if( $size === false ) {
80            throw new BuildException(sprintf('[FileSize] Cannot determine size of file: %s',$this->file));
81
82        }
83
84        // publish hash value
85        $this->project->setProperty($this->propertyName, $size);
86
87    }
88
89    /**
90     * checks file attribute
91     * @return void
92     * @throws BuildException
93     */
94    private function checkFile()
95    {
96        // check File
97        if ($this->file === null ||
98            strlen($this->file) == 0) {
99            throw new BuildException('[FileSize] You must specify an input file.', $this->file);
100        }
101
102        if( ! is_readable($this->file) ) {
103            throw new BuildException(sprintf('[FileSize] Input file does not exist or is not readable: %s',$this->file));
104        }
105
106    }
107
108    /**
109     * checks property attribute
110     * @return void
111     * @throws BuildException
112     */
113    private function checkPropertyName()
114    {
115        if (is_null($this->propertyName) ||
116            strlen($this->propertyName) === 0) {
117            throw new BuildException('[FileSize] Property name for publishing file size is not set');
118        }
119    }
120}