27 use 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 ) {