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
110
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 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 isExpired
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Language\Dependency;
8
9/**
10 * Depend on a file.
11 *
12 * @newable
13 * @ingroup Language
14 */
15class FileDependency extends CacheDependency {
16    /**
17     * @var string
18     * @private Make private when FileDependency_compat.php is removed
19     */
20    protected $filename;
21    /**
22     * @var null|false|int
23     * @private Make private when FileDependency_compat.php is removed
24     */
25    protected $timestamp;
26
27    /**
28     * Create a file dependency
29     *
30     * @stable to call
31     *
32     * @param string $filename The name of the file, preferably fully qualified
33     * @param null|false|int $timestamp The unix last modified timestamp, or false if the
34     *        file does not exist. If omitted, the timestamp will be loaded from
35     *        the file.
36     *
37     * A dependency on a nonexistent file will be triggered when the file is
38     * created. A dependency on an existing file will be triggered when the
39     * file is changed.
40     */
41    public function __construct( $filename, $timestamp = null ) {
42        $this->filename = $filename;
43        $this->timestamp = $timestamp;
44    }
45
46    /**
47     * @return array
48     */
49    public function __sleep() {
50        $this->loadDependencyValues();
51
52        return [ 'filename', 'timestamp' ];
53    }
54
55    public function loadDependencyValues() {
56        if ( $this->timestamp === null ) {
57            # Dependency on a non-existent file stores "false"
58            # This is a valid concept!
59            // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
60            $this->timestamp = @filemtime( $this->filename );
61        }
62    }
63
64    /** @inheritDoc */
65    public function isExpired( $callback = null ) {
66        // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
67        $lastmod = @filemtime( $this->filename );
68        if ( $lastmod === false ) {
69            if ( $this->timestamp === false ) {
70                # Still nonexistent
71                return false;
72            }
73
74            # Deleted
75            wfDebug( "Dependency triggered: {$this->filename} deleted." );
76
77            if ( is_callable( $callback ) ) {
78                $callback( "{$this->filename} was deleted" );
79            }
80            return true;
81        }
82
83        if ( $lastmod > $this->timestamp ) {
84            # Modified or created
85            wfDebug( "Dependency triggered: {$this->filename} changed." );
86
87            if ( is_callable( $callback ) ) {
88                $callback( "{$this->filename} mtime changed from {$this->timestamp} to $lastmod" );
89            }
90            return true;
91        }
92
93        # Not modified
94        return false;
95    }
96}