|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Stolt\LeanPackage; |
| 6 | + |
| 7 | +final class PhpConfigLoader |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Names searched in the current working directory, first match wins. |
| 11 | + * |
| 12 | + * @var string[] |
| 13 | + */ |
| 14 | + private const DEFAULT_FILENAMES = [ |
| 15 | + '.lpv.php', |
| 16 | + '.lpv.php.dist', |
| 17 | + ]; |
| 18 | + |
| 19 | + /** |
| 20 | + * Discover a configuration file in the current working directory. |
| 21 | + * |
| 22 | + * @return string|null |
| 23 | + */ |
| 24 | + public static function discover(): ?string |
| 25 | + { |
| 26 | + $cwd = \getcwd() ?: '.'; |
| 27 | + |
| 28 | + foreach (self::DEFAULT_FILENAMES as $file) { |
| 29 | + $path = $cwd . DIRECTORY_SEPARATOR . $file; |
| 30 | + |
| 31 | + if (\is_file($path) && \is_readable($path)) { |
| 32 | + return $path; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Load a configuration file and validate its structure. |
| 41 | + * |
| 42 | + * Allowed keys (subset relevant to validate command): |
| 43 | + * - directory: string |
| 44 | + * - preset: string |
| 45 | + * - glob-pattern: string |
| 46 | + * - glob-pattern-file: string |
| 47 | + * - stdin-input: bool |
| 48 | + * - diff: bool |
| 49 | + * - report-stale-export-ignores: bool |
| 50 | + * - enforce-strict-order: bool |
| 51 | + * - enforce-alignment: bool |
| 52 | + * - sort-from-directories-to-files: bool |
| 53 | + * - keep-license: bool |
| 54 | + * - keep-readme: bool |
| 55 | + * - keep-glob-pattern: string |
| 56 | + * - align-export-ignores: bool |
| 57 | + * |
| 58 | + * @param string $path |
| 59 | + * @return array<string, mixed> |
| 60 | + */ |
| 61 | + public static function load(string $path): array |
| 62 | + { |
| 63 | + /** @var mixed $config */ |
| 64 | + $config = (static function (string $__path) { |
| 65 | + /** @noinspection PhpIncludeInspection */ |
| 66 | + return require $__path; |
| 67 | + })($path); |
| 68 | + |
| 69 | + if (!\is_array($config)) { |
| 70 | + throw new \UnexpectedValueException('The configuration file must return an array.'); |
| 71 | + } |
| 72 | + |
| 73 | + $allowed = [ |
| 74 | + 'directory', |
| 75 | + 'preset', |
| 76 | + 'glob-pattern', |
| 77 | + 'glob-pattern-file', |
| 78 | + 'stdin-input', |
| 79 | + 'diff', |
| 80 | + 'report-stale-export-ignores', |
| 81 | + 'enforce-strict-order', |
| 82 | + 'enforce-alignment', |
| 83 | + 'sort-from-directories-to-files', |
| 84 | + 'keep-license', |
| 85 | + 'keep-readme', |
| 86 | + 'keep-glob-pattern', |
| 87 | + 'align-export-ignores', |
| 88 | + ]; |
| 89 | + |
| 90 | + $unknown = \array_diff(\array_keys($config), $allowed); |
| 91 | + if ($unknown !== []) { |
| 92 | + throw new \UnexpectedValueException('Unknown configuration keys: ' . \implode(', ', $unknown)); |
| 93 | + } |
| 94 | + |
| 95 | + // Basic type validation |
| 96 | + $stringKeys = ['directory', 'preset', 'glob-pattern', 'glob-pattern-file', 'keep-glob-pattern']; |
| 97 | + foreach ($stringKeys as $key) { |
| 98 | + if (isset($config[$key]) && !\is_string($config[$key])) { |
| 99 | + throw new \UnexpectedValueException(\sprintf('Configuration "%s" must be a string.', $key)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + $boolKeys = [ |
| 104 | + 'stdin-input', |
| 105 | + 'diff', |
| 106 | + 'report-stale-export-ignores', |
| 107 | + 'enforce-strict-order', |
| 108 | + 'enforce-alignment', |
| 109 | + 'sort-from-directories-to-files', |
| 110 | + 'keep-license', |
| 111 | + 'keep-readme', |
| 112 | + 'align-export-ignores', |
| 113 | + ]; |
| 114 | + foreach ($boolKeys as $key) { |
| 115 | + if (isset($config[$key]) && !\is_bool($config[$key])) { |
| 116 | + throw new \UnexpectedValueException(\sprintf('Configuration "%s" must be a boolean.', $key)); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return $config; |
| 121 | + } |
| 122 | +} |
0 commit comments