27use Wikimedia\AtEase\AtEase;
48 parent::__construct(
$path );
50 if ( self::$pathsCollect ===
null ) {
52 self::$pathsCollect = [];
53 register_shutdown_function( [ __CLASS__,
'purgeAllOnShutdown' ] );
69 public static function factory( $prefix, $extension =
'', $tmpDirectory =
null ) {
70 return (
new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
81 $tmpDir = array_map(
'getenv', [
'TMPDIR',
'TMP',
'TEMP' ] );
82 $tmpDir[] = sys_get_temp_dir();
83 $tmpDir[] = ini_get(
'upload_tmp_dir' );
84 foreach ( $tmpDir as $tmp ) {
85 if ( $tmp !=
'' && is_dir( $tmp ) && is_writable( $tmp ) ) {
95 if ( strtoupper( substr( PHP_OS, 0, 3 ) ) ===
'WIN' ) {
96 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'mwtmp-' . get_current_user();
97 if ( !file_exists( $tmp ) ) {
100 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
105 throw new RuntimeException(
106 'No writable temporary directory could be found. ' .
107 'Please explicitly specify a writable directory in configuration.' );
116 $this->canDelete =
false;
117 AtEase::suppressWarnings();
118 $ok = unlink( $this->path );
119 AtEase::restoreWarnings();
121 unset( self::$pathsCollect[$this->path] );
132 public function bind( $object ) {
133 if ( is_object( $object ) ) {
134 if ( !isset( $object->tempFSFileReferences ) ) {
136 $object->tempFSFileReferences = [];
138 $object->tempFSFileReferences[] = $this;
150 $this->canDelete =
false;
152 unset( self::$pathsCollect[$this->path] );
163 $this->canDelete =
true;
178 foreach ( self::$pathsCollect as
$path => $unused ) {
179 AtEase::suppressWarnings();
181 AtEase::restoreWarnings();
189 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.