@@ -16,427 +16,427 @@ |
||
| 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 the extension of the file |
|
| 80 | - * |
|
| 81 | - * @return string the file extension |
|
| 82 | - */ |
|
| 83 | - public function getExtension() |
|
| 84 | - { |
|
| 85 | - return pathinfo($this->path, PATHINFO_EXTENSION); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Returns the mime-type as determined by information from php's magic.mime file, null on failure |
|
| 90 | - * |
|
| 91 | - * @return string|null the mime type |
|
| 92 | - */ |
|
| 93 | - public function getMimeType() |
|
| 94 | - { |
|
| 95 | - $mime = mime_content_type($this->path); |
|
| 96 | - if ($mime === false) { |
|
| 97 | - return null; |
|
| 98 | - } |
|
| 99 | - return $mime; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Returns true if the file exists. |
|
| 104 | - * |
|
| 105 | - * @return bool true if the file exists. |
|
| 106 | - */ |
|
| 107 | - public function exists() |
|
| 108 | - { |
|
| 109 | - return file_exists($this->path); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Returns true if you can execute the file. |
|
| 114 | - * |
|
| 115 | - * @return bool true if you can execute the file. |
|
| 116 | - */ |
|
| 117 | - public function canExecute() |
|
| 118 | - { |
|
| 119 | - return is_executable($this->path); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Returns true if you can read the file. |
|
| 124 | - * |
|
| 125 | - * @return bool true if you can read the file. |
|
| 126 | - */ |
|
| 127 | - public function canRead() |
|
| 128 | - { |
|
| 129 | - return is_readable($this->path); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Returns true if you can write the file. |
|
| 134 | - * |
|
| 135 | - * @return bool true if you can write the file. |
|
| 136 | - */ |
|
| 137 | - public function canWrite() |
|
| 138 | - { |
|
| 139 | - return is_writeable($this->path); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Returns true if the file is a file. |
|
| 144 | - * |
|
| 145 | - * @return bool true if the file is a file. |
|
| 146 | - */ |
|
| 147 | - public function isFile() |
|
| 148 | - { |
|
| 149 | - return is_file($this->path); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Returns true if the file is a directory. |
|
| 154 | - * |
|
| 155 | - * @return bool true if the file is a directory. |
|
| 156 | - */ |
|
| 157 | - public function isDirectory() |
|
| 158 | - { |
|
| 159 | - return is_dir($this->path); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns the numer of bytes in the file, or -1 on failure. |
|
| 164 | - * |
|
| 165 | - * @return int the number of bytes in the file, or -1 on failure. |
|
| 166 | - */ |
|
| 167 | - public function count() |
|
| 168 | - { |
|
| 169 | - return $this->length(); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Returns the numer of bytes in the file, or -1 on failure. |
|
| 174 | - * |
|
| 175 | - * @return int the number of bytes in the file, or -1 on failure. |
|
| 176 | - */ |
|
| 177 | - public function size() |
|
| 178 | - { |
|
| 179 | - return $this->length(); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Returns the numer of bytes in the file, or -1 on failure. |
|
| 184 | - * |
|
| 185 | - * @return int the number of bytes in the file, or -1 on failure. |
|
| 186 | - */ |
|
| 187 | - public function length() |
|
| 188 | - { |
|
| 189 | - if (!$this->exists()) { |
|
| 190 | - return -1; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - return ($result = filesize($this->path)) !== false ? $result : -1; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Returns the time of the last modification as a unixtimestap, or -1 on failure. |
|
| 198 | - * |
|
| 199 | - * @return int the time of the last modification as a unixtimestap, or -1 on failure. |
|
| 200 | - */ |
|
| 201 | - public function lastModified() |
|
| 202 | - { |
|
| 203 | - if (!$this->exists()) { |
|
| 204 | - return -1; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return ($result = filemtime($this->path)) !== false ? $result : -1; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Returns an iterator with the files and directories in the current directory. |
|
| 212 | - * |
|
| 213 | - * @param bool $recursive = false |
|
| 214 | - * @param bool $showHidden = false |
|
| 215 | - * @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory. |
|
| 216 | - */ |
|
| 217 | - private function listAllIterator($recursive = false, $showHidden = false) |
|
| 218 | - { |
|
| 219 | - if (!$this->isDirectory()) { |
|
| 220 | - return new \ArrayIterator([]); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO; |
|
| 224 | - |
|
| 225 | - if (!$showHidden) { |
|
| 226 | - $flags = $flags | \FilesystemIterator::SKIP_DOTS; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - if ($recursive) { |
|
| 230 | - return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, $flags), \RecursiveIteratorIterator::SELF_FIRST); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - return new \FilesystemIterator($this->path, $flags); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Returns an array with the files and directories in the current directory. |
|
| 238 | - * |
|
| 239 | - * @param bool $recursive = false |
|
| 240 | - * @param bool $showHidden = false |
|
| 241 | - * @return string[] an array with the files and directories in the current directory. |
|
| 242 | - */ |
|
| 243 | - public function listAll($recursive = false, $showHidden = false) |
|
| 244 | - { |
|
| 245 | - $result = []; |
|
| 246 | - |
|
| 247 | - foreach ($this->listAllIterator($recursive, $showHidden) as $element) { |
|
| 248 | - $result[] = $element->getFilename(); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - return $result; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Returns an array with the directories in the current directory. |
|
| 256 | - * |
|
| 257 | - * @param bool $recursive = false |
|
| 258 | - * @param bool $showHidden = false |
|
| 259 | - * @return string[] an array with the directories in the current directory. |
|
| 260 | - */ |
|
| 261 | - public function listDirectories($recursive = false, $showHidden = false) |
|
| 262 | - { |
|
| 263 | - $result = []; |
|
| 264 | - |
|
| 265 | - foreach ($this->listAllIterator($recursive, $showHidden) as $element) { |
|
| 266 | - if ($element->isDir()) { |
|
| 267 | - $result[] = $element->getFilename(); |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - return $result; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Returns an array with the files in the current directory. |
|
| 276 | - * |
|
| 277 | - * @param bool $recursive = false |
|
| 278 | - * @param bool $showHidden = false |
|
| 279 | - * @return string[] an array with the files in the current directory. |
|
| 280 | - */ |
|
| 281 | - public function listFiles($recursive = false, $showHidden = false) |
|
| 282 | - { |
|
| 283 | - $result = []; |
|
| 284 | - |
|
| 285 | - foreach ($this->listAllIterator($recursive, $showHidden) as $element) { |
|
| 286 | - if ($element->isFile()) { |
|
| 287 | - $result[] = $element->getFilename(); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - return $result; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Returns true if the file has been created. |
|
| 296 | - * |
|
| 297 | - * @param bool $override = false |
|
| 298 | - * @return bool true if the file has been created. |
|
| 299 | - */ |
|
| 300 | - public function makeFile($override = false) |
|
| 301 | - { |
|
| 302 | - if ($this->exists() && !$override) { |
|
| 303 | - return false; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - return file_put_contents($this->path, '') !== false; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Returns true if the directory has been created. |
|
| 311 | - * |
|
| 312 | - * @param bool $recursive = false |
|
| 313 | - * @param int $permissions = 0755 |
|
| 314 | - * @return bool true if the directory has been created. |
|
| 315 | - */ |
|
| 316 | - public function makeDirectory($recursive = false, $permissions = 0775) |
|
| 317 | - { |
|
| 318 | - if ($this->exists()) { |
|
| 319 | - return false; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - $old = umask(0777 - $permissions); |
|
| 323 | - $result = mkdir($this->path, $permissions, $recursive); |
|
| 324 | - umask($old); |
|
| 325 | - |
|
| 326 | - return $result; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Returns true if the file is succesfully moved. |
|
| 331 | - * |
|
| 332 | - * @param string $path |
|
| 333 | - * @param bool $override = false |
|
| 334 | - * @return bool true if the file is succesfully moved. |
|
| 335 | - */ |
|
| 336 | - public function move($path, $override = false) |
|
| 337 | - { |
|
| 338 | - if (!$this->exists()) { |
|
| 339 | - return false; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - $file = new File($path); |
|
| 343 | - |
|
| 344 | - if (($file->exists() && !$override) || !rename($this->path, $file->getPath())) { |
|
| 345 | - return false; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - $this->path = $file->getPath(); |
|
| 349 | - |
|
| 350 | - return true; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Returns true if the file is succesfully renamed. |
|
| 355 | - * |
|
| 356 | - * @param string $file |
|
| 357 | - * @param bool $override = false |
|
| 358 | - * @return bool true if the file is succesfully renamed. |
|
| 359 | - */ |
|
| 360 | - public function rename($file, $override = false) |
|
| 361 | - { |
|
| 362 | - return $this->move($this->getDirectory() . static::DIRECTORY_SEPARATOR . basename($file), $override); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Returns true if the directory is succesfully removed. |
|
| 367 | - * |
|
| 368 | - * @param bool $recursive = false |
|
| 369 | - * @return bool true if the directory is succesfully removed. |
|
| 370 | - */ |
|
| 371 | - public function removeDirectory($recursive = false) |
|
| 372 | - { |
|
| 373 | - if (!$recursive) { |
|
| 374 | - return rmdir($this->path); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) { |
|
| 378 | - $path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname()); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - return true; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Returns true if the file is succesfully removed. |
|
| 386 | - * |
|
| 387 | - * @return bool true if the file is succesfully removed. |
|
| 388 | - */ |
|
| 389 | - public function removeFile() |
|
| 390 | - { |
|
| 391 | - if (!$this->isFile()) { |
|
| 392 | - return false; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - return unlink($this->path); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Returns the content of the file. |
|
| 400 | - * |
|
| 401 | - * @return string the content of the file. |
|
| 402 | - * @throws FileException on failure. |
|
| 403 | - */ |
|
| 404 | - public function read() |
|
| 405 | - { |
|
| 406 | - $result = file_get_contents($this->path); |
|
| 407 | - |
|
| 408 | - if ($result === false) { |
|
| 409 | - throw new FileException('Can\'t read the content.'); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - return $result; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * Append the given content. |
|
| 417 | - * |
|
| 418 | - * @param string $content |
|
| 419 | - * @return null |
|
| 420 | - * @throws FileException on failure. |
|
| 421 | - */ |
|
| 422 | - public function append($content) |
|
| 423 | - { |
|
| 424 | - if (file_put_contents($this->path, $content, \FILE_APPEND) === false) { |
|
| 425 | - throw new FileException('Can\'t append the given content.'); |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Write the given content. |
|
| 431 | - * |
|
| 432 | - * @param string $content |
|
| 433 | - * @return null |
|
| 434 | - * @throws FileException on failure. |
|
| 435 | - */ |
|
| 436 | - public function write($content) |
|
| 437 | - { |
|
| 438 | - if (file_put_contents($this->path, $content) === false) { |
|
| 439 | - throw new FileException('Can\'t write the given content.'); |
|
| 440 | - } |
|
| 441 | - } |
|
| 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 the extension of the file |
|
| 80 | + * |
|
| 81 | + * @return string the file extension |
|
| 82 | + */ |
|
| 83 | + public function getExtension() |
|
| 84 | + { |
|
| 85 | + return pathinfo($this->path, PATHINFO_EXTENSION); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Returns the mime-type as determined by information from php's magic.mime file, null on failure |
|
| 90 | + * |
|
| 91 | + * @return string|null the mime type |
|
| 92 | + */ |
|
| 93 | + public function getMimeType() |
|
| 94 | + { |
|
| 95 | + $mime = mime_content_type($this->path); |
|
| 96 | + if ($mime === false) { |
|
| 97 | + return null; |
|
| 98 | + } |
|
| 99 | + return $mime; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Returns true if the file exists. |
|
| 104 | + * |
|
| 105 | + * @return bool true if the file exists. |
|
| 106 | + */ |
|
| 107 | + public function exists() |
|
| 108 | + { |
|
| 109 | + return file_exists($this->path); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Returns true if you can execute the file. |
|
| 114 | + * |
|
| 115 | + * @return bool true if you can execute the file. |
|
| 116 | + */ |
|
| 117 | + public function canExecute() |
|
| 118 | + { |
|
| 119 | + return is_executable($this->path); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Returns true if you can read the file. |
|
| 124 | + * |
|
| 125 | + * @return bool true if you can read the file. |
|
| 126 | + */ |
|
| 127 | + public function canRead() |
|
| 128 | + { |
|
| 129 | + return is_readable($this->path); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Returns true if you can write the file. |
|
| 134 | + * |
|
| 135 | + * @return bool true if you can write the file. |
|
| 136 | + */ |
|
| 137 | + public function canWrite() |
|
| 138 | + { |
|
| 139 | + return is_writeable($this->path); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Returns true if the file is a file. |
|
| 144 | + * |
|
| 145 | + * @return bool true if the file is a file. |
|
| 146 | + */ |
|
| 147 | + public function isFile() |
|
| 148 | + { |
|
| 149 | + return is_file($this->path); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Returns true if the file is a directory. |
|
| 154 | + * |
|
| 155 | + * @return bool true if the file is a directory. |
|
| 156 | + */ |
|
| 157 | + public function isDirectory() |
|
| 158 | + { |
|
| 159 | + return is_dir($this->path); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns the numer of bytes in the file, or -1 on failure. |
|
| 164 | + * |
|
| 165 | + * @return int the number of bytes in the file, or -1 on failure. |
|
| 166 | + */ |
|
| 167 | + public function count() |
|
| 168 | + { |
|
| 169 | + return $this->length(); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Returns the numer of bytes in the file, or -1 on failure. |
|
| 174 | + * |
|
| 175 | + * @return int the number of bytes in the file, or -1 on failure. |
|
| 176 | + */ |
|
| 177 | + public function size() |
|
| 178 | + { |
|
| 179 | + return $this->length(); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Returns the numer of bytes in the file, or -1 on failure. |
|
| 184 | + * |
|
| 185 | + * @return int the number of bytes in the file, or -1 on failure. |
|
| 186 | + */ |
|
| 187 | + public function length() |
|
| 188 | + { |
|
| 189 | + if (!$this->exists()) { |
|
| 190 | + return -1; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + return ($result = filesize($this->path)) !== false ? $result : -1; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Returns the time of the last modification as a unixtimestap, or -1 on failure. |
|
| 198 | + * |
|
| 199 | + * @return int the time of the last modification as a unixtimestap, or -1 on failure. |
|
| 200 | + */ |
|
| 201 | + public function lastModified() |
|
| 202 | + { |
|
| 203 | + if (!$this->exists()) { |
|
| 204 | + return -1; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return ($result = filemtime($this->path)) !== false ? $result : -1; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Returns an iterator with the files and directories in the current directory. |
|
| 212 | + * |
|
| 213 | + * @param bool $recursive = false |
|
| 214 | + * @param bool $showHidden = false |
|
| 215 | + * @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory. |
|
| 216 | + */ |
|
| 217 | + private function listAllIterator($recursive = false, $showHidden = false) |
|
| 218 | + { |
|
| 219 | + if (!$this->isDirectory()) { |
|
| 220 | + return new \ArrayIterator([]); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO; |
|
| 224 | + |
|
| 225 | + if (!$showHidden) { |
|
| 226 | + $flags = $flags | \FilesystemIterator::SKIP_DOTS; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + if ($recursive) { |
|
| 230 | + return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, $flags), \RecursiveIteratorIterator::SELF_FIRST); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + return new \FilesystemIterator($this->path, $flags); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Returns an array with the files and directories in the current directory. |
|
| 238 | + * |
|
| 239 | + * @param bool $recursive = false |
|
| 240 | + * @param bool $showHidden = false |
|
| 241 | + * @return string[] an array with the files and directories in the current directory. |
|
| 242 | + */ |
|
| 243 | + public function listAll($recursive = false, $showHidden = false) |
|
| 244 | + { |
|
| 245 | + $result = []; |
|
| 246 | + |
|
| 247 | + foreach ($this->listAllIterator($recursive, $showHidden) as $element) { |
|
| 248 | + $result[] = $element->getFilename(); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + return $result; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Returns an array with the directories in the current directory. |
|
| 256 | + * |
|
| 257 | + * @param bool $recursive = false |
|
| 258 | + * @param bool $showHidden = false |
|
| 259 | + * @return string[] an array with the directories in the current directory. |
|
| 260 | + */ |
|
| 261 | + public function listDirectories($recursive = false, $showHidden = false) |
|
| 262 | + { |
|
| 263 | + $result = []; |
|
| 264 | + |
|
| 265 | + foreach ($this->listAllIterator($recursive, $showHidden) as $element) { |
|
| 266 | + if ($element->isDir()) { |
|
| 267 | + $result[] = $element->getFilename(); |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + return $result; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Returns an array with the files in the current directory. |
|
| 276 | + * |
|
| 277 | + * @param bool $recursive = false |
|
| 278 | + * @param bool $showHidden = false |
|
| 279 | + * @return string[] an array with the files in the current directory. |
|
| 280 | + */ |
|
| 281 | + public function listFiles($recursive = false, $showHidden = false) |
|
| 282 | + { |
|
| 283 | + $result = []; |
|
| 284 | + |
|
| 285 | + foreach ($this->listAllIterator($recursive, $showHidden) as $element) { |
|
| 286 | + if ($element->isFile()) { |
|
| 287 | + $result[] = $element->getFilename(); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + return $result; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Returns true if the file has been created. |
|
| 296 | + * |
|
| 297 | + * @param bool $override = false |
|
| 298 | + * @return bool true if the file has been created. |
|
| 299 | + */ |
|
| 300 | + public function makeFile($override = false) |
|
| 301 | + { |
|
| 302 | + if ($this->exists() && !$override) { |
|
| 303 | + return false; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + return file_put_contents($this->path, '') !== false; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Returns true if the directory has been created. |
|
| 311 | + * |
|
| 312 | + * @param bool $recursive = false |
|
| 313 | + * @param int $permissions = 0755 |
|
| 314 | + * @return bool true if the directory has been created. |
|
| 315 | + */ |
|
| 316 | + public function makeDirectory($recursive = false, $permissions = 0775) |
|
| 317 | + { |
|
| 318 | + if ($this->exists()) { |
|
| 319 | + return false; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + $old = umask(0777 - $permissions); |
|
| 323 | + $result = mkdir($this->path, $permissions, $recursive); |
|
| 324 | + umask($old); |
|
| 325 | + |
|
| 326 | + return $result; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Returns true if the file is succesfully moved. |
|
| 331 | + * |
|
| 332 | + * @param string $path |
|
| 333 | + * @param bool $override = false |
|
| 334 | + * @return bool true if the file is succesfully moved. |
|
| 335 | + */ |
|
| 336 | + public function move($path, $override = false) |
|
| 337 | + { |
|
| 338 | + if (!$this->exists()) { |
|
| 339 | + return false; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + $file = new File($path); |
|
| 343 | + |
|
| 344 | + if (($file->exists() && !$override) || !rename($this->path, $file->getPath())) { |
|
| 345 | + return false; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + $this->path = $file->getPath(); |
|
| 349 | + |
|
| 350 | + return true; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Returns true if the file is succesfully renamed. |
|
| 355 | + * |
|
| 356 | + * @param string $file |
|
| 357 | + * @param bool $override = false |
|
| 358 | + * @return bool true if the file is succesfully renamed. |
|
| 359 | + */ |
|
| 360 | + public function rename($file, $override = false) |
|
| 361 | + { |
|
| 362 | + return $this->move($this->getDirectory() . static::DIRECTORY_SEPARATOR . basename($file), $override); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Returns true if the directory is succesfully removed. |
|
| 367 | + * |
|
| 368 | + * @param bool $recursive = false |
|
| 369 | + * @return bool true if the directory is succesfully removed. |
|
| 370 | + */ |
|
| 371 | + public function removeDirectory($recursive = false) |
|
| 372 | + { |
|
| 373 | + if (!$recursive) { |
|
| 374 | + return rmdir($this->path); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) { |
|
| 378 | + $path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname()); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + return true; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Returns true if the file is succesfully removed. |
|
| 386 | + * |
|
| 387 | + * @return bool true if the file is succesfully removed. |
|
| 388 | + */ |
|
| 389 | + public function removeFile() |
|
| 390 | + { |
|
| 391 | + if (!$this->isFile()) { |
|
| 392 | + return false; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + return unlink($this->path); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Returns the content of the file. |
|
| 400 | + * |
|
| 401 | + * @return string the content of the file. |
|
| 402 | + * @throws FileException on failure. |
|
| 403 | + */ |
|
| 404 | + public function read() |
|
| 405 | + { |
|
| 406 | + $result = file_get_contents($this->path); |
|
| 407 | + |
|
| 408 | + if ($result === false) { |
|
| 409 | + throw new FileException('Can\'t read the content.'); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + return $result; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * Append the given content. |
|
| 417 | + * |
|
| 418 | + * @param string $content |
|
| 419 | + * @return null |
|
| 420 | + * @throws FileException on failure. |
|
| 421 | + */ |
|
| 422 | + public function append($content) |
|
| 423 | + { |
|
| 424 | + if (file_put_contents($this->path, $content, \FILE_APPEND) === false) { |
|
| 425 | + throw new FileException('Can\'t append the given content.'); |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Write the given content. |
|
| 431 | + * |
|
| 432 | + * @param string $content |
|
| 433 | + * @return null |
|
| 434 | + * @throws FileException on failure. |
|
| 435 | + */ |
|
| 436 | + public function write($content) |
|
| 437 | + { |
|
| 438 | + if (file_put_contents($this->path, $content) === false) { |
|
| 439 | + throw new FileException('Can\'t write the given content.'); |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | 442 | } |