简体   繁体   中英

Symfony Finder: Finding all directories except ones that start with an underscore

I want to find all directories (at root level, not recursive) that do not start with an underscore.

getcwd() = /Users/example/project

Directory example:

/Users/example/project/_pictures
/Users/example/project/_graphics
/Users/example/project/test

I've tried with the following code snippet:

$directories = $this->container->get('finder')
    ->directories()
    ->in(getcwd())
    ->notName('_*')
    ->depth(0);

But this seems to return:

/Users/example/project/_pictures/home
/Users/example/project/test

So it returns "test", which is correct, but it also returns a sub-folder inside one that starts with an underscore, which is incorrect. Any ideas what's gone wrong here?

This works:

$directories = glob(getcwd() . '/[!^_]*', GLOB_ONLYDIR);

But hoped for a solution using the Symfony finder.

I found in Finder::searchInDirectory :

if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
    $this->notPaths[] = '#(^|/)\..+(/|$)#';
}

Modify with this and it work :)

$finder->notPath('#(^|/)_.+(/|$)#')

Update

This's my code:

$finder = Finder::create()
    ->files()
    ->name('*.twig')
    ->notPath('#(^|/)_.+(/|$)#') // Ignore path start with underscore (_).
    ->ignoreVCS(true)
    ->ignoreDotFiles(true)
    ->ignoreUnreadableDirs()
    ->in(__DIR__);

foreach ($finder as $file) {
    var_dump($file->getRelativePath());
}

尝试使用$ finder-> exclude($ dirs);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM