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 whether they contain a
22
 * particular string.
23
 *
24
 * @author  Hans Lellelid <hans@xmpl.org> (Phing)
25
 * @author  Bruce Atherton <bruce@callenish.com> (Ant)
26
 * @package phing.types.selectors
27
 */
28
class ContainsSelector extends BaseExtendSelector
29
{
30
    private $contains = null;
31
    private $casesensitive = true;
32
    public const CONTAINS_KEY = "text";
33
    public const CASE_KEY = "casesensitive";
34
    public const WHITESPACE_KEY = "ignorewhitespace";
35
    private $ignorewhitespace = false;
36

37
    /**
38
     * @return string
39
     */
40 0
    public function __toString()
41
    {
42 0
        $buf = "{containsselector text: ";
43 0
        $buf .= $this->contains;
44 0
        $buf .= " casesensitive: ";
45 0
        if ($this->casesensitive) {
46 0
            $buf .= "true";
47
        } else {
48 0
            $buf .= "false";
49
        }
50 0
        $buf .= " ignorewhitespace: ";
51 0
        if ($this->ignorewhitespace) {
52 0
            $buf .= "true";
53
        } else {
54 0
            $buf .= "false";
55
        }
56 0
        $buf .= "}";
57

58 0
        return $buf;
59
    }
60

61
    /**
62
     * The string to search for within a file.
63
     *
64
     * @param string $contains the string that a file must contain to be selected.
65
     */
66 0
    public function setText($contains)
67
    {
68 0
        $this->contains = $contains;
69
    }
70

71
    /**
72
     * Whether to ignore case in the string being searched.
73
     *
74
     * @param boolean $casesensitive whether to pay attention to case sensitivity
75
     */
76 0
    public function setCasesensitive($casesensitive)
77
    {
78 0
        $this->casesensitive = $casesensitive;
79
    }
80

81
    /**
82
     * @param boolean $ignoreWhitespace
83
     */
84 0
    public function setIgnoreWhitespace($ignoreWhitespace)
85
    {
86 0
        $this->ignorewhitespace = $ignoreWhitespace;
87
    }
88

89
    /**
90
     * When using this as a custom selector, this method will be called.
91
     * It translates each parameter into the appropriate setXXX() call.
92
     *
93
     * @param array $parameters the complete set of parameters for this selector
94
     * @return mixed|void
95
     */
96 0
    public function setParameters(array $parameters): void
97
    {
98 0
        parent::setParameters($parameters);
99 0
        if ($parameters !== null) {
100 0
            for ($i = 0, $size = count($parameters); $i < $size; $i++) {
101 0
                $paramname = $parameters[$i]->getName();
102 0
                switch (strtolower($paramname)) {
103
                    case self::CONTAINS_KEY:
104 0
                        $this->setText($parameters[$i]->getValue());
105 0
                        break;
106
                    case self::CASE_KEY:
107 0
                        $this->setCasesensitive($parameters[$i]->getValue());
108 0
                        break;
109
                    case self::WHITESPACE_KEY:
110 0
                        $this->setIgnoreWhitespace($parameters[$i]->getValue());
111 0
                        break;
112
                    default:
113 0
                        $this->setError("Invalid parameter " . $paramname);
114
                }
115
            } // for each param
116
        } // if params
117
    }
118

119
    /**
120
     * Checks to make sure all settings are kosher. In this case, it
121
     * means that the pattern attribute has been set.
122
     */
123 0
    public function verifySettings()
124
    {
125 0
        if ($this->contains === null) {
126 0
            $this->setError("The text attribute is required");
127
        }
128
    }
129

130
    /**
131
     * The heart of the matter. This is where the selector gets to decide
132
     * on the inclusion of a file in a particular fileset.
133
     *
134
     * @param PhingFile $basedir
135
     * @param string $filename
136
     * @param PhingFile $file
137
     *
138
     * @throws BuildException
139
     *
140
     * @internal param the $basedir base directory the scan is being done from
141
     * @internal param is $filename the name of the file to check
142
     * @internal param a $file PhingFile object the selector can use
143
     *
144
     * @return bool whether the file should be selected or not
145
     */
146 0
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
147
    {
148 0
        $this->validate();
149

150
        try {
151 0
            if ($file->isDirectory() || $file->isLink()) {
152 0
                return true;
153
            }
154 0
        } catch (IOException $ioe) {
155 0
            if (OsCondition::isOS('windows')) {
156 0
                return true;
157
            }
158

159 0
            throw new BuildException($ioe);
160
        }
161

162 0
        $userstr = $this->contains;
163 0
        if (!$this->casesensitive) {
164 0
            $userstr = strtolower($this->contains);
165
        }
166 0
        if ($this->ignorewhitespace) {
167 0
            $userstr = SelectorUtils::removeWhitespace($userstr);
168
        }
169

170 0
        $in = null;
171
        try {
172 0
            $in = new BufferedReader(new FileReader($file));
173 0
            $teststr = $in->readLine();
174 0
            while ($teststr !== null) {
175 0
                if (!$this->casesensitive) {
176 0
                    $teststr = strtolower($teststr);
177
                }
178 0
                if ($this->ignorewhitespace) {
179 0
                    $teststr = SelectorUtils::removeWhitespace($teststr);
180
                }
181 0
                if (strpos($teststr, $userstr) !== false) {
182 0
                    return true;
183
                }
184 0
                $teststr = $in->readLine();
185
            }
186

187 0
            $in->close();
188

189 0
            return false;
190 0
        } catch (IOException $ioe) {
191 0
            if ($in) {
192 0
                $in->close();
193
            }
194 0
            throw new BuildException("Could not read file " . $filename);
195
        }
196
    }
197
}

Read our documentation on viewing source code .

Loading