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
 * Sets the given property if the specified target has a timestamp
22
 * greater than all of the source files.
23
 *
24
 * @author  Hans Lellelid <hans@xmpl.org> (Phing)
25
 * @author  William Ferguson <williamf@mincom.com> (Ant)
26
 * @author  Hiroaki Nakamura <hnakamur@mc.neweb.ne.jp> (Ant)
27
 * @author  Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
28
 * @package phing.tasks.system
29
 */
30
class UpToDateTask extends Task implements Condition
31
{
32
    use FileListAware;
33
    use FileSetAware;
34

35
    /**
36
     * @var string
37
     */
38
    private $property;
39

40
    /**
41
     * @var string
42
     */
43
    private $value;
44

45
    /**
46
     * @var PhingFile
47
     */
48
    private $sourceFile;
49

50
    /**
51
     * @var PhingFile
52
     */
53
    private $targetFile;
54

55
    protected $mapperElement = null;
56

57
    /**
58
     * The property to set if the target file is more up-to-date than
59
     * (each of) the source file(s).
60
     *
61
     * @param string $property the name of the property to set if Target is up-to-date.
62
     */
63 1
    public function setProperty($property)
64
    {
65 1
        $this->property = $property;
66
    }
67

68
    /**
69
     * Get property name
70
     *
71
     * @return string property the name of the property to set if Target is up-to-date.
72
     */
73 1
    public function getProperty()
74
    {
75 1
        return $this->property;
76
    }
77

78
    /**
79
     * The value to set the named property to if the target file is more
80
     * up-to-date than (each of) the source file(s). Defaults to 'true'.
81
     *
82
     * @param mixed $value the value to set the property to if Target is up-to-date
83
     */
84 1
    public function setValue($value)
85
    {
86 1
        $this->value = $value;
87
    }
88

89
    /**
90
     * Returns the value, or "true" if a specific value wasn't provided.
91
     */
92 1
    private function getValue()
93
    {
94 1
        return $this->value ?? "true";
95
    }
96

97
    /**
98
     * The file which must be more up-to-date than (each of) the source file(s)
99
     * if the property is to be set.
100
     *
101
     * @param string|PhingFile $file the file we are checking against.
102
     */
103 1
    public function setTargetFile($file)
104
    {
105 1
        if (is_string($file)) {
106 1
            $file = new PhingFile($file);
107
        }
108 1
        $this->targetFile = $file;
109
    }
110

111
    /**
112
     * The file that must be older than the target file
113
     * if the property is to be set.
114
     *
115
     * @param string|PhingFile $file the file we are checking against the target file.
116
     */
117 1
    public function setSrcfile($file)
118
    {
119 1
        if (is_string($file)) {
120 1
            $file = new PhingFile($file);
121
        }
122 1
        $this->sourceFile = $file;
123
    }
124

125
    /**
126
     * Defines the FileNameMapper to use (nested mapper element).
127
     */
128 0
    public function createMapper()
129
    {
130 0
        if ($this->mapperElement !== null) {
131 0
            throw new BuildException(
132 0
                "Cannot define more than one mapper",
133 0
                $this->getLocation()
134
            );
135
        }
136 0
        $this->mapperElement = new Mapper($this->getProject());
137

138 0
        return $this->mapperElement;
139
    }
140

141
    /**
142
     * Evaluate (all) target and source file(s) to
143
     * see if the target(s) is/are up-to-date.
144
     *
145
     * @throws BuildException
146
     * @return boolean
147
     */
148 1
    public function evaluate()
149
    {
150 1
        if (count($this->filesets) == 0 && count($this->filelists) == 0 && $this->sourceFile === null) {
151 0
            throw new BuildException(
152
                "At least one srcfile or a nested "
153 0
                . "<fileset> or <filelist> element must be set."
154
            );
155
        }
156

157 1
        if ((count($this->filesets) > 0 || count($this->filelists) > 0) && $this->sourceFile !== null) {
158 0
            throw new BuildException(
159
                "Cannot specify both the srcfile "
160
                . "attribute and a nested <fileset> "
161 0
                . "or <filelist> element."
162
            );
163
        }
164

165 1
        if ($this->targetFile === null && $this->mapperElement === null) {
166 0
            throw new BuildException(
167
                "The targetfile attribute or a nested "
168 0
                . "mapper element must be set."
169
            );
170
        }
171

172
        // if the target file is not there, then it can't be up-to-date
173 1
        if ($this->targetFile !== null && !$this->targetFile->exists()) {
174 0
            return false;
175
        }
176

177
        // if the source file isn't there, throw an exception
178 1
        if ($this->sourceFile !== null && !$this->sourceFile->exists()) {
179 0
            throw new BuildException(
180 0
                $this->sourceFile->getAbsolutePath()
181 0
                . " not found."
182
            );
183
        }
184

185 1
        $upToDate = true;
186 1
        for ($i = 0, $size = count($this->filesets); $i < $size && $upToDate; $i++) {
187 0
            $fs = $this->filesets[$i];
188 0
            $ds = $fs->getDirectoryScanner($this->project);
189 0
            $upToDate = $upToDate && $this->scanDir(
190 0
                $fs->getDir($this->project),
191 0
                $ds->getIncludedFiles()
192
            );
193
        }
194

195 1
        for ($i = 0, $size = count($this->filelists); $i < $size && $upToDate; $i++) {
196 0
            $fl = $this->filelists[$i];
197 0
            $srcFiles = $fl->getFiles($this->project);
198 0
            $upToDate = $upToDate && $this->scanDir(
199 0
                $fl->getDir($this->project),
200
                $srcFiles
201
            );
202
        }
203

204 1
        if ($this->sourceFile !== null) {
205 1
            if ($this->mapperElement === null) {
206 1
                $upToDate = $upToDate &&
207 1
                    ($this->targetFile->lastModified() >= $this->sourceFile->lastModified());
208
            } else {
209 0
                $sfs = new SourceFileScanner($this);
210 0
                $upToDate = $upToDate &&
211
                    count(
212 0
                        $sfs->restrict(
213 0
                            $this->sourceFile->getAbsolutePath(),
214 0
                            null,
215 0
                            null,
216 0
                            $this->mapperElement->getImplementation()
217
                        )
218 0
                    ) === 0;
219
            }
220
        }
221

222 1
        return $upToDate;
223
    }
224

225

226
    /**
227
     * Sets property to true if target file(s) have a more recent timestamp
228
     * than (each of) the corresponding source file(s).
229
     *
230
     * @throws BuildException
231
     */
232 1
    public function main()
233
    {
234 1
        if ($this->property === null) {
235 0
            throw new BuildException(
236 0
                "property attribute is required.",
237 0
                $this->getLocation()
238
            );
239
        }
240 1
        $upToDate = $this->evaluate();
241 1
        if ($upToDate) {
242 1
            $property = $this->project->createTask('property');
243 1
            $property->setName($this->getProperty());
244 1
            $property->setValue($this->getValue());
245 1
            $property->setOverride(true);
246 1
            $property->main(); // execute
247

248 1
            if ($this->mapperElement === null) {
249 1
                $this->log(
250 1
                    "File \"" . $this->targetFile->getAbsolutePath()
251 1
                    . "\" is up-to-date.",
252 1
                    Project::MSG_VERBOSE
253
                );
254
            } else {
255 0
                $this->log(
256 0
                    "All target files are up-to-date.",
257 0
                    Project::MSG_VERBOSE
258
                );
259
            }
260
        }
261
    }
262

263
    /**
264
     * @param PhingFile $srcDir
265
     * @param $files
266
     * @return bool
267
     */
268 0
    protected function scanDir(PhingFile $srcDir, $files)
269
    {
270 0
        $sfs = new SourceFileScanner($this);
271 0
        $mapper = null;
272 0
        $dir = $srcDir;
273 0
        if ($this->mapperElement === null) {
274 0
            $mm = new MergeMapper();
275 0
            $mm->setTo($this->targetFile->getAbsolutePath());
276 0
            $mapper = $mm;
277 0
            $dir = null;
278
        } else {
279 0
            $mapper = $this->mapperElement->getImplementation();
280
        }
281

282 0
        return (count($sfs->restrict($files, $srcDir, $dir, $mapper)) === 0);
283
    }
284
}

Read our documentation on viewing source code .

Loading