29use Wikimedia\AtEase\AtEase;
50 private static $references;
58 parent::__construct(
$path );
60 if ( self::$pathsCollect ===
null ) {
62 self::$pathsCollect = [];
63 register_shutdown_function( [ __CLASS__,
'purgeAllOnShutdown' ] );
79 public static function factory( $prefix, $extension =
'', $tmpDirectory =
null ) {
80 return (
new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
91 $tmpDir = array_map(
'getenv', [
'TMPDIR',
'TMP',
'TEMP' ] );
92 $tmpDir[] = sys_get_temp_dir();
93 $tmpDir[] = ini_get(
'upload_tmp_dir' );
94 foreach ( $tmpDir as $tmp ) {
95 if ( $tmp !=
'' && is_dir( $tmp ) && is_writable( $tmp ) ) {
105 if ( PHP_OS_FAMILY ===
'Windows' ) {
106 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'mwtmp-' . get_current_user();
107 if ( !is_dir( $tmp ) ) {
110 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
115 throw new RuntimeException(
116 'No writable temporary directory could be found. ' .
117 'Please explicitly specify a writable directory in configuration.' );
126 $this->canDelete =
false;
127 AtEase::suppressWarnings();
128 $ok = unlink( $this->path );
129 AtEase::restoreWarnings();
131 unset( self::$pathsCollect[$this->path] );
142 public function bind( $object ) {
143 if ( is_object( $object ) ) {
145 if ( PHP_VERSION_ID >= 80000 ) {
146 if ( self::$references ===
null ) {
147 self::$references =
new WeakMap;
149 self::$references[$object] = $this;
152 if ( !isset( $object->tempFSFileReferences ) ) {
154 $object->tempFSFileReferences = [];
156 $object->tempFSFileReferences[] = $this;
169 $this->canDelete =
false;
171 unset( self::$pathsCollect[$this->path] );
182 $this->canDelete =
true;
197 foreach ( self::$pathsCollect as
$path => $unused ) {
198 AtEase::suppressWarnings();
200 AtEase::restoreWarnings();
208 if ( $this->canDelete ) {
215class_alias( TempFSFile::class,
'TempFSFile' );