【问题标题】:Exception when directory does not exist目录不存在时的异常
【发布时间】:2022-11-15 20:24:22
【问题描述】:

问题是,如果通过 Finder 类观看多个目录。 如果不存在,Finder 将抛出异常,并且修复程序将死亡。

$finder = Finder::create()
    ->in([
        __DIR__ . '/web/app/mu-plugins/ys-*',
        __DIR__ . '/web/app/plugins/ys-*'
    ])
    ->name('*.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

是否有可能添加一个选项以“如果目录不存在则跳过”以便如果其中一个目录不存在它不会杀死修复程序?

【问题讨论】:

  • 没有这样做的选项,您需要检查(例如使用 array_filter)目录是否存在

标签: symfony phpcs php-cs-fixer symfony-finder


【解决方案1】:

我会事先使用带有简单glob() 函数的 array_filter 来检查目录/文件是否确实存在。如果找到任何文件/文件夹,Glob 将返回一个数组。

如果目录/文件确实存在,请将它们传递给 Finder:

$directories = [
    __DIR__ . '/web/app/mu-plugins/ys-*',
    __DIR__ . '/web/app/plugins/ys-*'
];

$checkDirs = array_filter($directories, static function ($dir){
    return !empty(glob($dir));
});

$finder = Finder::create()
->in($checkDirs)
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多