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
 * <available> task.
22
 *
23
 * Note: implements condition interface (see condition/Condition.php)
24
 *
25
 * @author    Andreas Aderhold <andi@binarycloud.com>
26
 * @copyright 2001,2002 THYRELL. All rights reserved
27
 * @package   phing.tasks.system
28
 */
29
class AvailableTask extends Task implements Condition
30
{
31

32
    /**
33
     * Property to check for.
34
     */
35
    private $property;
36

37
    /**
38
     * Value property should be set to.
39
     */
40
    private $value = "true";
41

42
    /**
43
     * File/directory to check existence
44
     */
45
    private $file;
46

47
    /**
48
     * Resource to check for
49
     */
50
    private $resource;
51

52
    /**
53
     * Extension to check if is loaded
54
     */
55
    private $extension;
56

57
    private $type = null;
58
    private $filepath = null;
59

60
    private $followSymlinks = false;
61

62
    /**
63
     * @param $property
64
     */
65 1
    public function setProperty($property)
66
    {
67 1
        $this->property = (string) $property;
68
    }
69

70
    /**
71
     * @param $value
72
     */
73 0
    public function setValue($value)
74
    {
75 0
        $this->value = (string) $value;
76
    }
77

78
    /**
79
     * @param PhingFile $file
80
     */
81 1
    public function setFile(PhingFile $file)
82
    {
83 1
        $this->file = $file;
84
    }
85

86
    /**
87
     * @param $resource
88
     */
89 0
    public function setResource($resource)
90
    {
91 0
        $this->resource = (string) $resource;
92
    }
93

94
    /**
95
     * @param $extension
96
     */
97 0
    public function setExtension($extension)
98
    {
99 0
        $this->extension = (string) $extension;
100
    }
101

102
    /**
103
     * @param $type
104
     */
105 1
    public function setType($type)
106
    {
107 1
        $this->type = (string) strtolower($type);
108
    }
109

110
    /**
111
     * @param $followSymlinks
112
     */
113 1
    public function setFollowSymlinks(bool $followSymlinks)
114
    {
115 1
        $this->followSymlinks = $followSymlinks;
116
    }
117

118
    /**
119
     * Set the path to use when looking for a file.
120
     *
121
     * @param Path $filepath a Path instance containing the search path for files.
122
     */
123 0
    public function setFilepath(Path $filepath)
124
    {
125 0
        if ($this->filepath === null) {
126 0
            $this->filepath = $filepath;
127
        } else {
128 0
            $this->filepath->append($filepath);
129
        }
130
    }
131

132
    /**
133
     * Creates a path to be configured
134
     *
135
     * @return Path
136
     */
137 0
    public function createFilepath()
138
    {
139 0
        if ($this->filepath === null) {
140 0
            $this->filepath = new Path($this->project);
141
        }
142

143 0
        return $this->filepath->createPath();
144
    }
145

146 1
    public function main()
147
    {
148 1
        if ($this->property === null) {
149 0
            throw new BuildException("property attribute is required", $this->getLocation());
150
        }
151 1
        if ($this->evaluate()) {
152 1
            $this->project->setProperty($this->property, $this->value);
153
        }
154
    }
155

156
    /**
157
     * @return bool
158
     * @throws BuildException
159
     */
160 1
    public function evaluate()
161
    {
162 1
        if ($this->file === null && $this->resource === null && $this->extension === null) {
163 0
            throw new BuildException("At least one of (file|resource|extension) is required", $this->getLocation());
164
        }
165

166 1
        if ($this->type !== null && ($this->type !== "file" && $this->type !== "dir")) {
167 0
            throw new BuildException("Type must be one of either dir or file", $this->getLocation());
168
        }
169

170 1
        if (($this->file !== null) && !$this->_checkFile()) {
171 1
            $this->log(
172 1
                "Unable to find " . $this->file->__toString() . " to set property " . $this->property,
173 1
                Project::MSG_VERBOSE
174
            );
175

176 1
            return false;
177
        }
178

179 1
        if (($this->resource !== null) && !$this->_checkResource($this->resource)) {
180 0
            $this->log(
181 0
                "Unable to load resource " . $this->resource . " to set property " . $this->property,
182 0
                Project::MSG_VERBOSE
183
            );
184

185 0
            return false;
186
        }
187

188 1
        if ($this->extension !== null && !extension_loaded($this->extension)) {
189 0
            $this->log(
190 0
                "Unable to load extension " . $this->extension . " to set property " . $this->property,
191 0
                Project::MSG_VERBOSE
192
            );
193

194 0
            return false;
195
        }
196

197 1
        return true;
198
    }
199

200
    // this is prepared for the path type
201

202
    /**
203
     * @return bool
204
     */
205 1
    private function _checkFile()
206
    {
207 1
        if ($this->filepath === null) {
208 1
            return $this->_checkFile1($this->file);
209
        }
210

211 0
        $paths = $this->filepath->listPaths();
212 0
        foreach ($paths as $path) {
213 0
            $this->log("Searching " . $path, Project::MSG_VERBOSE);
214 0
            $tmp = new PhingFile($path, $this->file->getName());
215 0
            if ($tmp->isFile()) {
216 0
                return true;
217
            }
218
        }
219

220 0
        return false;
221
    }
222

223
    /**
224
     * @param PhingFile $file
225
     * @return bool
226
     * @throws IOException
227
     */
228 1
    private function _checkFile1(PhingFile $file)
229
    {
230
        // Resolve symbolic links
231 1
        if ($this->followSymlinks && $file->isLink()) {
232 1
            $linkTarget = new PhingFile($file->getLinkTarget());
233 1
            if ($linkTarget->isAbsolute()) {
234 1
                $file = $linkTarget;
235
            } else {
236 1
                $fs = FileSystem::getFileSystem();
237 1
                $file = new PhingFile(
238 1
                    $fs->resolve(
239 1
                        $fs->normalize($file->getParent()),
240 1
                        $fs->normalize($file->getLinkTarget())
241
                    )
242
                );
243
            }
244
        }
245

246 1
        if ($this->type !== null) {
247 1
            if ($this->type === "dir") {
248 1
                return $file->isDirectory();
249
            }
250

251 1
            if ($this->type === "file") {
252 1
                return $file->isFile();
253
            }
254
        }
255

256 1
        return $file->exists();
257
    }
258

259
    /**
260
     * @param $resource
261
     * @return bool
262
     */
263 0
    private function _checkResource($resource)
264
    {
265 0
        if (null != ($resourcePath = Phing::getResourcePath($resource))) {
266 0
            return $this->_checkFile1(new PhingFile($resourcePath));
267
        }
268

269 0
        return false;
270
    }
271
}

Read our documentation on viewing source code .

Loading