MediaWiki master
TempFSFile.php
Go to the documentation of this file.
1<?php
2
12
13use RuntimeException;
14use WeakMap;
15
22class TempFSFile extends FSFile {
24 protected $canDelete = false;
25
27 protected static $pathsCollect = null;
28
35 private static $references;
36
42 public function __construct( $path ) {
43 parent::__construct( $path );
44
45 if ( self::$pathsCollect === null ) {
46 // @codeCoverageIgnoreStart
47 self::$pathsCollect = [];
48 register_shutdown_function( self::purgeAllOnShutdown( ... ) );
49 // @codeCoverageIgnoreEnd
50 }
51 }
52
64 public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
65 return ( new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
66 }
67
75 public static function getUsableTempDirectory() {
76 $tmpDir = array_map( 'getenv', [ 'TMPDIR', 'TMP', 'TEMP' ] );
77 $tmpDir[] = sys_get_temp_dir();
78 $tmpDir[] = ini_get( 'upload_tmp_dir' );
79 foreach ( $tmpDir as $tmp ) {
80 if ( $tmp != '' && is_dir( $tmp ) && is_writable( $tmp ) ) {
81 return $tmp;
82 }
83 }
84
85 throw new RuntimeException(
86 'No writable temporary directory could be found. ' .
87 'Please explicitly specify a writable directory in configuration.' );
88 }
89
95 public function purge() {
96 $this->canDelete = false; // done
97 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
98 $ok = @unlink( $this->path );
99
100 unset( self::$pathsCollect[$this->path] );
101
102 return $ok;
103 }
104
111 public function bind( $object ) {
112 if ( is_object( $object ) ) {
113 // Use a WeakMap to avoid dynamic property creation (T324894)
114 if ( self::$references === null ) {
115 self::$references = new WeakMap;
116 }
117 self::$references[$object] = $this;
118 }
119
120 return $this;
121 }
122
128 public function preserve() {
129 $this->canDelete = false;
130
131 unset( self::$pathsCollect[$this->path] );
132
133 return $this;
134 }
135
141 public function autocollect() {
142 $this->canDelete = true;
143
144 self::$pathsCollect[$this->path] = 1;
145
146 return $this;
147 }
148
156 public static function purgeAllOnShutdown() {
157 foreach ( self::$pathsCollect as $path => $unused ) {
158 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
159 @unlink( $path );
160 }
161 }
162
166 public function __destruct() {
167 if ( $this->canDelete ) {
168 $this->purge();
169 }
170 }
171}
172
174class_alias( TempFSFile::class, 'TempFSFile' );
Class representing a non-directory file on the file system.
Definition FSFile.php:20
string $path
Path to file.
Definition FSFile.php:22
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
purge()
Purge this file off the file system.
bool $canDelete
Garbage collect the temp file.
__construct( $path)
Do not call directly.
autocollect()
Set flag clean up after the temporary file.
__destruct()
Cleans up after the temporary file by deleting it.
static factory( $prefix, $extension='', $tmpDirectory=null)
Make a new temporary file on the file system.
static purgeAllOnShutdown()
Try to make sure that all files are purged on error.
static array $pathsCollect
Map of (path => 1) for paths to delete on shutdown.
bind( $object)
Clean up the temporary file only after an object goes out of scope.
preserve()
Set flag to not clean up after the temporary file.