30use Wikimedia\AtEase\AtEase;
51 private static $references;
59 parent::__construct(
$path );
61 if ( self::$pathsCollect ===
null ) {
63 self::$pathsCollect = [];
64 register_shutdown_function( [ __CLASS__,
'purgeAllOnShutdown' ] );
80 public static function factory( $prefix, $extension =
'', $tmpDirectory =
null ) {
81 return (
new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
92 $tmpDir = array_map(
'getenv', [
'TMPDIR',
'TMP',
'TEMP' ] );
93 $tmpDir[] = sys_get_temp_dir();
94 $tmpDir[] = ini_get(
'upload_tmp_dir' );
95 foreach ( $tmpDir as $tmp ) {
96 if ( $tmp !=
'' && is_dir( $tmp ) && is_writable( $tmp ) ) {
106 if ( PHP_OS_FAMILY ===
'Windows' ) {
107 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'mwtmp-' . get_current_user();
108 if ( !is_dir( $tmp ) ) {
111 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
116 throw new RuntimeException(
117 'No writable temporary directory could be found. ' .
118 'Please explicitly specify a writable directory in configuration.' );
127 $this->canDelete =
false;
128 AtEase::suppressWarnings();
129 $ok = unlink( $this->path );
130 AtEase::restoreWarnings();
132 unset( self::$pathsCollect[$this->path] );
143 public function bind( $object ) {
144 if ( is_object( $object ) ) {
146 if ( PHP_VERSION_ID >= 80000 ) {
147 if ( self::$references ===
null ) {
148 self::$references =
new WeakMap;
150 self::$references[$object] = $this;
153 if ( !isset( $object->tempFSFileReferences ) ) {
155 $object->tempFSFileReferences = [];
157 $object->tempFSFileReferences[] = $this;
170 $this->canDelete =
false;
172 unset( self::$pathsCollect[$this->path] );
183 $this->canDelete =
true;
198 foreach ( self::$pathsCollect as
$path => $unused ) {
199 AtEase::suppressWarnings();
201 AtEase::restoreWarnings();
209 if ( $this->canDelete ) {
216class_alias( TempFSFile::class,
'TempFSFile' );