26use Wikimedia\AtEase\AtEase;
47 private static $references;
55 parent::__construct(
$path );
57 if ( self::$pathsCollect ===
null ) {
59 self::$pathsCollect = [];
60 register_shutdown_function( [ __CLASS__,
'purgeAllOnShutdown' ] );
76 public static function factory( $prefix, $extension =
'', $tmpDirectory =
null ) {
77 return (
new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
88 $tmpDir = array_map(
'getenv', [
'TMPDIR',
'TMP',
'TEMP' ] );
89 $tmpDir[] = sys_get_temp_dir();
90 $tmpDir[] = ini_get(
'upload_tmp_dir' );
91 foreach ( $tmpDir as $tmp ) {
92 if ( $tmp !=
'' && is_dir( $tmp ) && is_writable( $tmp ) ) {
102 if ( PHP_OS_FAMILY ===
'Windows' ) {
103 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'mwtmp-' . get_current_user();
104 if ( !is_dir( $tmp ) ) {
107 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
112 throw new RuntimeException(
113 'No writable temporary directory could be found. ' .
114 'Please explicitly specify a writable directory in configuration.' );
123 $this->canDelete =
false;
124 AtEase::suppressWarnings();
125 $ok = unlink( $this->path );
126 AtEase::restoreWarnings();
128 unset( self::$pathsCollect[$this->path] );
139 public function bind( $object ) {
140 if ( is_object( $object ) ) {
142 if ( PHP_VERSION_ID >= 80000 ) {
143 if ( self::$references ===
null ) {
144 self::$references =
new WeakMap;
146 self::$references[$object] = $this;
149 if ( !isset( $object->tempFSFileReferences ) ) {
151 $object->tempFSFileReferences = [];
153 $object->tempFSFileReferences[] = $this;
166 $this->canDelete =
false;
168 unset( self::$pathsCollect[$this->path] );
179 $this->canDelete =
true;
194 foreach ( self::$pathsCollect as
$path => $unused ) {
195 AtEase::suppressWarnings();
197 AtEase::restoreWarnings();
205 if ( $this->canDelete ) {
Class representing a non-directory file on the file system.
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
__construct( $path)
Do not call directly.
bind( $object)
Clean up the temporary file only after an object goes out of scope.
autocollect()
Set flag clean up after the temporary file.
__destruct()
Cleans up after the temporary file by deleting it.
purge()
Purge this file off the file system.
preserve()
Set flag to not clean up after the temporary file.
bool $canDelete
Garbage collect the temp file.
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 getUsableTempDirectory()
static array $pathsCollect
Map of (path => 1) for paths to delete on shutdown.