|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of fast-forward/dev-tools. |
| 7 | + * |
| 8 | + * This source file is subject to the license bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | + * @license https://opensource.org/licenses/MIT MIT License |
| 13 | + * |
| 14 | + * @see https://github.com/php-fast-forward/dev-tools |
| 15 | + * @see https://github.com/php-fast-forward |
| 16 | + * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | + */ |
| 18 | + |
| 19 | +namespace FastForward\DevTools\GitAttributes; |
| 20 | + |
| 21 | +use Symfony\Component\Filesystem\Filesystem; |
| 22 | + |
| 23 | +/** |
| 24 | + * Checks the existence of files and directories in a given base path. |
| 25 | + * |
| 26 | + * This class determines which candidate paths from the canonical list |
| 27 | + * actually exist in the target repository, enabling selective export-ignore rules. |
| 28 | + */ |
| 29 | +final readonly class ExistenceChecker implements ExistenceCheckerInterface |
| 30 | +{ |
| 31 | + private string $basePath; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param string $basePath The base directory to check paths against |
| 35 | + * @param Filesystem $filesystem |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + string $basePath, |
| 39 | + private Filesystem $filesystem = new Filesystem() |
| 40 | + ) { |
| 41 | + $this->basePath = rtrim($basePath, '/'); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Checks if a path exists as a file or directory. |
| 46 | + * |
| 47 | + * @param string $path The path to check (e.g., "/.github/" or "/.editorconfig") |
| 48 | + * |
| 49 | + * @return bool True if the path exists as a file or directory |
| 50 | + */ |
| 51 | + public function exists(string $path): bool |
| 52 | + { |
| 53 | + $fullPath = $this->basePath . $path; |
| 54 | + |
| 55 | + return $this->filesystem->exists($fullPath); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Filters a list of paths to only those that exist. |
| 60 | + * |
| 61 | + * @param list<string> $paths The paths to filter |
| 62 | + * |
| 63 | + * @return list<string> Only the paths that exist |
| 64 | + */ |
| 65 | + public function filterExisting(array $paths): array |
| 66 | + { |
| 67 | + return array_values(array_filter($paths, $this->exists(...))); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Checks if a path is a directory. |
| 72 | + * |
| 73 | + * @param string $path The path to check (e.g., "/.github/") |
| 74 | + * |
| 75 | + * @return bool True if the path exists and is a directory |
| 76 | + */ |
| 77 | + public function isDirectory(string $path): bool |
| 78 | + { |
| 79 | + $fullPath = $this->basePath . $path; |
| 80 | + |
| 81 | + return is_dir($fullPath); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Checks if a path is a file. |
| 86 | + * |
| 87 | + * @param string $path The path to check (e.g., "/.editorconfig") |
| 88 | + * |
| 89 | + * @return bool True if the path exists and is a file |
| 90 | + */ |
| 91 | + public function isFile(string $path): bool |
| 92 | + { |
| 93 | + $fullPath = $this->basePath . $path; |
| 94 | + |
| 95 | + return is_file($fullPath); |
| 96 | + } |
| 97 | +} |
0 commit comments