-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathDirSetupUtil.php
67 lines (57 loc) · 1.73 KB
/
DirSetupUtil.php
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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Util\Filesystem;
use FilesystemIterator;
use RecursiveDirectoryIterator;
class DirSetupUtil
{
/**
* Array which will track any previously cleared directories, to prevent any unintended removal.
*
* @var array
*/
private static $DIR_CONTEXT = [];
/**
* Method used to clean export dir if needed and create new empty export dir.
*
* @param string $fullPath
* @return void
*/
public static function createGroupDir($fullPath)
{
//prevent redundant calls to these directories
$sanitizedPath = rtrim($fullPath, DIRECTORY_SEPARATOR);
// make sure we haven't already cleaned up this directory at any point before deletion
if (in_array($sanitizedPath, self::$DIR_CONTEXT)) {
return;
}
if (file_exists($sanitizedPath)) {
self::rmDirRecursive($sanitizedPath);
}
mkdir($sanitizedPath, 0777, true);
self::$DIR_CONTEXT[] = $sanitizedPath;
}
/**
* Takes a directory path and recursively deletes all files and folders.
*
* @param string $directory
* @return void
*/
public static function rmdirRecursive($directory)
{
$it = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS);
while ($it->valid()) {
$path = $directory . DIRECTORY_SEPARATOR . $it->getFilename();
if ($it->isDir()) {
self::rmDirRecursive($path);
} else {
unlink($path);
}
$it->next();
}
rmdir($directory);
}
}