function PackageManagerFixtureCreator::removeAllExcept

Removes all files in a directory except the ones specified.

Parameters

string $directory: The directory path.

string[] $files_to_keep: The files to not delete.

1 call to PackageManagerFixtureCreator::removeAllExcept()
PackageManagerFixtureCreator::createFixture in core/scripts/PackageManagerFixtureCreator.php
Creates the fixture.

File

core/scripts/PackageManagerFixtureCreator.php, line 99

Class

PackageManagerFixtureCreator
Creates fixture at 'core/modules/package_manager/tests/fixtures/fake_site'.

Code

private static function removeAllExcept(string $directory, array $files_to_keep) : void {
    if (!is_dir($directory)) {
        throw new \LogicException("Expected directory {$directory}");
    }
    $paths_to_remove = glob("{$directory}/*");
    $fs = new Filesystem();
    foreach ($paths_to_remove as $path_to_remove) {
        $base_name = basename($path_to_remove);
        if (!in_array($base_name, $files_to_keep, TRUE)) {
            $fs->remove($path_to_remove);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.