Completed
Push — develop ( 478a5a...f1f328 )
by Michael
03:55
created
src/File.php 1 patch
Indentation   +419 added lines, -419 removed lines patch added patch discarded remove patch
@@ -17,423 +17,423 @@
 block discarded – undo
17 17
  */
18 18
 class File implements \Countable
19 19
 {
20
-	const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR;
21
-
22
-	/** @var string the file path. */
23
-	private $path;
24
-
25
-	/**
26
-	 * Constructs a File object with the given path.
27
-	 *
28
-	 * @param string $path
29
-	 */
30
-	public function __construct($path)
31
-	{
32
-		if (substr($path, -1) === static::DIRECTORY_SEPARATOR) {
33
-			$this->path = substr($path, 0, -1);
34
-		} else {
35
-			$this->path = $path;
36
-		}
37
-	}
38
-
39
-	/**
40
-	 * Returns the string representation of the File object.
41
-	 *
42
-	 * @return string the string representation of the File object.
43
-	 */
44
-	public function __toString()
45
-	{
46
-		return $this->getPath();
47
-	}
48
-
49
-	/**
50
-	 * Returns the path of the file.
51
-	 *
52
-	 * @return string the path of the file.
53
-	 */
54
-	public function getPath()
55
-	{
56
-		return $this->path;
57
-	}
58
-
59
-	/**
60
-	 * Returns the parent directory of the file.
61
-	 *
62
-	 * @return string the parent directory of the file.
63
-	 */
64
-	public function getDirectory()
65
-	{
66
-		return dirname($this->path);
67
-	}
68
-
69
-	/**
70
-	 * Returns the name of the file.
71
-	 *
72
-	 * @return string the name of the file.
73
-	 */
74
-	public function getName()
75
-	{
76
-		return basename($this->path);
77
-	}
78
-
79
-	/**
80
-	 * Returns true if the file exists.
81
-	 *
82
-	 * @return bool true if the file exists.
83
-	 */
84
-	public function exists()
85
-	{
86
-		return file_exists($this->path);
87
-	}
88
-
89
-	/**
90
-	 * Returns true if you can execute the file.
91
-	 *
92
-	 * @return bool true if you can execute the file.
93
-	 */
94
-	public function canExecute()
95
-	{
96
-		return is_executable($this->path);
97
-	}
98
-
99
-	/**
100
-	 * Returns true if you can read the file.
101
-	 *
102
-	 * @return bool true if you can read the file.
103
-	 */
104
-	public function canRead()
105
-	{
106
-		return is_readable($this->path);
107
-	}
108
-
109
-	/**
110
-	 * Returns true if you can write the file.
111
-	 *
112
-	 * @return bool true if you can write the file.
113
-	 */
114
-	public function canWrite()
115
-	{
116
-		return is_writeable($this->path);
117
-	}
118
-
119
-	/**
120
-	 * Returns true if the file is a file.
121
-	 *
122
-	 * @return bool true if the file is a file.
123
-	 */
124
-	public function isFile()
125
-	{
126
-		return is_file($this->path);
127
-	}
128
-
129
-	/**
130
-	 * Returns true if the file is a directory.
131
-	 *
132
-	 * @return bool true if the file is a directory.
133
-	 */
134
-	public function isDirectory()
135
-	{
136
-		return is_dir($this->path);
137
-	}
138
-
139
-	/**
140
-	 * Returns the numer of bytes in the file, or -1 on failure.
141
-	 *
142
-	 * @return int the number of bytes in the file, or -1 on failure.
143
-	 */
144
-	public function count()
145
-	{
146
-		// Check if the file exists
147
-		if (!$this->exists()) {
148
-			return -1;
149
-		}
150
-
151
-		// Return length
152
-		return ($result = filesize($this->path)) !== false ? $result : -1;
153
-	}
154
-
155
-	/**
156
-	 * Returns the time of the last modification as a unixtimestap, or -1 on failure.
157
-	 *
158
-	 * @return int the time of the last modification as a unixtimestap, or -1 on failure.
159
-	 */
160
-	public function lastModified()
161
-	{
162
-		// Check if the file exists
163
-		if (!$this->exists()) {
164
-			return -1;
165
-		}
166
-
167
-		// Return last modified timestamp
168
-		return ($result = filemtime($this->path)) !== false ? $result : -1;
169
-	}
170
-
171
-	/**
172
-	 * Returns an iterator with the files and directories in the current directory.
173
-	 *
174
-	 * @param bool $recursive = false
175
-	 * @param bool $showHidden = false
176
-	 * @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory.
177
-	 */
178
-	private function listAllIterator($recursive = false, $showHidden = false)
179
-	{
180
-		// Check file
181
-		if (!$this->isDirectory()) {
182
-			return new \ArrayIterator([]);
183
-		}
184
-
185
-		// Check flags
186
-		$flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO;
187
-
188
-		if (!$showHidden) {
189
-			$flags = $flags | \FilesystemIterator::SKIP_DOTS;
190
-		}
191
-
192
-		// Check recursive
193
-		if ($recursive) {
194
-			return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, $flags), \RecursiveIteratorIterator::SELF_FIRST);
195
-		}
196
-
197
-		return new \FilesystemIterator($this->path, $flags);
198
-	}
199
-
200
-	/**
201
-	 * Returns an array with the files and directories in the current directory.
202
-	 *
203
-	 * @param bool $recursive = false
204
-	 * @param bool $showHidden = false
205
-	 * @return string[] an array with the files and directories in the current directory.
206
-	 */
207
-	public function listAll($recursive = false, $showHidden = false)
208
-	{
209
-		// Init
210
-		$result = [];
211
-
212
-		// List all
213
-		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
214
-			$result[] = $element->getFilename();
215
-		}
216
-
217
-		// Return all
218
-		return $result;
219
-	}
220
-
221
-	/**
222
-	 * Returns an array with the directories in the current directory.
223
-	 *
224
-	 * @param bool $recursive = false
225
-	 * @param bool $showHidden = false
226
-	 * @return string[] an array with the directories in the current directory.
227
-	 */
228
-	public function listDirectories($recursive = false, $showHidden = false)
229
-	{
230
-		// Init
231
-		$result = [];
232
-
233
-		// List directories
234
-		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
235
-			if ($element->isDir()) {
236
-				$result[] = $element->getFilename();
237
-			}
238
-		}
239
-
240
-		// Return directories
241
-		return $result;
242
-	}
243
-
244
-	/**
245
-	 * Returns an array with the files in the current directory.
246
-	 *
247
-	 * @param bool $recursive = false
248
-	 * @param bool $showHidden = false
249
-	 * @return string[] an array with the files in the current directory.
250
-	 */
251
-	public function listFiles($recursive = false, $showHidden = false)
252
-	{
253
-		// Init
254
-		$result = [];
255
-
256
-		// List files
257
-		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
258
-			if ($element->isFile()) {
259
-				$result[] = $element->getFilename();
260
-			}
261
-		}
262
-
263
-		// Return files
264
-		return $result;
265
-	}
266
-
267
-	/**
268
-	 * Returns true if the file has been created.
269
-	 *
270
-	 * @param bool $override = false
271
-	 * @return bool true if the file has been created.
272
-	 */
273
-	public function makeFile($override = false)
274
-	{
275
-		// Check if the file exists
276
-		if ($this->exists() && !$override) {
277
-			return false;
278
-		}
279
-
280
-		return file_put_contents($this->path, '') !== false;
281
-	}
282
-
283
-	/**
284
-	 * Returns true if the directory has been created.
285
-	 *
286
-	 * @param bool $recursive = false
287
-	 * @param int $permissions = 0755
288
-	 * @return bool true if the directory has been created.
289
-	 */
290
-	public function makeDirectory($recursive = false, $permissions = 0775)
291
-	{
292
-		// Check if the directory exists
293
-		if ($this->exists()) {
294
-			return false;
295
-		}
296
-
297
-		// Make directory
298
-		$old = umask(0777 - $permissions);
299
-		$result = mkdir($this->path, $permissions, $recursive);
300
-		umask($old);
301
-
302
-		// Return result
303
-		return $result;
304
-	}
305
-
306
-	/**
307
-	 * Returns true if the file is succesfully moved.
308
-	 *
309
-	 * @param string $path
310
-	 * @param bool $override = false
311
-	 * @return bool true if the file is succesfully moved.
312
-	 */
313
-	public function move($path, $override = false)
314
-	{
315
-		// Check if the old file exists
316
-		if (!$this->exists()) {
317
-			return false;
318
-		}
319
-
320
-		// Create file
321
-		$file = new File($path);
322
-
323
-		// Check if the new file exists
324
-		if (($file->exists() && !$override) || !rename($this->path, $file->getPath())) {
325
-			return false;
326
-		}
327
-
328
-		$this->path = $file->getPath();
329
-
330
-		return true;
331
-	}
332
-
333
-	/**
334
-	 * Returns true if the file is succesfully renamed.
335
-	 *
336
-	 * @param string $file
337
-	 * @param bool $override = false
338
-	 * @return bool true if the file is succesfully renamed.
339
-	 */
340
-	public function rename($file, $override = false)
341
-	{
342
-		return $this->move($this->getDirectory() . static::DIRECTORY_SEPARATOR . basename($file), $override);
343
-	}
344
-
345
-	/**
346
-	 * Returns true if the directory is succesfully removed.
347
-	 *
348
-	 * @param bool $recursive = false
349
-	 * @return bool true if the directory is succesfully removed.
350
-	 */
351
-	public function removeDirectory($recursive = false)
352
-	{
353
-		// Check if the directory has to be removed recursive
354
-		if (!$recursive) {
355
-			return rmdir($this->path);
356
-		}
357
-
358
-		// Loop trough the directory
359
-		foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) {
360
-			$path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
361
-		}
362
-
363
-		// Return true
364
-		return true;
365
-	}
366
-
367
-	/**
368
-	 * Returns true if the file is succesfully removed.
369
-	 *
370
-	 * @return bool true if the file is succesfully removed.
371
-	 */
372
-	public function removeFile()
373
-	{
374
-		// Check if the file exists
375
-		if (!$this->isFile()) {
376
-			return false;
377
-		}
378
-
379
-		// Remove file
380
-		return unlink($this->path);
381
-	}
382
-
383
-	/**
384
-	 * Returns the content of the file.
385
-	 *
386
-	 * @return string the content of the file.
387
-	 * @throws FileException on failure.
388
-	 */
389
-	public function read()
390
-	{
391
-		try {
392
-			$result = file_get_contents($this->path);
393
-
394
-			if ($result === false) {
395
-				throw new \Exception('Failed');
396
-			}
397
-
398
-			return $result;
399
-		} catch (\Exception $e) {
400
-			throw new FileException('Can\'t read the content.', $e->getCode(), $e);
401
-		}
402
-	}
403
-
404
-	/**
405
-	 * Append the given content.
406
-	 *
407
-	 * @param string $content
408
-	 * @return null
409
-	 * @throws FileException on failure.
410
-	 */
411
-	public function append($content)
412
-	{
413
-		try {
414
-			if (file_put_contents($this->path, $content, \FILE_APPEND) === false) {
415
-				throw new \Exception('Failed');
416
-			}
417
-		} catch (\Exception $e) {
418
-			throw new FileException('Can\'t append the given content.', $e->getCode(), $e);
419
-		}
420
-	}
421
-
422
-	/**
423
-	 * Write the given content.
424
-	 *
425
-	 * @param string $content
426
-	 * @return null
427
-	 * @throws FileException on failure.
428
-	 */
429
-	public function write($content)
430
-	{
431
-		try {
432
-			if (file_put_contents($this->path, $content) === false) {
433
-				throw new \Exception('Failed');
434
-			}
435
-		} catch (\Exception $e) {
436
-			throw new FileException('Can\'t write the given content.', $e->getCode(), $e);
437
-		}
438
-	}
20
+    const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR;
21
+
22
+    /** @var string the file path. */
23
+    private $path;
24
+
25
+    /**
26
+     * Constructs a File object with the given path.
27
+     *
28
+     * @param string $path
29
+     */
30
+    public function __construct($path)
31
+    {
32
+        if (substr($path, -1) === static::DIRECTORY_SEPARATOR) {
33
+            $this->path = substr($path, 0, -1);
34
+        } else {
35
+            $this->path = $path;
36
+        }
37
+    }
38
+
39
+    /**
40
+     * Returns the string representation of the File object.
41
+     *
42
+     * @return string the string representation of the File object.
43
+     */
44
+    public function __toString()
45
+    {
46
+        return $this->getPath();
47
+    }
48
+
49
+    /**
50
+     * Returns the path of the file.
51
+     *
52
+     * @return string the path of the file.
53
+     */
54
+    public function getPath()
55
+    {
56
+        return $this->path;
57
+    }
58
+
59
+    /**
60
+     * Returns the parent directory of the file.
61
+     *
62
+     * @return string the parent directory of the file.
63
+     */
64
+    public function getDirectory()
65
+    {
66
+        return dirname($this->path);
67
+    }
68
+
69
+    /**
70
+     * Returns the name of the file.
71
+     *
72
+     * @return string the name of the file.
73
+     */
74
+    public function getName()
75
+    {
76
+        return basename($this->path);
77
+    }
78
+
79
+    /**
80
+     * Returns true if the file exists.
81
+     *
82
+     * @return bool true if the file exists.
83
+     */
84
+    public function exists()
85
+    {
86
+        return file_exists($this->path);
87
+    }
88
+
89
+    /**
90
+     * Returns true if you can execute the file.
91
+     *
92
+     * @return bool true if you can execute the file.
93
+     */
94
+    public function canExecute()
95
+    {
96
+        return is_executable($this->path);
97
+    }
98
+
99
+    /**
100
+     * Returns true if you can read the file.
101
+     *
102
+     * @return bool true if you can read the file.
103
+     */
104
+    public function canRead()
105
+    {
106
+        return is_readable($this->path);
107
+    }
108
+
109
+    /**
110
+     * Returns true if you can write the file.
111
+     *
112
+     * @return bool true if you can write the file.
113
+     */
114
+    public function canWrite()
115
+    {
116
+        return is_writeable($this->path);
117
+    }
118
+
119
+    /**
120
+     * Returns true if the file is a file.
121
+     *
122
+     * @return bool true if the file is a file.
123
+     */
124
+    public function isFile()
125
+    {
126
+        return is_file($this->path);
127
+    }
128
+
129
+    /**
130
+     * Returns true if the file is a directory.
131
+     *
132
+     * @return bool true if the file is a directory.
133
+     */
134
+    public function isDirectory()
135
+    {
136
+        return is_dir($this->path);
137
+    }
138
+
139
+    /**
140
+     * Returns the numer of bytes in the file, or -1 on failure.
141
+     *
142
+     * @return int the number of bytes in the file, or -1 on failure.
143
+     */
144
+    public function count()
145
+    {
146
+        // Check if the file exists
147
+        if (!$this->exists()) {
148
+            return -1;
149
+        }
150
+
151
+        // Return length
152
+        return ($result = filesize($this->path)) !== false ? $result : -1;
153
+    }
154
+
155
+    /**
156
+     * Returns the time of the last modification as a unixtimestap, or -1 on failure.
157
+     *
158
+     * @return int the time of the last modification as a unixtimestap, or -1 on failure.
159
+     */
160
+    public function lastModified()
161
+    {
162
+        // Check if the file exists
163
+        if (!$this->exists()) {
164
+            return -1;
165
+        }
166
+
167
+        // Return last modified timestamp
168
+        return ($result = filemtime($this->path)) !== false ? $result : -1;
169
+    }
170
+
171
+    /**
172
+     * Returns an iterator with the files and directories in the current directory.
173
+     *
174
+     * @param bool $recursive = false
175
+     * @param bool $showHidden = false
176
+     * @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory.
177
+     */
178
+    private function listAllIterator($recursive = false, $showHidden = false)
179
+    {
180
+        // Check file
181
+        if (!$this->isDirectory()) {
182
+            return new \ArrayIterator([]);
183
+        }
184
+
185
+        // Check flags
186
+        $flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO;
187
+
188
+        if (!$showHidden) {
189
+            $flags = $flags | \FilesystemIterator::SKIP_DOTS;
190
+        }
191
+
192
+        // Check recursive
193
+        if ($recursive) {
194
+            return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, $flags), \RecursiveIteratorIterator::SELF_FIRST);
195
+        }
196
+
197
+        return new \FilesystemIterator($this->path, $flags);
198
+    }
199
+
200
+    /**
201
+     * Returns an array with the files and directories in the current directory.
202
+     *
203
+     * @param bool $recursive = false
204
+     * @param bool $showHidden = false
205
+     * @return string[] an array with the files and directories in the current directory.
206
+     */
207
+    public function listAll($recursive = false, $showHidden = false)
208
+    {
209
+        // Init
210
+        $result = [];
211
+
212
+        // List all
213
+        foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
214
+            $result[] = $element->getFilename();
215
+        }
216
+
217
+        // Return all
218
+        return $result;
219
+    }
220
+
221
+    /**
222
+     * Returns an array with the directories in the current directory.
223
+     *
224
+     * @param bool $recursive = false
225
+     * @param bool $showHidden = false
226
+     * @return string[] an array with the directories in the current directory.
227
+     */
228
+    public function listDirectories($recursive = false, $showHidden = false)
229
+    {
230
+        // Init
231
+        $result = [];
232
+
233
+        // List directories
234
+        foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
235
+            if ($element->isDir()) {
236
+                $result[] = $element->getFilename();
237
+            }
238
+        }
239
+
240
+        // Return directories
241
+        return $result;
242
+    }
243
+
244
+    /**
245
+     * Returns an array with the files in the current directory.
246
+     *
247
+     * @param bool $recursive = false
248
+     * @param bool $showHidden = false
249
+     * @return string[] an array with the files in the current directory.
250
+     */
251
+    public function listFiles($recursive = false, $showHidden = false)
252
+    {
253
+        // Init
254
+        $result = [];
255
+
256
+        // List files
257
+        foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
258
+            if ($element->isFile()) {
259
+                $result[] = $element->getFilename();
260
+            }
261
+        }
262
+
263
+        // Return files
264
+        return $result;
265
+    }
266
+
267
+    /**
268
+     * Returns true if the file has been created.
269
+     *
270
+     * @param bool $override = false
271
+     * @return bool true if the file has been created.
272
+     */
273
+    public function makeFile($override = false)
274
+    {
275
+        // Check if the file exists
276
+        if ($this->exists() && !$override) {
277
+            return false;
278
+        }
279
+
280
+        return file_put_contents($this->path, '') !== false;
281
+    }
282
+
283
+    /**
284
+     * Returns true if the directory has been created.
285
+     *
286
+     * @param bool $recursive = false
287
+     * @param int $permissions = 0755
288
+     * @return bool true if the directory has been created.
289
+     */
290
+    public function makeDirectory($recursive = false, $permissions = 0775)
291
+    {
292
+        // Check if the directory exists
293
+        if ($this->exists()) {
294
+            return false;
295
+        }
296
+
297
+        // Make directory
298
+        $old = umask(0777 - $permissions);
299
+        $result = mkdir($this->path, $permissions, $recursive);
300
+        umask($old);
301
+
302
+        // Return result
303
+        return $result;
304
+    }
305
+
306
+    /**
307
+     * Returns true if the file is succesfully moved.
308
+     *
309
+     * @param string $path
310
+     * @param bool $override = false
311
+     * @return bool true if the file is succesfully moved.
312
+     */
313
+    public function move($path, $override = false)
314
+    {
315
+        // Check if the old file exists
316
+        if (!$this->exists()) {
317
+            return false;
318
+        }
319
+
320
+        // Create file
321
+        $file = new File($path);
322
+
323
+        // Check if the new file exists
324
+        if (($file->exists() && !$override) || !rename($this->path, $file->getPath())) {
325
+            return false;
326
+        }
327
+
328
+        $this->path = $file->getPath();
329
+
330
+        return true;
331
+    }
332
+
333
+    /**
334
+     * Returns true if the file is succesfully renamed.
335
+     *
336
+     * @param string $file
337
+     * @param bool $override = false
338
+     * @return bool true if the file is succesfully renamed.
339
+     */
340
+    public function rename($file, $override = false)
341
+    {
342
+        return $this->move($this->getDirectory() . static::DIRECTORY_SEPARATOR . basename($file), $override);
343
+    }
344
+
345
+    /**
346
+     * Returns true if the directory is succesfully removed.
347
+     *
348
+     * @param bool $recursive = false
349
+     * @return bool true if the directory is succesfully removed.
350
+     */
351
+    public function removeDirectory($recursive = false)
352
+    {
353
+        // Check if the directory has to be removed recursive
354
+        if (!$recursive) {
355
+            return rmdir($this->path);
356
+        }
357
+
358
+        // Loop trough the directory
359
+        foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) {
360
+            $path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
361
+        }
362
+
363
+        // Return true
364
+        return true;
365
+    }
366
+
367
+    /**
368
+     * Returns true if the file is succesfully removed.
369
+     *
370
+     * @return bool true if the file is succesfully removed.
371
+     */
372
+    public function removeFile()
373
+    {
374
+        // Check if the file exists
375
+        if (!$this->isFile()) {
376
+            return false;
377
+        }
378
+
379
+        // Remove file
380
+        return unlink($this->path);
381
+    }
382
+
383
+    /**
384
+     * Returns the content of the file.
385
+     *
386
+     * @return string the content of the file.
387
+     * @throws FileException on failure.
388
+     */
389
+    public function read()
390
+    {
391
+        try {
392
+            $result = file_get_contents($this->path);
393
+
394
+            if ($result === false) {
395
+                throw new \Exception('Failed');
396
+            }
397
+
398
+            return $result;
399
+        } catch (\Exception $e) {
400
+            throw new FileException('Can\'t read the content.', $e->getCode(), $e);
401
+        }
402
+    }
403
+
404
+    /**
405
+     * Append the given content.
406
+     *
407
+     * @param string $content
408
+     * @return null
409
+     * @throws FileException on failure.
410
+     */
411
+    public function append($content)
412
+    {
413
+        try {
414
+            if (file_put_contents($this->path, $content, \FILE_APPEND) === false) {
415
+                throw new \Exception('Failed');
416
+            }
417
+        } catch (\Exception $e) {
418
+            throw new FileException('Can\'t append the given content.', $e->getCode(), $e);
419
+        }
420
+    }
421
+
422
+    /**
423
+     * Write the given content.
424
+     *
425
+     * @param string $content
426
+     * @return null
427
+     * @throws FileException on failure.
428
+     */
429
+    public function write($content)
430
+    {
431
+        try {
432
+            if (file_put_contents($this->path, $content) === false) {
433
+                throw new \Exception('Failed');
434
+            }
435
+        } catch (\Exception $e) {
436
+            throw new FileException('Can\'t write the given content.', $e->getCode(), $e);
437
+        }
438
+    }
439 439
 }
Please login to merge, or discard this patch.