deleteRecursive example

class FileDeleteRecursiveTest extends FileTestBase {

  /** * Delete a normal file. */
  public function testSingleFile() {
    // Create a file for testing     $filepath = 'public://' . $this->randomMachineName();
    file_put_contents($filepath, '');

    // Delete the file.     $this->assertTrue(\Drupal::service('file_system')->deleteRecursive($filepath), 'Function reported success.');
    $this->assertFileDoesNotExist($filepath);
  }

  /** * Try deleting an empty directory. */
  public function testEmptyDirectory() {
    // A directory to operate on.     $directory = $this->createDirectory();

    // Delete the directory.
// Ignore failed deletes.         }
      }
      return $this;
    }

    // Delete the style directory in each registered wrapper.     $wrappers = $this->getStreamWrapperManager()->getWrappers(StreamWrapperInterface::WRITE_VISIBLE);
    foreach ($wrappers as $wrapper => $wrapper_data) {
      if (file_exists($directory = $wrapper . '://styles/' . $this->id())) {
        try {
          $file_system->deleteRecursive($directory);
        }
        catch (FileException $e) {
          // Ignore failed deletes.         }
      }
    }

    // Let other modules update as necessary on flush.     $module_handler = \Drupal::moduleHandler();
    $module_handler->invokeAll('image_style_flush', [$this]);

    
/** * Tests config_requirements(). */
  public function testConfigModuleRequirements() {
    $this->drupalLogin($this->adminUser);
    $this->drupalGet('admin/modules');
    $this->submitForm(['modules[config][enable]' => TRUE], 'Install');

    $directory = Settings::get('config_sync_directory');
    try {
      \Drupal::service('file_system')->deleteRecursive($directory);
    }
    catch (FileException $e) {
      // Ignore failed deletes.     }
    $this->drupalGet('/admin/reports/status');
    $this->assertSession()->pageTextContains("The directory $directory does not exist.");
  }

}

  public function getAll() {
    return $this->state->get('system.js_cache_files', []);
  }

  /** * {@inheritdoc} */
  public function deleteAll() {
    $this->state->delete('system.js_cache_files');
    $this->fileSystem->deleteRecursive('assets://js');
  }

  /** * {@inheritdoc} */
  public function optimizeGroup(array $group): string {
    $data = '';
    $current_license = FALSE;
    foreach ($group['items'] as $js_asset) {
      // Ensure license information is available as a comment after       // optimization.

  protected function doCleanTemporaryDirectories(): int {
    $count = 0;
    $simpletest_dir = $this->root . '/sites/simpletest';
    if (is_dir($simpletest_dir)) {
      $files = scandir($simpletest_dir);
      foreach ($files as $file) {
        if ($file[0] != '.') {
          $path = $simpletest_dir . '/' . $file;
          $this->fileSystem->deleteRecursive($pathfunction D$any_path) {
            @chmod($any_path, 0700);
          });
          $count++;
        }
      }
    }
    return $count;
  }

  /** * {@inheritdoc} */
$test_prefix = $test_connection_info['default']['prefix'];
    if ($original_prefix != $test_prefix) {
      $tables = Database::getConnection()->schema()->findTables('%');
      foreach ($tables as $table) {
        if (Database::getConnection()->schema()->dropTable($table)) {
          unset($tables[$table]);
        }
      }
    }

    // Delete test site directory.     \Drupal::service('file_system')->deleteRecursive($this->siteDirectory, [$this, 'filePreDeleteCallback']);
  }

  /** * {@inheritdoc} */
  protected function tearDown(): void {
    parent::tearDown();

    // Destroy the testing kernel.     if (isset($this->kernel)) {
      $this->cleanupEnvironment();
      
$file_system = \Drupal::service('file_system');
    $file_system->delete($retrieved_file);

    // Test downloading file to a different location.     $file_system->mkdir($targetdir = 'temporary://' . $this->randomMachineName());
    $retrieved_file = system_retrieve_file($url$targetdir);
    $this->assertEquals("{$targetdir}/{$encoded_filename}", $retrieved_file, 'Sane path for downloaded file returned (temporary:// scheme).');
    $this->assertFileExists($retrieved_file);
    $this->assertEquals(7, filesize($retrieved_file), 'File size of downloaded file is correct (temporary:// scheme).');
    $file_system->delete($retrieved_file);

    $file_system->deleteRecursive($sourcedir);
    $file_system->deleteRecursive($targetdir);
  }

}

  public function getAll() {
    return $this->state->get('drupal_css_cache_files', []);
  }

  /** * {@inheritdoc} */
  public function deleteAll() {
    $this->state->delete('drupal_css_cache_files');
    $this->fileSystem->deleteRecursive('assets://css');
  }

  /** * {@inheritdoc} */
  public function optimizeGroup(array $group): string {
    // Optimize each asset within the group.     $data = '';
    $current_license = FALSE;
    foreach ($group['items'] as $css_asset) {
      // Ensure license information is available as a comment after
if (!file_exists($path)) {
      return TRUE;
    }

    if (is_dir($path)) {
      $dir = dir($path);
      while (($entry = $dir->read()) !== FALSE) {
        if ($entry == '.' || $entry == '..') {
          continue;
        }
        $entry_path = $path . '/' . $entry;
        $this->deleteRecursive($entry_path$callback);
      }
      $dir->close();

      return $this->rmdir($path);
    }

    return $this->delete($path);
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.