|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Paraunit\File; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class TempDirectory |
|
7
|
|
|
* @package Paraunit\File |
|
8
|
|
|
*/ |
|
9
|
|
|
class TempDirectory |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var string[] */ |
|
12
|
|
|
private static $tempDirs = array( |
|
13
|
|
|
'/dev/shm', |
|
14
|
|
|
); |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private static $timestamp; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Paraunit constructor. |
|
21
|
|
|
*/ |
|
22
|
57 |
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
57 |
|
static::$timestamp = uniqid(date('Ymd-His'), true); |
|
|
|
|
|
|
25
|
57 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
49 |
|
public function getTempDirForThisExecution() |
|
31
|
|
|
{ |
|
32
|
49 |
|
$dir = self::getTempBaseDir() . DIRECTORY_SEPARATOR . static::$timestamp; |
|
|
|
|
|
|
33
|
49 |
|
self::mkdirIfNotExists($dir); |
|
34
|
49 |
|
self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'config'); |
|
35
|
49 |
|
self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'logs'); |
|
36
|
49 |
|
self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'coverage'); |
|
37
|
|
|
|
|
38
|
49 |
|
return $dir; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return string |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \RuntimeException |
|
45
|
|
|
*/ |
|
46
|
49 |
|
public static function getTempBaseDir() |
|
47
|
|
|
{ |
|
48
|
49 |
|
$dirs = self::$tempDirs; |
|
49
|
|
|
// Fallback to sys temp dir |
|
50
|
49 |
|
$dirs[] = sys_get_temp_dir(); |
|
51
|
|
|
|
|
52
|
49 |
|
foreach ($dirs as $directory) { |
|
53
|
49 |
|
if (file_exists($directory)) { |
|
54
|
49 |
|
$baseDir = $directory . DIRECTORY_SEPARATOR . 'paraunit'; |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
49 |
|
self::mkdirIfNotExists($baseDir); |
|
58
|
|
|
|
|
59
|
49 |
|
return $baseDir; |
|
60
|
|
|
} catch (\RuntimeException $e) { |
|
61
|
|
|
// ignore and try next dir |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
throw new \RuntimeException('Unable to create a temporary directory'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $path |
|
71
|
|
|
* |
|
72
|
|
|
* @throws \RuntimeException |
|
73
|
|
|
*/ |
|
74
|
49 |
|
private static function mkdirIfNotExists($path) |
|
75
|
|
|
{ |
|
76
|
49 |
|
if (file_exists($path)) { |
|
77
|
49 |
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
49 |
View Code Duplication |
if (!mkdir($path) && !is_dir($path)) { |
|
|
|
|
|
|
81
|
|
|
throw new \RuntimeException( |
|
82
|
|
|
sprintf('Unable to create temporary directory \'%s\'.', $path) |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
49 |
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: