You are here

protected function QuickStartTest::fileUnmanagedDeleteRecursive in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Command/QuickStartTest.php \Drupal\Tests\Core\Command\QuickStartTest::fileUnmanagedDeleteRecursive()

Deletes all files and directories in the specified path recursively.

Note this method has no dependencies on Drupal core to ensure that the test site can be torn down even if something in the test site is broken.

Parameters

string $path: A string containing either a URI or a file or directory path.

callable $callback: (optional) Callback function to run on each file prior to deleting it and on each directory prior to traversing it. For example, can be used to modify permissions.

Return value

bool TRUE for success or if path does not exist, FALSE in the event of an error.

See also

\Drupal\Core\File\FileSystemInterface::deleteRecursive()

1 call to QuickStartTest::fileUnmanagedDeleteRecursive()
QuickStartTest::tearDown in core/tests/Drupal/Tests/Core/Command/QuickStartTest.php

File

core/tests/Drupal/Tests/Core/Command/QuickStartTest.php, line 313

Class

QuickStartTest
Tests the quick-start commands.

Namespace

Drupal\Tests\Core\Command

Code

protected function fileUnmanagedDeleteRecursive($path, $callback = NULL) {
  if (isset($callback)) {
    call_user_func($callback, $path);
  }
  if (is_dir($path)) {
    $dir = dir($path);
    while (($entry = $dir
      ->read()) !== FALSE) {
      if ($entry == '.' || $entry == '..') {
        continue;
      }
      $entry_path = $path . '/' . $entry;
      $this
        ->fileUnmanagedDeleteRecursive($entry_path, $callback);
    }
    $dir
      ->close();
    return rmdir($path);
  }
  return unlink($path);
}