27use Wikimedia\AtEase\AtEase;
48 private static $references;
56 parent::__construct(
$path );
58 if ( self::$pathsCollect ===
null ) {
60 self::$pathsCollect = [];
61 register_shutdown_function( [ __CLASS__,
'purgeAllOnShutdown' ] );
77 public static function factory( $prefix, $extension =
'', $tmpDirectory =
null ) {
78 return (
new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
89 $tmpDir = array_map(
'getenv', [
'TMPDIR',
'TMP',
'TEMP' ] );
90 $tmpDir[] = sys_get_temp_dir();
91 $tmpDir[] = ini_get(
'upload_tmp_dir' );
92 foreach ( $tmpDir as $tmp ) {
93 if ( $tmp !=
'' && is_dir( $tmp ) && is_writable( $tmp ) ) {
103 if ( PHP_OS_FAMILY ===
'Windows' ) {
104 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'mwtmp-' . get_current_user();
105 if ( !is_dir( $tmp ) ) {
108 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
113 throw new RuntimeException(
114 'No writable temporary directory could be found. ' .
115 'Please explicitly specify a writable directory in configuration.' );
124 $this->canDelete =
false;
125 AtEase::suppressWarnings();
126 $ok = unlink( $this->path );
127 AtEase::restoreWarnings();
129 unset( self::$pathsCollect[$this->path] );
140 public function bind( $object ) {
141 if ( is_object( $object ) ) {
143 if ( PHP_VERSION_ID >= 80000 ) {
144 if ( self::$references ===
null ) {
145 self::$references =
new WeakMap;
147 self::$references[$object] = $this;
150 if ( !isset( $object->tempFSFileReferences ) ) {
152 $object->tempFSFileReferences = [];
154 $object->tempFSFileReferences[] = $this;
167 $this->canDelete =
false;
169 unset( self::$pathsCollect[$this->path] );
180 $this->canDelete =
true;
195 foreach ( self::$pathsCollect as
$path => $unused ) {
196 AtEase::suppressWarnings();
198 AtEase::restoreWarnings();
206 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.