35 private static $references;
43 parent::__construct(
$path );
45 if ( self::$pathsCollect ===
null ) {
47 self::$pathsCollect = [];
48 register_shutdown_function( self::purgeAllOnShutdown( ... ) );
64 public static function factory( $prefix, $extension =
'', $tmpDirectory =
null ) {
65 return (
new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
76 $tmpDir = array_map(
'getenv', [
'TMPDIR',
'TMP',
'TEMP' ] );
77 $tmpDir[] = sys_get_temp_dir();
78 $tmpDir[] = ini_get(
'upload_tmp_dir' );
79 foreach ( $tmpDir as $tmp ) {
80 if ( $tmp !=
'' && is_dir( $tmp ) && is_writable( $tmp ) ) {
90 if ( PHP_OS_FAMILY ===
'Windows' ) {
91 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'mwtmp-' . get_current_user();
92 if ( !is_dir( $tmp ) ) {
95 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
100 throw new RuntimeException(
101 'No writable temporary directory could be found. ' .
102 'Please explicitly specify a writable directory in configuration.' );
111 $this->canDelete =
false;
113 $ok = @unlink( $this->path );
115 unset( self::$pathsCollect[$this->path] );
126 public function bind( $object ) {
127 if ( is_object( $object ) ) {
129 if ( self::$references ===
null ) {
130 self::$references =
new WeakMap;
132 self::$references[$object] = $this;
144 $this->canDelete =
false;
146 unset( self::$pathsCollect[$this->path] );
157 $this->canDelete =
true;
172 foreach ( self::$pathsCollect as
$path => $unused ) {
182 if ( $this->canDelete ) {
189class_alias( TempFSFile::class,
'TempFSFile' );