You are here

protected function TestSiteTearDownCommand::fileUnmanagedDeleteRecursive in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php \Drupal\TestSite\Commands\TestSiteTearDownCommand::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 TestSiteTearDownCommand::fileUnmanagedDeleteRecursive()
TestSiteTearDownCommand::tearDown in core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php
Removes a given instance by deleting all the database tables and files.

File

core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php, line 115

Class

TestSiteTearDownCommand
Command to tear down a test Drupal site.

Namespace

Drupal\TestSite\Commands

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);
}