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 appear in another
22
 * directory tree. It can contain a mapper element, so isn't available
23
 * as an ExtendSelector (since those parameters can't hold other
24
 * elements).
25
 *
26
 * @author Hans Lellelid <hans@xmpl.org> (Phing)
27
 * @author Bruce Atherton <bruce@callenish.com> (Ant)
28
 *
29
 * @package phing.types.selectors
30
 */
31
class PresentSelector extends BaseSelector
32
{
33
    private $targetdir = null;
34
    private $mapperElement = null;
35
    private $map = null;
36
    private $destmustexist = true;
37
    private static $filePresence = ["srconly", "both"];
38

39
    /**
40
     * @return string
41
     */
42 0
    public function __toString()
43
    {
44 0
        $buf = "{presentselector targetdir: ";
45 0
        if ($this->targetdir === null) {
46 0
            $buf .= "NOT YET SET";
47
        } else {
48 0
            $buf .= $this->targetdir->getName();
49
        }
50 0
        $buf .= " present: ";
51 0
        if ($this->destmustexist) {
52 0
            $buf .= "both";
53
        } else {
54 0
            $buf .= "srconly";
55
        }
56 0
        if ($this->map !== null) {
57 0
            $buf .= (string) $this->map;
58 0
        } elseif ($this->mapperElement !== null) {
59 0
            $buf .= (string) $this->mapperElement;
60
        }
61 0
        $buf .= "}";
62

63 0
        return $buf;
64
    }
65

66
    /**
67
     * The name of the file or directory which is checked for matching
68
     * files.
69
     *
70
     * @param PhingFile $targetdir the directory to scan looking for matching files.
71
     *
72
     * @return void
73
     */
74 0
    public function setTargetdir(PhingFile $targetdir)
75
    {
76 0
        $this->targetdir = $targetdir;
77
    }
78

79
    /**
80
     * Defines the FileNameMapper to use (nested mapper element).
81
     *
82
     * @return Mapper
83
     *
84
     * @throws BuildException
85
     */
86 0
    public function createMapper()
87
    {
88 0
        if ($this->mapperElement !== null) {
89 0
            throw new BuildException("Cannot define more than one mapper");
90
        }
91 0
        $this->mapperElement = new Mapper($this->getProject());
92

93 0
        return $this->mapperElement;
94
    }
95

96
    /**
97
     * This sets whether to select a file if its dest file is present.
98
     * It could be a <code>negate</code> boolean, but by doing things
99
     * this way, we get some documentation on how the system works.
100
     * A user looking at the documentation should clearly understand
101
     * that the ONLY files whose presence is being tested are those
102
     * that already exist in the source directory, hence the lack of
103
     * a <code>destonly</code> option.
104
     *
105
     * @param string $fp An attribute set to either <code>srconly</code> or
106
     *                   <code>both</code>.
107
     *
108
     * @return void
109
     */
110 0
    public function setPresent($fp)
111
    {
112 0
        $idx = array_search($fp, self::$filePresence, true);
113 0
        if ($idx === 0) {
114 0
            $this->destmustexist = false;
115
        }
116
    }
117

118
    /**
119
     * Checks to make sure all settings are kosher. In this case, it
120
     * means that the targetdir attribute has been set and we have a mapper.
121
     *
122
     * @return void
123
     */
124 0
    public function verifySettings()
125
    {
126 0
        if ($this->targetdir === null) {
127 0
            $this->setError("The targetdir attribute is required.");
128
        }
129 0
        if ($this->mapperElement === null) {
130 0
            $this->map = new IdentityMapper();
131
        } else {
132 0
            $this->map = $this->mapperElement->getImplementation();
133
        }
134 0
        if ($this->map === null) {
135 0
            $this->setError("Could not set <mapper> element.");
136
        }
137
    }
138

139
    /**
140
     * The heart of the matter. This is where the selector gets to decide
141
     * on the inclusion of a file in a particular fileset.
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
        // Determine file whose existence is to be checked
156 0
        $destfiles = $this->map->main($filename);
157
        // If filename does not match the To attribute of the mapper
158
        // then filter it out of the files we are considering
159 0
        if ($destfiles === null) {
160 0
            return false;
161
        }
162
        // Sanity check
163 0
        if (count($destfiles) !== 1 || $destfiles[0] === null) {
164 0
            throw new BuildException(
165
                "Invalid destination file results for "
166 0
                . $this->targetdir . " with filename " . $filename
167
            );
168
        }
169 0
        $destname = $destfiles[0];
170 0
        $destfile = new PhingFile($this->targetdir, $destname);
171

172 0
        return $destfile->exists() === $this->destmustexist;
173
    }
174
}

Read our documentation on viewing source code .

Loading