DiskDriver::makeDir()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Class DiskDriver
4
 *
5
 * @filesource   DiskDriver.php
6
 * @created      02.03.2017
7
 * @package      chillerlan\Filereader\Drivers
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Filereader\Drivers;
14
15
use chillerlan\Filereader\FilereaderException;
16
use stdClass;
17
18
/**
19
 *
20
 */
21
class DiskDriver implements FSDriverInterface{
22
23
	/**
24
	 * Checks if the given file exists.
25
	 *
26
	 * @param string $path the file path
27
	 *
28
	 * @return bool
29
	 */
30
	public function fileExists(string $path):bool{
31
		return file_exists($path);
32
	}
33
34
	/**
35
	 * Checks if the given $path is a file.
36
	 *
37
	 * @param string $path the file path
38
	 *
39
	 * @return bool
40
	 */
41
	public function isFile(string $path):bool{
42
		return is_file($path);
43
	}
44
45
	/**
46
	 * Checks if the given $path is a directory.
47
	 *
48
	 * @param string $path the directory path
49
	 *
50
	 * @return bool
51
	 */
52
	public function isDir(string $path):bool{
53
		return is_dir($path);
54
	}
55
56
	/**
57
	 * Returns the given file's contents
58
	 *
59
	 * @param string $path the file path
60
	 *
61
	 * @return string the file's contents
62
	 * @throws \chillerlan\Filereader\FilereaderException
63
	 */
64
	public function fileContents(string $path):string{
65
66
		if(!$this->isFile($path)){
67
			throw new FilereaderException('File not found: '.$path);
68
		}
69
70
		return file_get_contents($path);
71
	}
72
73
	/**
74
	 * Requires the given source file and returns it's executed contents.
75
	 * Caution! This is a potential security leak. You could even eval().
76
	 *
77
	 * @param string $path the file path
78
	 *
79
	 * @return mixed the file's executed contents
80
	 * @throws \chillerlan\Filereader\FilereaderException
81
	 */
82
	public function getRequire(string $path){
83
84
		if(!$this->isFile($path)){
85
			throw new FilereaderException('File not found: '.$path);
86
		}
87
88
		return require $path;
89
	}
90
91
	/**
92
	 * Creates a new directory under $path.
93
	 *
94
	 * @param string $path
95
	 *
96
	 * @return bool
97
	 * @throws \chillerlan\Filereader\FilereaderException
98
	 */
99
	public function makeDir(string $path):bool{
100
101
		if($this->isDir($path)){
102
			throw new FilereaderException('Directory already exists: '.$path);
103
		}
104
105
		return mkdir($path);
106
	}
107
108
	/**
109
	 * Deletes directory $path (if empty)
110
	 *
111
	 * @param string $path
112
	 *
113
	 * @return bool
114
	 * @throws \chillerlan\Filereader\FilereaderException
115
	 */
116
	public function deleteDir(string $path):bool{
117
118
		if(!$this->isDir($path)){
119
			throw new FilereaderException('Directory not found: '.$path);
120
		}
121
122
		return rmdir($path);
123
	}
124
125
	/**
126
	 * Deletes a file
127
	 *
128
	 * @param string $path
129
	 *
130
	 * @return bool
131
	 * @throws \chillerlan\Filereader\FilereaderException
132
	 */
133
	public function deleteFile(string $path):bool{
134
135
		if(!$this->isFile($path)){
136
			throw new FilereaderException('File not found: '.$path);
137
		}
138
139
		return unlink($path);
140
	}
141
142
	/**
143
	 * Renames a file or directory from $oldname to $newname
144
	 *
145
	 * @param string $oldname
146
	 * @param string $newname
147
	 * @param bool   $overwrite
148
	 *
149
	 * @return bool
150
	 * @throws \chillerlan\Filereader\FilereaderException
151
	 */
152
	public function rename(string $oldname, string $newname, bool $overwrite = true):bool{
153
154
		if(!$overwrite && ($this->fileExists($newname) || $this->isDir($newname))){
155
			throw new FilereaderException('Destination already exists.');
156
		}
157
158
		return rename($oldname, $newname);
159
	}
160
161
	/**
162
	 * Copies a file from $source to $dest
163
	 *
164
	 * @param string $source
165
	 * @param string $destination
166
	 * @param bool   $overwrite
167
	 *
168
	 * @return bool
169
	 * @throws \chillerlan\Filereader\FilereaderException
170
	 */
171
	public function copyFile(string $source, string $destination, bool $overwrite = true):bool{
172
173
		if(!$overwrite && $this->fileExists($destination)){
174
			throw new FilereaderException('Destination file already exists.');
175
		}
176
177
		return copy($source, $destination);
178
	}
179
180
	/**
181
	 * @param string $path
182
	 * @param string $data
183
	 * @param bool   $overwrite
184
	 *
185
	 * @return int|bool
186
	 * @throws \chillerlan\Filereader\FilereaderException
187
	 */
188
	public function write(string $path, string $data, bool $overwrite = true){
189
190
		if(!$overwrite && $this->fileExists($path)){
191
			throw new FilereaderException('Destination file already exists.');
192
		}
193
194
		return file_put_contents($path, $data);
195
	}
196
197
	/**
198
	 * @param string $path
199
	 *
200
	 * @return int|null
201
	 */
202
	public function fileModifyTime(string $path):?int{
203
		return filemtime($path);
204
	}
205
206
	/**
207
	 * @param string $path
208
	 *
209
	 * @return bool
210
	 * @throws \chillerlan\Filereader\FilereaderException
211
	 */
212
	public function isWritable(string $path):bool{
213
214
		if(!$this->isDir($path)){
215
			throw new FilereaderException('Directory not found: '.$path);
216
		}
217
218
		return is_writable($path);
219
	}
220
221
	/**
222
	 * @param string $pattern
223
	 *
224
	 * @return array
225
	 */
226
	public function findFiles(string $pattern):array {
227
		return glob($pattern);
228
	}
229
230
}
231