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
 * Condition returns true if selected partition has the requested space, false otherwise.
22
 *
23
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
24
 * @package phing.tasks.system.condition
25
 */
26
class HasFreeSpaceCondition implements Condition
27
{
28
    /**
29
     * @var string $partition
30
     */
31
    private $partition;
32

33
    /**
34
     * @var string $needed
35
     */
36
    private $needed;
37

38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @throws BuildException
42
     *
43
     * @return boolean
44
     */
45 0
    public function evaluate()
46
    {
47 0
        $this->validate();
48

49 0
        $free = disk_free_space($this->partition);
50 0
        return $free >= $this->parseHumanSizes($this->needed);
51
    }
52

53
    /**
54
     * @return void
55
     *
56
     * @throws BuildException
57
     */
58 0
    private function validate()
59
    {
60 0
        if (null == $this->partition) {
61 0
            throw new BuildException("Please set the partition attribute.");
62
        }
63 0
        if (null == $this->needed) {
64 0
            throw new BuildException("Please set the needed attribute.");
65
        }
66
    }
67

68
    /**
69
     * Set the partition/device to check.
70
     *
71
     * @param $partition
72
     *
73
     * @return void
74
     */
75 0
    public function setPartition($partition)
76
    {
77 0
        $this->partition = $partition;
78
    }
79

80
    /**
81
     * Set the amount of free space required.
82
     *
83
     * @return void
84
     */
85 0
    public function setNeeded($needed)
86
    {
87 0
        $this->needed = $needed;
88
    }
89

90
    /**
91
     * @param string $humanSize
92
     *
93
     * @return float
94
     */
95 0
    private function parseHumanSizes($humanSize)
96
    {
97 0
        if (ctype_alpha($char = $humanSize[strlen($humanSize - 1)])) {
98 0
            $value = (float) substr($humanSize, 0, strlen($humanSize - 1));
99 0
            switch ($char) {
100 0
                case 'K':
101 0
                    return $value * 1024;
102 0
                case 'M':
103 0
                    return $value * 1024 * 1024;
104 0
                case 'G':
105 0
                    return $value * 1024 * 1024 * 1024;
106 0
                case 'T':
107 0
                    return $value * 1024 * 1024 * 1024 * 1024;
108 0
                case 'P':
109 0
                    return $value * 1024 * 1024 * 1024 * 1024 * 1024;
110 0
                default:
111 0
                    return $value;
112
            }
113 0
        } else {
114 0
            return (float) $humanSize;
115
        }
116
    }
117
}

Read our documentation on viewing source code .

Loading