15use Wikimedia\AtEase\AtEase;
36 private static $references;
44 parent::__construct(
$path );
46 if ( self::$pathsCollect ===
null ) {
48 self::$pathsCollect = [];
49 register_shutdown_function( self::purgeAllOnShutdown( ... ) );
65 public static function factory( $prefix, $extension =
'', $tmpDirectory =
null ) {
66 return (
new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
77 $tmpDir = array_map(
'getenv', [
'TMPDIR',
'TMP',
'TEMP' ] );
78 $tmpDir[] = sys_get_temp_dir();
79 $tmpDir[] = ini_get(
'upload_tmp_dir' );
80 foreach ( $tmpDir as $tmp ) {
81 if ( $tmp !=
'' && is_dir( $tmp ) && is_writable( $tmp ) ) {
91 if ( PHP_OS_FAMILY ===
'Windows' ) {
92 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'mwtmp-' . get_current_user();
93 if ( !is_dir( $tmp ) ) {
96 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
101 throw new RuntimeException(
102 'No writable temporary directory could be found. ' .
103 'Please explicitly specify a writable directory in configuration.' );
112 $this->canDelete =
false;
113 AtEase::suppressWarnings();
114 $ok = unlink( $this->path );
115 AtEase::restoreWarnings();
117 unset( self::$pathsCollect[$this->path] );
128 public function bind( $object ) {
129 if ( is_object( $object ) ) {
131 if ( self::$references ===
null ) {
132 self::$references =
new WeakMap;
134 self::$references[$object] = $this;
146 $this->canDelete =
false;
148 unset( self::$pathsCollect[$this->path] );
159 $this->canDelete =
true;
174 foreach ( self::$pathsCollect as
$path => $unused ) {
175 AtEase::suppressWarnings();
177 AtEase::restoreWarnings();
185 if ( $this->canDelete ) {
192class_alias( TempFSFile::class,
'TempFSFile' );