1
|
|
|
<?php namespace Limoncello\Application\FileSystem; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use DirectoryIterator; |
20
|
|
|
use Limoncello\Application\Exceptions\FileSystemException; |
21
|
|
|
use Limoncello\Contracts\FileSystem\FileSystemInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Limoncello\Application |
25
|
|
|
* |
26
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
27
|
|
|
*/ |
28
|
|
|
class FileSystem implements FileSystemInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function exists(string $path): bool |
34
|
|
|
{ |
35
|
|
|
return file_exists($path); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public function read(string $filePath): string |
42
|
|
|
{ |
43
|
|
|
$content = file_get_contents($filePath); |
44
|
|
|
$content !== false ?: $this->throwEx(new FileSystemException()); |
45
|
|
|
|
46
|
|
|
return $content; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function write(string $filePath, string $contents): void |
53
|
|
|
{ |
54
|
|
|
$bytesWritten = file_put_contents($filePath, $contents); |
55
|
|
|
$bytesWritten !== false ?: $this->throwEx(new FileSystemException()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function delete(string $filePath): void |
62
|
|
|
{ |
63
|
|
|
$isDeleted = file_exists($filePath) === false || unlink($filePath) === true; |
64
|
|
|
$isDeleted === true ?: $this->throwEx(new FileSystemException()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function scanFolder(string $folderPath): array |
71
|
|
|
{ |
72
|
|
|
is_dir($folderPath) === true ?: $this->throwEx(new FileSystemException()); |
73
|
|
|
|
74
|
|
|
$iterator = call_user_func(function () use ($folderPath) { |
75
|
|
|
foreach (new DirectoryIterator($folderPath) as $directoryIterator) { |
76
|
|
|
/** @var DirectoryIterator $directoryIterator */ |
77
|
|
|
if ($directoryIterator->isDot() === false) { |
78
|
|
|
yield $directoryIterator->getFilename() => $directoryIterator->getRealPath(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$result = iterator_to_array($iterator); |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function isFolder(string $path): bool |
92
|
|
|
{ |
93
|
|
|
return is_dir($path); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
|
|
public function createFolder(string $folderPath): void |
100
|
|
|
{ |
101
|
|
|
$isCreated = mkdir($folderPath); |
102
|
|
|
$isCreated === true ?: $this->throwEx(new FileSystemException()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
|
|
public function deleteFolder(string $folderPath): void |
109
|
|
|
{ |
110
|
|
|
$isDeleted = is_dir($folderPath) === true && rmdir($folderPath) === true; |
111
|
|
|
$isDeleted === true ?: $this->throwEx(new FileSystemException()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
117
|
|
|
public function deleteFolderRecursive(string $folderPath): void |
118
|
|
|
{ |
119
|
|
|
foreach ($this->scanFolder($folderPath) as $path) { |
120
|
|
|
$this->isFolder($path) === true ? $this->deleteFolderRecursive($path) : $this->delete($path); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->deleteFolder($folderPath); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritdoc |
128
|
|
|
*/ |
129
|
|
|
public function symlink(string $targetPath, string $linkPath): void |
130
|
|
|
{ |
131
|
|
|
$isCreated = symlink($targetPath, $linkPath) === true; |
132
|
|
|
$isCreated === true ?: $this->throwEx(new FileSystemException()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritdoc |
137
|
|
|
*/ |
138
|
|
|
public function requireFile(string $path) |
139
|
|
|
{ |
140
|
|
|
/** @noinspection PhpIncludeInspection */ |
141
|
|
|
$result = require $path; |
142
|
|
|
|
143
|
|
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param FileSystemException $exception |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
protected function throwEx(FileSystemException $exception) |
152
|
|
|
{ |
153
|
|
|
throw $exception; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|