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
 * Selector that selects a certain kind of file: directory or regular file.
22
 *
23
 * @author  Hans Lellelid <hans@xmpl.org> (Phing)
24
 * @author  Jeff Turner <jefft@apache.org> (Ant)
25
 * @package phing.types.selectors
26
 */
27
class TypeSelector extends BaseExtendSelector
28
{
29
    private $type;
30

31
    /**
32
     * Key to used for parameterized custom selector
33
     */
34
    public const TYPE_KEY = "type";
35

36
    /**
37
     * Valid types
38
     */
39
    private static $types = ['file', 'dir', 'link'];
40

41
    /**
42
     * @return string A string describing this object
43
     */
44 1
    public function __toString()
45
    {
46 1
        $buf = "{typeselector type: " . $this->type . "}";
47

48 1
        return $buf;
49
    }
50

51
    /**
52
     * Set the type of file to require.
53
     *
54
     * @param string $type The type of file - 'file' or 'dir'
55
     */
56 1
    public function setType($type)
57
    {
58 1
        $this->type = $type;
59
    }
60

61
    /**
62
     * When using this as a custom selector, this method will be called.
63
     * It translates each parameter into the appropriate setXXX() call.
64
     *
65
     * @param array $parameters the complete set of parameters for this selector
66
     * @return mixed|void
67
     */
68 0
    public function setParameters(array $parameters): void
69
    {
70 0
        parent::setParameters($parameters);
71 0
        if ($parameters !== null) {
72 0
            for ($i = 0, $size = count($parameters); $i < $size; $i++) {
73 0
                $paramname = $parameters[$i]->getName();
74 0
                if (self::TYPE_KEY == strtolower($paramname)) {
75 0
                    $this->setType($parameters[$i]->getValue());
76
                } else {
77 0
                    $this->setError("Invalid parameter " . $paramname);
78
                }
79
            }
80
        }
81
    }
82

83
    /**
84
     * Checks to make sure all settings are kosher. In this case, it
85
     * means that the pattern attribute has been set.
86
     */
87 1
    public function verifySettings()
88
    {
89 1
        if ($this->type === null) {
90 0
            $this->setError("The type attribute is required");
91 1
        } elseif (!in_array($this->type, self::$types, true)) {
92 0
            $this->setError("Invalid type specified; must be one of (" . implode(self::$types) . ")");
93
        }
94
    }
95

96
    /**
97
     * The heart of the matter. This is where the selector gets to decide
98
     * on the inclusion of a file in a particular fileset.
99
     *
100
     * @param  PhingFile $basedir the base directory the scan is being done from
101
     * @param  string $filename is the name of the file to check
102
     * @param  PhingFile $file is a PhingFile object the selector can use
103
     * @return boolean   Whether the file should be selected or not
104
     */
105 1
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
106
    {
107
        // throw BuildException on error
108 1
        $this->validate();
109

110 1
        if ($file->isLink()) {
111 0
            if ($this->type == 'link') {
112 0
                return true;
113
            }
114

115 0
            $this->log(
116 0
                $file->getAbsolutePath() . " is a link, proceeding with " . $file->getCanonicalPath() . " instead.",
117 0
                Project::MSG_DEBUG
118
            );
119 0
            $file = new PhingFile($file->getCanonicalPath());
120
        }
121

122 1
        if ($file->isDirectory()) {
123 1
            return $this->type === 'dir';
124
        }
125

126 0
        return $this->type === 'file';
127
    }
128
}

Read our documentation on viewing source code .

Loading