Debian Codegraph @

includes/language/dependency/FileDependency.php

raw symbols SCIP
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
21 use Wikimedia\AtEase\AtEase;
22
23 /**
24 * Depend on a file.
25 *
26 * @newable
27 * @ingroup Language
28 */
29 class FileDependency extends CacheDependency {
30 /** @var string */
31 private $filename;
32 /** @var null|false|int */
33 private $timestamp;
34
35 /**
36 * Create a file dependency
37 *
38 * @stable to call
39 *
40 * @param string $filename The name of the file, preferably fully qualified
41 * @param null|false|int $timestamp The unix last modified timestamp, or false if the
42 * file does not exist. If omitted, the timestamp will be loaded from
43 * the file.
44 *
45 * A dependency on a nonexistent file will be triggered when the file is
46 * created. A dependency on an existing file will be triggered when the
47 * file is changed.
48 */
49 public function __construct( $filename, $timestamp = null ) {
50 $this->filename = $filename;
51 $this->timestamp = $timestamp;
52 }
53
54 /**
55 * @return array
56 */
57 public function __sleep() {
58 $this->loadDependencyValues();
59
60 return [ 'filename', 'timestamp' ];
61 }
62
63 public function loadDependencyValues() {
64 if ( $this->timestamp === null ) {
65 AtEase::suppressWarnings();
66 # Dependency on a non-existent file stores "false"
67 # This is a valid concept!
68 $this->timestamp = filemtime( $this->filename );
69 AtEase::restoreWarnings();
70 }
71 }
72
73 public function isExpired() {
74 AtEase::suppressWarnings();
75 $lastmod = filemtime( $this->filename );
76 AtEase::restoreWarnings();
77 if ( $lastmod === false ) {
78 if ( $this->timestamp === false ) {
79 # Still nonexistent
80 return false;
81 }
82
83 # Deleted
84 wfDebug( "Dependency triggered: {$this->filename} deleted." );
85
86 return true;
87 }
88
89 if ( $lastmod > $this->timestamp ) {
90 # Modified or created
91 wfDebug( "Dependency triggered: {$this->filename} changed." );
92
93 return true;
94 }
95
96 # Not modified
97 return false;
98 }
99 }