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 filters files based on the how deep in the directory
22
 * tree they are.
23
 *
24
 * @author Hans Lellelid <hans@xmpl.org> (Phing)
25
 * @author Bruce Atherton <bruce@callenish.com> (Ant)
26
 *
27
 * @package phing.types.selectors
28
 */
29
class DepthSelector extends BaseExtendSelector
30
{
31
    /**
32
     * @var int $min
33
     */
34
    public $min = -1;
35

36
    /**
37
     * @var int $max
38
     */
39
    public $max = -1;
40

41
    public const MIN_KEY = "min";
42
    public const MAX_KEY = "max";
43

44
    /**
45
     * @return string
46
     */
47 0
    public function __toString()
48
    {
49 0
        $buf = "{depthselector min: ";
50 0
        $buf .= $this->min;
51 0
        $buf .= " max: ";
52 0
        $buf .= $this->max;
53 0
        $buf .= "}";
54

55 0
        return $buf;
56
    }
57

58
    /**
59
     * The minimum depth below the basedir before a file is selected.
60
     *
61
     * @param int $min minimum directory levels below basedir to go
62
     *
63
     * @return void
64
     */
65 0
    public function setMin($min)
66
    {
67 0
        $this->min = (int) $min;
68
    }
69

70
    /**
71
     * The minimum depth below the basedir before a file is selected.
72
     *
73
     * @param int $max maximum directory levels below basedir to go
74
     *
75
     * @return void
76
     */
77 0
    public function setMax($max)
78
    {
79 0
        $this->max = (int) $max;
80
    }
81

82
    /**
83
     * When using this as a custom selector, this method will be called.
84
     * It translates each parameter into the appropriate setXXX() call.
85
     *
86
     * {@inheritdoc}
87
     *
88
     * @param array $parameters the complete set of parameters for this selector
89
     *
90
     * @return mixed|void
91
     */
92 0
    public function setParameters(array $parameters): void
93
    {
94 0
        parent::setParameters($parameters);
95 0
        if ($parameters !== null) {
96 0
            for ($i = 0, $size = count($parameters); $i < $size; $i++) {
97 0
                $paramname = $parameters[$i]->getName();
98 0
                switch (strtolower($paramname)) {
99
                    case self::MIN_KEY:
100 0
                        $this->setMin($parameters[$i]->getValue());
101 0
                        break;
102
                    case self::MAX_KEY:
103 0
                        $this->setMax($parameters[$i]->getValue());
104 0
                        break;
105

106
                    default:
107 0
                        $this->setError("Invalid parameter " . $paramname);
108
                } // switch
109
            }
110
        }
111
    }
112

113
    /**
114
     * Checks to make sure all settings are kosher. In this case, it
115
     * means that the max depth is not lower than the min depth.
116
     *
117
     * {@inheritdoc}
118
     *
119
     * @return void
120
     */
121 0
    public function verifySettings()
122
    {
123 0
        if ($this->min < 0 && $this->max < 0) {
124 0
            $this->setError(
125
                "You must set at least one of the min or the " .
126 0
                "max levels."
127
            );
128
        }
129 0
        if ($this->max < $this->min && $this->max > -1) {
130 0
            $this->setError("The maximum depth is lower than the minimum.");
131
        }
132
    }
133

134
    /**
135
     * The heart of the matter. This is where the selector gets to decide
136
     * on the inclusion of a file in a particular fileset. Most of the work
137
     * for this selector is offloaded into SelectorUtils, a static class
138
     * that provides the same services for both FilenameSelector and
139
     * DirectoryScanner.
140
     *
141
     * {@inheritdoc}
142
     *
143
     * @param PhingFile $basedir base directory the scan is being done from
144
     * @param string $filename the name of the file to check
145
     * @param PhingFile $file a PhingFile object the selector can use
146
     *
147
     * @throws BuildException
148
     *
149
     * @return bool whether the file should be selected or not
150
     */
151 0
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
152
    {
153 0
        $this->validate();
154

155 0
        $depth = -1;
156
        // If you felt daring, you could cache the basedir absolute path
157 0
        $abs_base = $basedir->getAbsolutePath();
158 0
        $abs_file = $file->getAbsolutePath();
159

160 0
        $tok_base = explode(DIRECTORY_SEPARATOR, $abs_base);
161 0
        $tok_file = explode(DIRECTORY_SEPARATOR, $abs_file);
162

163 0
        for ($i = 0, $size = count($tok_file); $i < $size; $i++) {
164 0
            $filetoken = $tok_file[$i];
165 0
            if (isset($tok_base[$i])) {
166 0
                $basetoken = $tok_base[$i];
167
                // Sanity check. Ditch it if you want faster performance
168 0
                if ($basetoken !== $filetoken) {
169 0
                    throw new BuildException(
170 0
                        "File " . $filename .
171 0
                        " does not appear within " . $abs_base . "directory"
172
                    );
173
                }
174
            } else { // no more basepath tokens
175 0
                $depth++;
176 0
                if ($this->max > -1 && $depth > $this->max) {
177 0
                    return false;
178
                }
179
            }
180
        }
181 0
        if (isset($tok_base[$i + 1])) {
182 0
            throw new BuildException(
183 0
                "File " . $filename .
184 0
                " is outside of " . $abs_base . "directory tree"
185
            );
186
        }
187 0
        if ($this->min > -1 && $depth < $this->min) {
188 0
            return false;
189
        }
190

191 0
        return true;
192
    }
193
}

Read our documentation on viewing source code .

Loading