-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileSystem.php
More file actions
122 lines (107 loc) · 2.95 KB
/
FileSystem.php
File metadata and controls
122 lines (107 loc) · 2.95 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
<?php
namespace VersionPress\Utils;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Traversable;
/**
* Helper functions to work with filesystem. Currently, the functions use either bare implementation
* or {@link http://symfony.com/doc/master/components/filesystem/introduction.html Symfony Filesystem}.
*/
class FileSystem
{
/**
* Renames (moves) origin to target.
*
* @see SymfonyFilesystem::rename()
*
* @param string $origin
* @param string $target
* @param bool $overwrite
*/
public static function rename($origin, $target, $overwrite = false)
{
$fs = new SymfonyFilesystem();
$fs->rename($origin, $target, $overwrite);
}
/**
* Removes a file / directory. Works recursively.
*
* @see SymfonyFilesystem::remove()
*
* @param string|Traversable $path Path to a file or directory.
*/
public static function remove($path)
{
$fs = new SymfonyFilesystem();
$fs->remove($path);
}
/**
* Removes the content of a directory (not the directory itself). Works recursively.
*
* @param string $path Path to a directory.
*/
public static function removeContent($path)
{
if (!is_dir($path)) {
return;
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
$fs = new SymfonyFilesystem();
$fs->remove($iterator);
}
/**
* Copies a file. Uses Symfony's copy but actually honors the third parameter.
*
* @param string $origin
* @param string $target
* @param bool $override
*/
public static function copy($origin, $target, $override = false)
{
$fs = new SymfonyFilesystem();
if (!$override && $fs->exists($target)) {
return;
}
$fs->copy($origin, $target, $override);
}
/**
* Copies a directory. Uses Symfony's mirror() under the cover.
*
* @see SymfonyFilesystem::mirror()
*
* @param string $origin
* @param string $target
*/
public static function copyDir($origin, $target)
{
$fs = new SymfonyFilesystem();
$fs->mirror($origin, $target);
}
/**
* Creates a directory with usual permissions.
*
* @param string $dir
* @param int $mode
*/
public static function mkdir($dir, $mode = 0750)
{
$fs = new SymfonyFilesystem();
$fs->mkdir($dir, $mode);
}
/**
* Compares two files and returns true if their contents is equal.
*
* @param $file1
* @param $file2
* @return bool
*/
public static function filesHaveSameContents($file1, $file2)
{
return sha1_file($file1) === sha1_file($file2);
}
}