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