Completed
Push — develop ( 88e90e...a239dd )
by Michael
02:36
created
src/File.php 1 patch
Indentation   +389 added lines, -389 removed lines patch added patch discarded remove patch
@@ -17,393 +17,393 @@
 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
-		return $this->length();
147
-	}
148
-
149
-	/**
150
-	 * Returns the numer of bytes in the file, or -1 on failure.
151
-	 *
152
-	 * @return int the number of bytes in the file, or -1 on failure.
153
-	 */
154
-	public function length()
155
-	{
156
-		if (!$this->exists()) {
157
-			return -1;
158
-		}
159
-
160
-		return ($result = filesize($this->path)) !== false ? $result : -1;
161
-	}
162
-
163
-	/**
164
-	 * Returns the time of the last modification as a unixtimestap, or -1 on failure.
165
-	 *
166
-	 * @return int the time of the last modification as a unixtimestap, or -1 on failure.
167
-	 */
168
-	public function lastModified()
169
-	{
170
-		if (!$this->exists()) {
171
-			return -1;
172
-		}
173
-
174
-		return ($result = filemtime($this->path)) !== false ? $result : -1;
175
-	}
176
-
177
-	/**
178
-	 * Returns an iterator with the files and directories in the current directory.
179
-	 *
180
-	 * @param bool $recursive = false
181
-	 * @param bool $showHidden = false
182
-	 * @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory.
183
-	 */
184
-	private function listAllIterator($recursive = false, $showHidden = false)
185
-	{
186
-		if (!$this->isDirectory()) {
187
-			return new \ArrayIterator([]);
188
-		}
189
-
190
-		$flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO;
191
-
192
-		if (!$showHidden) {
193
-			$flags = $flags | \FilesystemIterator::SKIP_DOTS;
194
-		}
195
-
196
-		if ($recursive) {
197
-			return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, $flags), \RecursiveIteratorIterator::SELF_FIRST);
198
-		}
199
-
200
-		return new \FilesystemIterator($this->path, $flags);
201
-	}
202
-
203
-	/**
204
-	 * Returns an array with the files and directories in the current directory.
205
-	 *
206
-	 * @param bool $recursive = false
207
-	 * @param bool $showHidden = false
208
-	 * @return string[] an array with the files and directories in the current directory.
209
-	 */
210
-	public function listAll($recursive = false, $showHidden = false)
211
-	{
212
-		$result = [];
213
-
214
-		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
215
-			$result[] = $element->getFilename();
216
-		}
217
-
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
-		$result = [];
231
-
232
-		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
233
-			if ($element->isDir()) {
234
-				$result[] = $element->getFilename();
235
-			}
236
-		}
237
-
238
-		return $result;
239
-	}
240
-
241
-	/**
242
-	 * Returns an array with the files in the current directory.
243
-	 *
244
-	 * @param bool $recursive = false
245
-	 * @param bool $showHidden = false
246
-	 * @return string[] an array with the files in the current directory.
247
-	 */
248
-	public function listFiles($recursive = false, $showHidden = false)
249
-	{
250
-		$result = [];
251
-
252
-		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
253
-			if ($element->isFile()) {
254
-				$result[] = $element->getFilename();
255
-			}
256
-		}
257
-
258
-		return $result;
259
-	}
260
-
261
-	/**
262
-	 * Returns true if the file has been created.
263
-	 *
264
-	 * @param bool $override = false
265
-	 * @return bool true if the file has been created.
266
-	 */
267
-	public function makeFile($override = false)
268
-	{
269
-		if ($this->exists() && !$override) {
270
-			return false;
271
-		}
272
-
273
-		return file_put_contents($this->path, '') !== false;
274
-	}
275
-
276
-	/**
277
-	 * Returns true if the directory has been created.
278
-	 *
279
-	 * @param bool $recursive = false
280
-	 * @param int $permissions = 0755
281
-	 * @return bool true if the directory has been created.
282
-	 */
283
-	public function makeDirectory($recursive = false, $permissions = 0775)
284
-	{
285
-		if ($this->exists()) {
286
-			return false;
287
-		}
288
-
289
-		$old = umask(0777 - $permissions);
290
-		$result = mkdir($this->path, $permissions, $recursive);
291
-		umask($old);
292
-
293
-		return $result;
294
-	}
295
-
296
-	/**
297
-	 * Returns true if the file is succesfully moved.
298
-	 *
299
-	 * @param string $path
300
-	 * @param bool $override = false
301
-	 * @return bool true if the file is succesfully moved.
302
-	 */
303
-	public function move($path, $override = false)
304
-	{
305
-		if (!$this->exists()) {
306
-			return false;
307
-		}
308
-
309
-		$file = new File($path);
310
-
311
-		if (($file->exists() && !$override) || !rename($this->path, $file->getPath())) {
312
-			return false;
313
-		}
314
-
315
-		$this->path = $file->getPath();
316
-
317
-		return true;
318
-	}
319
-
320
-	/**
321
-	 * Returns true if the file is succesfully renamed.
322
-	 *
323
-	 * @param string $file
324
-	 * @param bool $override = false
325
-	 * @return bool true if the file is succesfully renamed.
326
-	 */
327
-	public function rename($file, $override = false)
328
-	{
329
-		return $this->move($this->getDirectory() . static::DIRECTORY_SEPARATOR . basename($file), $override);
330
-	}
331
-
332
-	/**
333
-	 * Returns true if the directory is succesfully removed.
334
-	 *
335
-	 * @param bool $recursive = false
336
-	 * @return bool true if the directory is succesfully removed.
337
-	 */
338
-	public function removeDirectory($recursive = false)
339
-	{
340
-		if (!$recursive) {
341
-			return rmdir($this->path);
342
-		}
343
-
344
-		foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) {
345
-			$path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
346
-		}
347
-
348
-		return true;
349
-	}
350
-
351
-	/**
352
-	 * Returns true if the file is succesfully removed.
353
-	 *
354
-	 * @return bool true if the file is succesfully removed.
355
-	 */
356
-	public function removeFile()
357
-	{
358
-		if (!$this->isFile()) {
359
-			return false;
360
-		}
361
-
362
-		return unlink($this->path);
363
-	}
364
-
365
-	/**
366
-	 * Returns the content of the file.
367
-	 *
368
-	 * @return string the content of the file.
369
-	 * @throws FileException on failure.
370
-	 */
371
-	public function read()
372
-	{
373
-		$result = file_get_contents($this->path);
374
-
375
-		if ($result === false) {
376
-			throw new FileException('Can\'t read the content.');
377
-		}
378
-
379
-		return $result;
380
-	}
381
-
382
-	/**
383
-	 * Append the given content.
384
-	 *
385
-	 * @param string $content
386
-	 * @return null
387
-	 * @throws FileException on failure.
388
-	 */
389
-	public function append($content)
390
-	{
391
-		if (file_put_contents($this->path, $content, \FILE_APPEND) === false) {
392
-			throw new FileException('Can\'t append the given content.');
393
-		}
394
-	}
395
-
396
-	/**
397
-	 * Write the given content.
398
-	 *
399
-	 * @param string $content
400
-	 * @return null
401
-	 * @throws FileException on failure.
402
-	 */
403
-	public function write($content)
404
-	{
405
-		if (file_put_contents($this->path, $content) === false) {
406
-			throw new FileException('Can\'t write the given content.');
407
-		}
408
-	}
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
+        return $this->length();
147
+    }
148
+
149
+    /**
150
+     * Returns the numer of bytes in the file, or -1 on failure.
151
+     *
152
+     * @return int the number of bytes in the file, or -1 on failure.
153
+     */
154
+    public function length()
155
+    {
156
+        if (!$this->exists()) {
157
+            return -1;
158
+        }
159
+
160
+        return ($result = filesize($this->path)) !== false ? $result : -1;
161
+    }
162
+
163
+    /**
164
+     * Returns the time of the last modification as a unixtimestap, or -1 on failure.
165
+     *
166
+     * @return int the time of the last modification as a unixtimestap, or -1 on failure.
167
+     */
168
+    public function lastModified()
169
+    {
170
+        if (!$this->exists()) {
171
+            return -1;
172
+        }
173
+
174
+        return ($result = filemtime($this->path)) !== false ? $result : -1;
175
+    }
176
+
177
+    /**
178
+     * Returns an iterator with the files and directories in the current directory.
179
+     *
180
+     * @param bool $recursive = false
181
+     * @param bool $showHidden = false
182
+     * @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory.
183
+     */
184
+    private function listAllIterator($recursive = false, $showHidden = false)
185
+    {
186
+        if (!$this->isDirectory()) {
187
+            return new \ArrayIterator([]);
188
+        }
189
+
190
+        $flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO;
191
+
192
+        if (!$showHidden) {
193
+            $flags = $flags | \FilesystemIterator::SKIP_DOTS;
194
+        }
195
+
196
+        if ($recursive) {
197
+            return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, $flags), \RecursiveIteratorIterator::SELF_FIRST);
198
+        }
199
+
200
+        return new \FilesystemIterator($this->path, $flags);
201
+    }
202
+
203
+    /**
204
+     * Returns an array with the files and directories in the current directory.
205
+     *
206
+     * @param bool $recursive = false
207
+     * @param bool $showHidden = false
208
+     * @return string[] an array with the files and directories in the current directory.
209
+     */
210
+    public function listAll($recursive = false, $showHidden = false)
211
+    {
212
+        $result = [];
213
+
214
+        foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
215
+            $result[] = $element->getFilename();
216
+        }
217
+
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
+        $result = [];
231
+
232
+        foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
233
+            if ($element->isDir()) {
234
+                $result[] = $element->getFilename();
235
+            }
236
+        }
237
+
238
+        return $result;
239
+    }
240
+
241
+    /**
242
+     * Returns an array with the files in the current directory.
243
+     *
244
+     * @param bool $recursive = false
245
+     * @param bool $showHidden = false
246
+     * @return string[] an array with the files in the current directory.
247
+     */
248
+    public function listFiles($recursive = false, $showHidden = false)
249
+    {
250
+        $result = [];
251
+
252
+        foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
253
+            if ($element->isFile()) {
254
+                $result[] = $element->getFilename();
255
+            }
256
+        }
257
+
258
+        return $result;
259
+    }
260
+
261
+    /**
262
+     * Returns true if the file has been created.
263
+     *
264
+     * @param bool $override = false
265
+     * @return bool true if the file has been created.
266
+     */
267
+    public function makeFile($override = false)
268
+    {
269
+        if ($this->exists() && !$override) {
270
+            return false;
271
+        }
272
+
273
+        return file_put_contents($this->path, '') !== false;
274
+    }
275
+
276
+    /**
277
+     * Returns true if the directory has been created.
278
+     *
279
+     * @param bool $recursive = false
280
+     * @param int $permissions = 0755
281
+     * @return bool true if the directory has been created.
282
+     */
283
+    public function makeDirectory($recursive = false, $permissions = 0775)
284
+    {
285
+        if ($this->exists()) {
286
+            return false;
287
+        }
288
+
289
+        $old = umask(0777 - $permissions);
290
+        $result = mkdir($this->path, $permissions, $recursive);
291
+        umask($old);
292
+
293
+        return $result;
294
+    }
295
+
296
+    /**
297
+     * Returns true if the file is succesfully moved.
298
+     *
299
+     * @param string $path
300
+     * @param bool $override = false
301
+     * @return bool true if the file is succesfully moved.
302
+     */
303
+    public function move($path, $override = false)
304
+    {
305
+        if (!$this->exists()) {
306
+            return false;
307
+        }
308
+
309
+        $file = new File($path);
310
+
311
+        if (($file->exists() && !$override) || !rename($this->path, $file->getPath())) {
312
+            return false;
313
+        }
314
+
315
+        $this->path = $file->getPath();
316
+
317
+        return true;
318
+    }
319
+
320
+    /**
321
+     * Returns true if the file is succesfully renamed.
322
+     *
323
+     * @param string $file
324
+     * @param bool $override = false
325
+     * @return bool true if the file is succesfully renamed.
326
+     */
327
+    public function rename($file, $override = false)
328
+    {
329
+        return $this->move($this->getDirectory() . static::DIRECTORY_SEPARATOR . basename($file), $override);
330
+    }
331
+
332
+    /**
333
+     * Returns true if the directory is succesfully removed.
334
+     *
335
+     * @param bool $recursive = false
336
+     * @return bool true if the directory is succesfully removed.
337
+     */
338
+    public function removeDirectory($recursive = false)
339
+    {
340
+        if (!$recursive) {
341
+            return rmdir($this->path);
342
+        }
343
+
344
+        foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) {
345
+            $path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
346
+        }
347
+
348
+        return true;
349
+    }
350
+
351
+    /**
352
+     * Returns true if the file is succesfully removed.
353
+     *
354
+     * @return bool true if the file is succesfully removed.
355
+     */
356
+    public function removeFile()
357
+    {
358
+        if (!$this->isFile()) {
359
+            return false;
360
+        }
361
+
362
+        return unlink($this->path);
363
+    }
364
+
365
+    /**
366
+     * Returns the content of the file.
367
+     *
368
+     * @return string the content of the file.
369
+     * @throws FileException on failure.
370
+     */
371
+    public function read()
372
+    {
373
+        $result = file_get_contents($this->path);
374
+
375
+        if ($result === false) {
376
+            throw new FileException('Can\'t read the content.');
377
+        }
378
+
379
+        return $result;
380
+    }
381
+
382
+    /**
383
+     * Append the given content.
384
+     *
385
+     * @param string $content
386
+     * @return null
387
+     * @throws FileException on failure.
388
+     */
389
+    public function append($content)
390
+    {
391
+        if (file_put_contents($this->path, $content, \FILE_APPEND) === false) {
392
+            throw new FileException('Can\'t append the given content.');
393
+        }
394
+    }
395
+
396
+    /**
397
+     * Write the given content.
398
+     *
399
+     * @param string $content
400
+     * @return null
401
+     * @throws FileException on failure.
402
+     */
403
+    public function write($content)
404
+    {
405
+        if (file_put_contents($this->path, $content) === false) {
406
+            throw new FileException('Can\'t write the given content.');
407
+        }
408
+    }
409 409
 }
Please login to merge, or discard this patch.