Метод ignoreVCS
Метод ignoreVCS класса Finder управляет тем,
будут ли при поиске файлов и директорий пропускаться
служебные файлы и папки систем контроля версий,
такие как . git, . svn, . hg и другие.
По умолчанию игнорирование включено. Первым параметром
передаётся булево значение: true включает игнорирование,
false отключает его. Метод возвращает текущий объект
Finder, что позволяет выстраивать цепочку вызовов.
Синтаксис
$finder->ignoreVCS(bool $ignoreVCS);
Пример
Давайте найдём все файлы в директории src,
явно включив игнорирование систем контроля версий:
<?php
namespace AppService;
use SymfonyComponentFinderFinder;
class FileService
{
public function findFiles(): array
{
$finder = new Finder();
$finder->files()->in('src')->ignoreVCS(true);
$res = [];
foreach ($finder as $file) {
$res[] = $file->getRelativePathname();
}
return $res;
}
}
?>
Результат выполнения кода:
["Service/FileService.php", "Controller/ArticleController.php"]
Пример
Давайте отключим игнорирование систем контроля версий, чтобы служебные файлы попали в результат поиска:
<?php
namespace AppService;
use SymfonyComponentFinderFinder;
class FileService
{
public function findFiles(): array
{
$finder = new Finder();
$finder->files()->in('project')->ignoreVCS(false);
$res = [];
foreach ($finder as $file) {
$res[] = $file->getRelativePathname();
}
return $res;
}
}
?>
Результат выполнения кода:
[".git/config", ".git/HEAD", "src/Controller/ArticleController.php"]
Пример
Давайте сравним вывод для директории, содержащей файлы системы контроля версий, при включённом и отключённом игнорировании:
<?php
namespace AppService;
use SymfonyComponentFinderFinder;
class FileService
{
public function compare(): array
{
$finderWith = new Finder();
$finderWith->files()->in('project')->ignoreVCS(true);
$withIgnore = [];
foreach ($finderWith as $file) {
$withIgnore[] = $file->getRelativePathname();
}
$finderWithout = new Finder();
$finderWithout->files()->in('project')->ignoreVCS(false);
$withoutIgnore = [];
foreach ($finderWithout as $file) {
$withoutIgnore[] = $file->getRelativePathname();
}
return [
'with' => $withIgnore,
'without' => $withoutIgnore,
];
}
}
?>
Результат выполнения кода:
[
"with" => ["src/Controller/ArticleController.php"],
"without" => [".git/config", ".git/HEAD", "src/Controller/ArticleController.php"]
]
Смотрите также
-
класс
Finder,
который выполняет поиск файлов и директорий -
метод
files,
который ограничивает поиск только файлами -
метод
ignoreDotFiles,
который управляет игнорированием скрытых файлов -
метод
in,
который задаёт директорию для поиска