/** @var array Active temp files to purge on shutdown */
protected static $instances = array();
+ /** @var array Map of (path => 1) for paths to delete on shutdown */
+ protected static $pathsCollect = null;
+
+ public function __construct( $path ) {
+ parent::__construct( $path );
+
+ if ( self::$pathsCollect === null ) {
+ self::$pathsCollect = array();
+ register_shutdown_function( array( __CLASS__, 'purgeAllOnShutdown' ) );
+ }
+ }
+
/**
* Make a new temporary file on the file system.
* Temporary files may be purged when the file object falls out of scope.
}
}
$tmpFile = new self( $path );
- $tmpFile->canDelete = true; // safely instantiated
+ $tmpFile->autocollect(); // safely instantiated
wfProfileOut( __METHOD__ );
return $tmpFile;
$ok = unlink( $this->path );
wfRestoreWarnings();
+ unset( self::$pathsCollect[$this->path] );
+
return $ok;
}
public function preserve() {
$this->canDelete = false;
+ unset( self::$pathsCollect[$this->path] );
+
return $this;
}
public function autocollect() {
$this->canDelete = true;
+ self::$pathsCollect[$this->path] = 1;
+
return $this;
}
+ /**
+ * Try to make sure that all files are purged on error
+ *
+ * This method should only be called internally
+ */
+ public static function purgeAllOnShutdown() {
+ foreach ( self::$pathsCollect as $path ) {
+ wfSuppressWarnings();
+ unlink( $path );
+ wfRestoreWarnings();
+ }
+ }
+
/**
* Cleans up after the temporary file by deleting it
*/
function __destruct() {
if ( $this->canDelete ) {
- wfSuppressWarnings();
- unlink( $this->path );
- wfRestoreWarnings();
+ $this->purge();
}
}
}