Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FileDependency
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 __sleep
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 loadDependencyValues
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isExpired
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21use Wikimedia\AtEase\AtEase;
22
23/**
24 * Depend on a file.
25 *
26 * @newable
27 * @ingroup Language
28 */
29class FileDependency extends CacheDependency {
30    private $filename;
31    private $timestamp;
32
33    /**
34     * Create a file dependency
35     *
36     * @stable to call
37     *
38     * @param string $filename The name of the file, preferably fully qualified
39     * @param null|false|int $timestamp The unix last modified timestamp, or false if the
40     *        file does not exist. If omitted, the timestamp will be loaded from
41     *        the file.
42     *
43     * A dependency on a nonexistent file will be triggered when the file is
44     * created. A dependency on an existing file will be triggered when the
45     * file is changed.
46     */
47    public function __construct( $filename, $timestamp = null ) {
48        $this->filename = $filename;
49        $this->timestamp = $timestamp;
50    }
51
52    /**
53     * @return array
54     */
55    public function __sleep() {
56        $this->loadDependencyValues();
57
58        return [ 'filename', 'timestamp' ];
59    }
60
61    public function loadDependencyValues() {
62        if ( $this->timestamp === null ) {
63            AtEase::suppressWarnings();
64            # Dependency on a non-existent file stores "false"
65            # This is a valid concept!
66            $this->timestamp = filemtime( $this->filename );
67            AtEase::restoreWarnings();
68        }
69    }
70
71    public function isExpired() {
72        AtEase::suppressWarnings();
73        $lastmod = filemtime( $this->filename );
74        AtEase::restoreWarnings();
75        if ( $lastmod === false ) {
76            if ( $this->timestamp === false ) {
77                # Still nonexistent
78                return false;
79            }
80
81            # Deleted
82            wfDebug( "Dependency triggered: {$this->filename} deleted." );
83
84            return true;
85        }
86
87        if ( $lastmod > $this->timestamp ) {
88            # Modified or created
89            wfDebug( "Dependency triggered: {$this->filename} changed." );
90
91            return true;
92        }
93
94        # Not modified
95        return false;
96    }
97}