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
|
|
|
class FileSystem implements FileSystemInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function exists(string $path): bool |
32
|
|
|
{ |
33
|
|
|
return file_exists($path); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function read(string $filePath): string |
40
|
|
|
{ |
41
|
|
|
$content = file_get_contents($filePath); |
42
|
|
|
$content !== false ?: $this->throwEx(new FileSystemException()); |
43
|
|
|
|
44
|
|
|
return $content; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function write(string $filePath, string $contents) |
51
|
|
|
{ |
52
|
|
|
$bytesWritten = file_put_contents($filePath, $contents); |
53
|
|
|
$bytesWritten !== false ?: $this->throwEx(new FileSystemException()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function delete(string $filePath) |
60
|
|
|
{ |
61
|
|
|
$isDeleted = file_exists($filePath) === false || unlink($filePath) === true; |
62
|
|
|
$isDeleted === true ?: $this->throwEx(new FileSystemException()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function scanFolder(string $folderPath): array |
69
|
|
|
{ |
70
|
|
|
is_dir($folderPath) === true ?: $this->throwEx(new FileSystemException()); |
71
|
|
|
|
72
|
|
|
$iterator = call_user_func(function () use ($folderPath) { |
73
|
|
|
foreach (new DirectoryIterator($folderPath) as $directoryIterator) { |
74
|
|
|
/** @var DirectoryIterator $directoryIterator */ |
75
|
|
|
if ($directoryIterator->isDot() === false) { |
76
|
|
|
yield $directoryIterator->getFilename() => $directoryIterator->getRealPath(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
$result = iterator_to_array($iterator); |
82
|
|
|
|
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function isFolder(string $path): bool |
90
|
|
|
{ |
91
|
|
|
return is_dir($path); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
|
|
public function createFolder(string $folderPath) |
98
|
|
|
{ |
99
|
|
|
$isCreated = mkdir($folderPath); |
100
|
|
|
$isCreated === true ?: $this->throwEx(new FileSystemException()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
|
|
public function deleteFolder(string $folderPath) |
107
|
|
|
{ |
108
|
|
|
$isDeleted = is_dir($folderPath) === true && rmdir($folderPath) === true; |
109
|
|
|
$isDeleted === true ?: $this->throwEx(new FileSystemException()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
|
|
public function deleteFolderRecursive(string $folderPath) |
116
|
|
|
{ |
117
|
|
|
foreach ($this->scanFolder($folderPath) as $path) { |
118
|
|
|
$this->isFolder($path) === true ? $this->deleteFolderRecursive($path) : $this->delete($path); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->deleteFolder($folderPath); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritdoc |
126
|
|
|
*/ |
127
|
|
|
public function requireFile(string $path) |
128
|
|
|
{ |
129
|
|
|
/** @noinspection PhpIncludeInspection */ |
130
|
|
|
$result = require $path; |
131
|
|
|
|
132
|
|
|
return $result; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param FileSystemException $exception |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
protected function throwEx(FileSystemException $exception) |
141
|
|
|
{ |
142
|
|
|
throw $exception; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|