/** @var bool Garbage collect the temp file */
protected $canDelete = false;
- /** @var array Active temp files to purge on shutdown */
- protected static $instances = [];
-
/** @var array Map of (path => 1) for paths to delete on shutdown */
protected static $pathsCollect = null;
* @return TempFSFile|null
*/
public static function factory( $prefix, $extension = '' ) {
- $base = wfTempDir() . '/' . $prefix . wfRandomString( 12 );
- $ext = ( $extension != '' ) ? ".{$extension}" : "";
- for ( $attempt = 1; true; $attempt++ ) {
- $path = "{$base}-{$attempt}{$ext}";
+ $ext = ( $extension != '' ) ? ".{$extension}" : '';
+
+ $attempts = 5;
+ while ( $attempts-- ) {
+ $path = wfTempDir() . '/' . $prefix . wfRandomString( 12 ) . $ext;
MediaWiki\suppressWarnings();
$newFileHandle = fopen( $path, 'x' );
MediaWiki\restoreWarnings();
if ( $newFileHandle ) {
fclose( $newFileHandle );
- break; // got it
- }
- if ( $attempt >= 5 ) {
- return null; // give up
+ $tmpFile = new self( $path );
+ $tmpFile->autocollect();
+ // Safely instantiated, end loop.
+ return $tmpFile;
}
}
- $tmpFile = new self( $path );
- $tmpFile->autocollect(); // safely instantiated
- return $tmpFile;
+ // Give up
+ return null;
}
/**