|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiGen\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\Utils\FileSystem as NetteFileSystem; |
|
6
|
|
|
use Nette\Utils\Finder; |
|
7
|
|
|
use RecursiveDirectoryIterator; |
|
8
|
|
|
use SplFileInfo; |
|
9
|
|
|
|
|
10
|
|
|
final class FileSystem |
|
11
|
|
|
{ |
|
12
|
|
|
public function normalizePath(string $path): string |
|
13
|
|
|
{ |
|
14
|
|
|
return str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function forceDir(string $path): string |
|
18
|
|
|
{ |
|
19
|
|
|
@mkdir(dirname($path), 0755, true); |
|
|
|
|
|
|
20
|
|
|
return $path; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function deleteDir(string $path): void |
|
24
|
|
|
{ |
|
25
|
|
|
NetteFileSystem::delete($path); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function purgeDir(string $path): void |
|
29
|
|
|
{ |
|
30
|
|
|
NetteFileSystem::delete($path); |
|
31
|
|
|
NetteFileSystem::createDir($path); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $path |
|
36
|
|
|
* @param string[] $baseDirectories |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getAbsolutePath(string $path, array $baseDirectories = []): string |
|
40
|
|
|
{ |
|
41
|
|
|
foreach ($baseDirectories as $directory) { |
|
42
|
|
|
$fileName = $directory . '/' . $path; |
|
43
|
|
|
if (is_file($fileName)) { |
|
44
|
|
|
return $this->normalizePath(realpath($fileName)); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (file_exists($path)) { |
|
49
|
|
|
$path = realpath($path); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (file_exists(getcwd() . $path)) { |
|
53
|
|
|
$path = getcwd() . $path; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->normalizePath($path); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function isDirEmpty(string $path): bool |
|
60
|
|
|
{ |
|
61
|
|
|
if (count(glob($path . '/*'))) { |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string[]|string[][] $source |
|
70
|
|
|
* @param string $destination |
|
71
|
|
|
*/ |
|
72
|
|
|
public function copy(array $source, string $destination): void |
|
73
|
|
|
{ |
|
74
|
|
|
foreach ($source as $resourceSource => $resourceDestination) { |
|
75
|
|
|
if (is_file($resourceSource)) { |
|
76
|
|
|
copy($resourceSource, FileSystem::forceDir($destination . '/' . $resourceDestination)); |
|
77
|
|
|
continue; |
|
78
|
|
|
} else { |
|
79
|
|
|
/** @var RecursiveDirectoryIterator $iterator */ |
|
80
|
|
|
$iterator = Finder::findFiles('*')->from($resourceSource)->getIterator(); |
|
81
|
|
|
foreach ($iterator as $item) { |
|
82
|
|
|
/** @var SplFileInfo $item */ |
|
83
|
|
|
copy($item->getPathname(), FileSystem::forceDir($destination |
|
84
|
|
|
. '/' . $resourceDestination |
|
85
|
|
|
. '/' . $iterator->getSubPathname())); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: