1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file contains an extended Filesystem class. |
5
|
|
|
* |
6
|
|
|
* This file is part of the Stakx project. |
7
|
|
|
* |
8
|
|
|
* @copyright 2016 Vladimir Jimenez |
9
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace allejo\stakx\System; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
15
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
16
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Filesystem |
20
|
|
|
* |
21
|
|
|
* This class extends Symfony's Filesystem to provide convenience functions |
22
|
|
|
* |
23
|
|
|
* @package allejo\stakx\Environment |
24
|
|
|
*/ |
25
|
|
|
class Filesystem extends \Symfony\Component\Filesystem\Filesystem |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Build an absolute file or directory path separated by the OS specific directory separator |
29
|
|
|
* |
30
|
|
|
* @param string ...$pathFragments |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
42 |
|
public function absolutePath ($pathFragments) |
35
|
|
|
{ |
36
|
42 |
|
if ($this->isAbsolutePath($pathFragments)) |
37
|
42 |
|
{ |
38
|
15 |
|
return $pathFragments; |
39
|
|
|
} |
40
|
|
|
|
41
|
36 |
|
$args = func_get_args(); |
42
|
36 |
|
array_unshift($args, getcwd()); |
43
|
|
|
|
44
|
36 |
|
return implode(DIRECTORY_SEPARATOR, $args); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Build a file or directory path separated by the OS specific directory separator |
49
|
|
|
* |
50
|
|
|
* @param string ...$pathFragments |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
15 |
|
public function appendPath ($pathFragments) |
|
|
|
|
55
|
|
|
{ |
56
|
15 |
|
return implode(DIRECTORY_SEPARATOR, func_get_args()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Copy a file or folder recursively |
61
|
|
|
* |
62
|
|
|
* @param string $originFile The original filename |
63
|
|
|
* @param string $targetFile The target filename |
64
|
|
|
* @param bool $overwriteNewerFiles If true, target files newer than origin files are overwritten |
65
|
|
|
* |
66
|
|
|
* @throws FileNotFoundException When originFile doesn't exist |
67
|
|
|
* @throws IOException When copy fails |
68
|
|
|
*/ |
69
|
|
|
public function copy($originFile, $targetFile, $overwriteNewerFiles = false) |
70
|
|
|
{ |
71
|
|
|
if ($this->isDir($originFile)) |
72
|
|
|
{ |
73
|
|
|
if (!$this->isDir($targetFile)) |
74
|
|
|
{ |
75
|
|
|
mkdir($targetFile, 0755, true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$dir = dir($originFile); |
79
|
|
|
|
80
|
|
|
while (false !== $entry = $dir->read()) |
81
|
|
|
{ |
82
|
|
|
// Skip pointers |
83
|
|
|
if ($entry == '.' || $entry == '..') { continue; } |
84
|
|
|
|
85
|
|
|
$this->copy("$originFile/$entry", "$targetFile/$entry", true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$dir->close(); |
89
|
|
|
} |
90
|
|
|
else |
91
|
|
|
{ |
92
|
|
|
parent::copy($originFile, $targetFile, $overwriteNewerFiles); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create an instance of Symfony's SplFileInfo with relative path information |
98
|
|
|
* |
99
|
|
|
* @param string $filePath |
100
|
|
|
* |
101
|
|
|
* @return SplFileInfo |
102
|
|
|
*/ |
103
|
|
|
public function createSplFileInfo ($filePath) |
104
|
|
|
{ |
105
|
|
|
return (new SplFileInfo( |
106
|
|
|
$this->absolutePath($filePath), |
107
|
|
|
$this->getRelativePath($this->getFolderPath($filePath)), |
108
|
|
|
$this->getRelativePath($filePath) |
109
|
|
|
)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Strip the current working directory from an absolute path |
114
|
|
|
* |
115
|
|
|
* @param string $path An absolute path |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
53 |
|
public function getRelativePath ($path) |
120
|
|
|
{ |
121
|
53 |
|
return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the name of a given file without the extension |
126
|
|
|
* |
127
|
|
|
* @param string $filePath A file path |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
43 |
|
public function getBaseName ($filePath) |
132
|
|
|
{ |
133
|
43 |
|
return pathinfo($filePath, PATHINFO_FILENAME); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the name of a given file |
138
|
|
|
* |
139
|
|
|
* @param string $filePath A file path |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getFileName ($filePath) |
144
|
|
|
{ |
145
|
|
|
return pathinfo($filePath, PATHINFO_BASENAME); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the parent directory of a given file |
150
|
|
|
* |
151
|
|
|
* @param string $filePath A file path |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
4 |
|
public function getFolderPath ($filePath) |
156
|
|
|
{ |
157
|
4 |
|
return pathinfo($filePath, PATHINFO_DIRNAME); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the extension of a given file |
162
|
|
|
* |
163
|
|
|
* @param string $filename A file path |
164
|
|
|
* |
165
|
|
|
* @return string The extension of the file |
166
|
|
|
*/ |
167
|
105 |
|
public function getExtension ($filename) |
168
|
|
|
{ |
169
|
105 |
|
return pathinfo($filename, PATHINFO_EXTENSION); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Check whether or not if a given path is a directory |
174
|
|
|
* |
175
|
|
|
* @param string $folderPath |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
5 |
|
public function isDir ($folderPath) |
180
|
|
|
{ |
181
|
5 |
|
return is_dir($folderPath); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the full path to the file without the extension |
186
|
|
|
* |
187
|
|
|
* @param string $filename A file path |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
4 |
|
public function removeExtension ($filename) |
192
|
|
|
{ |
193
|
4 |
|
return $this->appendPath( |
194
|
4 |
|
$this->getFolderPath($filename), |
195
|
4 |
|
$this->getBaseName($filename) |
196
|
4 |
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Write a file |
201
|
|
|
* |
202
|
|
|
* @param string $targetDir The directory of where the file will be created; the file name is a separate variable |
203
|
|
|
* @param string $fileName The name of the file |
204
|
|
|
* @param string $content The content that belongs in the file |
205
|
|
|
* |
206
|
|
|
* @return SplFileInfo A reference to the newly created file |
207
|
|
|
*/ |
208
|
|
View Code Duplication |
public function writeFile ($targetDir, $fileName, $content) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$outputFolder = $this->getFolderPath($this->absolutePath($targetDir, $fileName)); |
211
|
|
|
$targetFile = $this->getFileName($fileName); |
212
|
|
|
|
213
|
|
|
if (!file_exists($outputFolder)) |
214
|
|
|
{ |
215
|
|
|
mkdir($outputFolder, 0755, true); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
file_put_contents($this->appendPath($outputFolder, $targetFile), $content, LOCK_EX); |
219
|
|
|
|
220
|
|
|
return (new SplFileInfo( |
221
|
|
|
$fileName, |
222
|
|
|
$this->absolutePath($targetDir), |
223
|
|
|
$this->absolutePath($targetDir, $fileName)) |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.