|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Helpers; |
|
8
|
|
|
|
|
9
|
|
|
use League\Flysystem\FilesystemOperator; |
|
10
|
|
|
|
|
11
|
|
|
final class FileHelper |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct( |
|
14
|
|
|
private readonly FilesystemOperator $resourceFilesystem |
|
15
|
|
|
) {} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Writes contents to a file. |
|
19
|
|
|
* |
|
20
|
|
|
* Equivalent to file_put_contents(). |
|
21
|
|
|
*/ |
|
22
|
|
|
public function write(string $path, string $contents): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->resourceFilesystem->write($path, $contents); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Reads the contents of a file as a string. |
|
29
|
|
|
* |
|
30
|
|
|
* Equivalent to file_get_contents(). |
|
31
|
|
|
*/ |
|
32
|
|
|
public function read(string $path): ?string |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->resourceFilesystem->read($path); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Deletes a file. |
|
39
|
|
|
* |
|
40
|
|
|
* Equivalent to unlink(). |
|
41
|
|
|
*/ |
|
42
|
|
|
public function delete(string $path): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->resourceFilesystem->delete($path); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Checks if a file exists. |
|
49
|
|
|
* |
|
50
|
|
|
* Equivalent to file_exists(). |
|
51
|
|
|
*/ |
|
52
|
|
|
public function exists(string $path): bool |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->resourceFilesystem->fileExists($path); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Opens a file for reading as a stream resource. |
|
59
|
|
|
* |
|
60
|
|
|
* Equivalent to fopen() in read mode. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function readStream(string $path) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->resourceFilesystem->readStream($path); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Writes a stream resource to a file. |
|
69
|
|
|
* |
|
70
|
|
|
* Equivalent to fwrite() when working with streams. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function writeStream(string $path, $stream): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->resourceFilesystem->writeStream($path, $stream); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Moves an uploaded file into Flysystem storage. |
|
79
|
|
|
* |
|
80
|
|
|
* Equivalent to UploadedFile->move(). |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $tempPath Local temporary path of the uploaded file. |
|
83
|
|
|
* @param string $targetPath Target path in the filesystem. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function moveUploadedFile(string $tempPath, string $targetPath): void |
|
86
|
|
|
{ |
|
87
|
|
|
$stream = fopen($tempPath, 'r'); |
|
88
|
|
|
$this->writeStream($targetPath, $stream); |
|
89
|
|
|
fclose($stream); |
|
90
|
|
|
|
|
91
|
|
|
unlink($tempPath); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Outputs the contents of a file directly to the browser. |
|
96
|
|
|
* |
|
97
|
|
|
* Equivalent to readfile(). |
|
98
|
|
|
*/ |
|
99
|
|
|
public function streamToOutput(string $path): void |
|
100
|
|
|
{ |
|
101
|
|
|
$stream = $this->readStream($path); |
|
102
|
|
|
fpassthru($stream); |
|
103
|
|
|
fclose($stream); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Creates a directory. |
|
108
|
|
|
* |
|
109
|
|
|
* Equivalent to mkdir(). |
|
110
|
|
|
*/ |
|
111
|
|
|
public function createDirectory(string $path): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->resourceFilesystem->createDirectory($path); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Lists files and directories under a given path as a flat array. |
|
118
|
|
|
* |
|
119
|
|
|
* Equivalent to scandir(), opendir(), readdir(). |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $path Path to list (empty string = root). |
|
122
|
|
|
* @param bool $deep true = recursive listing, false = shallow listing. |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public function listContents(string $path = '', bool $deep = false): array |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->resourceFilesystem |
|
129
|
|
|
->listContents($path, $deep) |
|
130
|
|
|
->toArray(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Gets the size of a file in bytes. |
|
135
|
|
|
* |
|
136
|
|
|
* Equivalent to filesize(). |
|
137
|
|
|
*/ |
|
138
|
|
|
public function fileSize(string $path): int |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->resourceFilesystem->fileSize($path); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Gets the last modified timestamp of a file. |
|
145
|
|
|
* |
|
146
|
|
|
* Equivalent to filemtime(). |
|
147
|
|
|
*/ |
|
148
|
|
|
public function lastModified(string $path): int |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->resourceFilesystem->lastModified($path); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Deletes an entire directory and its contents. |
|
155
|
|
|
* |
|
156
|
|
|
* Equivalent to rmdir() or a recursive delete. |
|
157
|
|
|
*/ |
|
158
|
|
|
public function deleteDirectory(string $path): void |
|
159
|
|
|
{ |
|
160
|
|
|
$this->resourceFilesystem->deleteDirectory($path); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Copies a file to a new location. |
|
165
|
|
|
* |
|
166
|
|
|
* Equivalent to copy(). |
|
167
|
|
|
*/ |
|
168
|
|
|
public function copy(string $source, string $destination): void |
|
169
|
|
|
{ |
|
170
|
|
|
$this->resourceFilesystem->copy($source, $destination); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Moves or renames a file. |
|
175
|
|
|
* |
|
176
|
|
|
* Equivalent to rename() or a file move. |
|
177
|
|
|
*/ |
|
178
|
|
|
public function move(string $source, string $destination): void |
|
179
|
|
|
{ |
|
180
|
|
|
$this->resourceFilesystem->move($source, $destination); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|