LocalDirectoryDeletor.php 849 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace Gaufrette\Functional;
  3. class LocalDirectoryDeletor
  4. {
  5. public static function deleteDirectory($directory)
  6. {
  7. if ($directory === '/') {
  8. throw new \InvalidArgumentException('Deleting "/" is disallowed.');
  9. }
  10. if (file_exists($directory)) {
  11. $iterator = new \RecursiveIteratorIterator(
  12. new \RecursiveDirectoryIterator(
  13. $directory,
  14. \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
  15. ),
  16. \RecursiveIteratorIterator::CHILD_FIRST
  17. );
  18. foreach ($iterator as $item) {
  19. if ($item->isDir()) {
  20. rmdir(strval($item));
  21. } else {
  22. unlink(strval($item));
  23. }
  24. }
  25. }
  26. }
  27. }