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