-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempDirExtension.php
More file actions
140 lines (124 loc) · 4.2 KB
/
TempDirExtension.php
File metadata and controls
140 lines (124 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace Oro\Component\Testing;
use Symfony\Component\Filesystem\Filesystem;
/**
* This trait can be used in any types of tests, e.g. unit or functional tests,
* in case they need to work with a temporary directories or files.
*/
trait TempDirExtension
{
/** @var string[] */
private array $tempDirs = [];
/**
* @var string Suffix that is added to the temp dir name to ensure uniqueness to isolate current runtime
* from others.
*/
private static string $uniqueSuffix = '';
/**
* Removes all temporary directories requested via getTempDir() method.
*
* @after
*/
protected function removeTempDirs()
{
if (empty($this->tempDirs)) {
return;
}
try {
$fs = new Filesystem();
foreach ($this->tempDirs as $dir) {
if ($fs->exists($dir)) {
$fs->remove($dir);
}
}
} finally {
$this->tempDirs = [];
}
}
/**
* Gets a temporary directory.
*
* @param string $subDir
* @param bool|null $existence TRUE to make sure that the directory exists and empty
* FALSE to make sure that the directory does not exist
* NULL to not check the directory existence
*
* @return string The full path of the temporary directory
*/
protected function getTempDir(string $subDir, ?bool $existence = true): string
{
$tmpDir = static::getTempDirName($subDir);
if (!in_array($tmpDir, $this->tempDirs, true)) {
$this->tempDirs[] = $tmpDir;
}
if (null !== $existence) {
$fs = new Filesystem();
if ($fs->exists($tmpDir)) {
$fs->remove($tmpDir);
}
if ($existence) {
$fs->mkdir($tmpDir);
}
}
return $tmpDir;
}
/**
* @param string $subDir
*
* @return string Absolute path to the temp dir with a suffix unique per runtime.
*/
protected static function getTempDirName(string $subDir): string
{
$subDir = preg_replace('/^[\/\\\\]+(.*)/', '$1', $subDir);
if (!str_starts_with(strtolower($subDir), 'oro')) {
$subDir = 'oro_' . $subDir;
}
if (!self::$uniqueSuffix) {
// Generates unique suffix and sets it to a static property so it does not change during current runtime.
self::$uniqueSuffix = str_replace('.', '', uniqid('', true));
}
return rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR
. $subDir . '_' . self::$uniqueSuffix;
}
/**
* Generates a file path with a unique file name in a temporary directory and returns it.
* The file itself is not actually created. The temporary directory will be created though if it does not exist yet.
*
* @return string The full path of a file in the temporary directory
*/
protected function getTempFile(string $subDir, string $prefix = 'tmp', string $suffix = ''): string
{
return
$this->getTempDir($subDir)
. DIRECTORY_SEPARATOR
. str_replace('.', '', uniqid($prefix, true))
. $suffix;
}
/**
* Copies the content from $sourceDir directory to a temporary directory.
*
* @param string $subDir
* @param string $sourceDir
*
* @return string The full path of the temporary directory
*/
protected function copyToTempDir(string $subDir, string $sourceDir): string
{
$tmpDir = $this->getTempDir($subDir, false);
$fs = new Filesystem();
$fs->mkdir($tmpDir);
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($sourceDir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
$target = $tmpDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName();
if ($item->isDir()) {
$fs->mkdir($target);
} else {
$fs->copy($item, $target);
}
}
return $tmpDir;
}
}