/**
* Make a new temporary file on the file system.
* Temporary files may be purged when the file object falls out of scope.
- *
+ *
* @param $prefix string
* @param $extension string
- * @return TempFSFile|null
+ * @return TempFSFile|null
*/
public static function factory( $prefix, $extension = '' ) {
- $base = wfTempDir() . '/' . $prefix . dechex( mt_rand( 0, 99999999 ) );
+ wfProfileIn( __METHOD__ );
+ $base = wfTempDir() . '/' . $prefix . wfRandomString( 12 );
$ext = ( $extension != '' ) ? ".{$extension}" : "";
for ( $attempt = 1; true; $attempt++ ) {
$path = "{$base}-{$attempt}{$ext}";
fclose( $newFileHandle );
break; // got it
}
- if ( $attempt >= 15 ) {
+ if ( $attempt >= 5 ) {
+ wfProfileOut( __METHOD__ );
return null; // give up
}
}
$tmpFile = new self( $path );
$tmpFile->canDelete = true; // safely instantiated
+ wfProfileOut( __METHOD__ );
return $tmpFile;
}
/**
* Purge this file off the file system
- *
+ *
* @return bool Success
*/
public function purge() {
* Clean up the temporary file only after an object goes out of scope
*
* @param $object Object
- * @return void
+ * @return TempFSFile This object
*/
public function bind( $object ) {
if ( is_object( $object ) ) {
+ if ( !isset( $object->tempFSFileReferences ) ) {
+ // Init first since $object might use __get() and return only a copy variable
+ $object->tempFSFileReferences = array();
+ }
$object->tempFSFileReferences[] = $this;
}
+ return $this;
}
/**
* Set flag to not clean up after the temporary file
*
- * @return void
+ * @return TempFSFile This object
*/
public function preserve() {
$this->canDelete = false;
+ return $this;
+ }
+
+ /**
+ * Set flag clean up after the temporary file
+ *
+ * @return TempFSFile This object
+ */
+ public function autocollect() {
+ $this->canDelete = true;
+ return $this;
}
/**