Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
17 / 18 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
TempFSFileFactory | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
newTempFSFile | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | namespace Wikimedia\FileBackend\FSFile; |
4 | |
5 | use Wikimedia\AtEase\AtEase; |
6 | |
7 | /** |
8 | * @ingroup FileBackend |
9 | */ |
10 | class TempFSFileFactory { |
11 | /** @var string|null */ |
12 | private $tmpDirectory; |
13 | |
14 | /** |
15 | * @param string|null $tmpDirectory A directory to put the temporary files in, e.g., |
16 | * $wgTmpDirectory. If null, we'll try to find one ourselves. |
17 | */ |
18 | public function __construct( $tmpDirectory = null ) { |
19 | $this->tmpDirectory = $tmpDirectory; |
20 | } |
21 | |
22 | /** |
23 | * Make a new temporary file on the file system. |
24 | * Temporary files may be purged when the file object falls out of scope. |
25 | * |
26 | * @param string $prefix |
27 | * @param string $extension Optional file extension |
28 | * @return TempFSFile|null |
29 | */ |
30 | public function newTempFSFile( $prefix, $extension = '' ) { |
31 | $ext = ( $extension != '' ) ? ".{$extension}" : ''; |
32 | $tmpDirectory = $this->tmpDirectory; |
33 | if ( !is_string( $tmpDirectory ) ) { |
34 | $tmpDirectory = TempFSFile::getUsableTempDirectory(); |
35 | } |
36 | |
37 | $attempts = 5; |
38 | while ( $attempts-- ) { |
39 | $hex = sprintf( '%06x%06x', mt_rand( 0, 0xffffff ), mt_rand( 0, 0xffffff ) ); |
40 | $path = "$tmpDirectory/$prefix$hex$ext"; |
41 | AtEase::suppressWarnings(); |
42 | $newFileHandle = fopen( $path, 'x' ); |
43 | AtEase::restoreWarnings(); |
44 | if ( $newFileHandle ) { |
45 | fclose( $newFileHandle ); |
46 | $tmpFile = new TempFSFile( $path ); |
47 | $tmpFile->autocollect(); |
48 | // Safely instantiated, end loop. |
49 | return $tmpFile; |
50 | } |
51 | } |
52 | |
53 | // Give up |
54 | return null; // @codeCoverageIgnore |
55 | } |
56 | } |
57 | |
58 | /** @deprecated class alias since 1.44 */ |
59 | class_alias( TempFSFileFactory::class, 'MediaWiki\FileBackend\FSFile\TempFSFileFactory' ); |