|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ock\ClassFilesIterator\ClassFilesIA; |
| 4 | + |
| 5 | +use Ock\ClassFilesIterator\NamespaceDirectory; |
| 6 | +use Ock\ClassFilesIterator\NsDirUtil; |
| 7 | + |
| 8 | +class ClassFilesIA_NamespaceDirectoryPsr4 implements ClassFilesIAInterface { |
| 9 | + |
| 10 | + /** |
| 11 | + * See http://php.net/manual/en/language.oop5.basic.php |
| 12 | + */ |
| 13 | + const CLASS_NAME_REGEX = /** @lang RegExp */ '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'; |
| 14 | + |
| 15 | + /** |
| 16 | + * @param string $dir |
| 17 | + * @param string $namespace |
| 18 | + * |
| 19 | + * @return self |
| 20 | + */ |
| 21 | + public static function create(string $dir, string $namespace): self { |
| 22 | + return new self( |
| 23 | + $dir, |
| 24 | + NsDirUtil::terminateNamespace($namespace), |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @param class-string $class |
| 30 | + * @param int $nLevelsUp |
| 31 | + * |
| 32 | + * @return \Ock\ClassFilesIterator\ClassFilesIA\ClassFilesIAInterface |
| 33 | + * @throws \ReflectionException |
| 34 | + */ |
| 35 | + public static function createFromClass(string $class, int $nLevelsUp = 0): ClassFilesIAInterface { |
| 36 | + $nsDir = NamespaceDirectory::createFromClass($class) |
| 37 | + ->requireParentN($nLevelsUp); |
| 38 | + return self::createFromNsdirObject($nsDir); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param \Ock\ClassFilesIterator\NamespaceDirectory $nsdir |
| 43 | + * |
| 44 | + * @return \Ock\ClassFilesIterator\ClassFilesIA\ClassFilesIAInterface |
| 45 | + */ |
| 46 | + public static function createFromNsdirObject(NamespaceDirectory $nsdir): ClassFilesIAInterface { |
| 47 | + if (!is_dir($nsdir->getDirectory())) { |
| 48 | + return new ClassFilesIA_Empty(); |
| 49 | + } |
| 50 | + return new self( |
| 51 | + $nsdir->getDirectory(), |
| 52 | + $nsdir->getTerminatedNamespace(), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param string $directory |
| 58 | + * @param string $terminatedNamespace |
| 59 | + */ |
| 60 | + public function __construct( |
| 61 | + private string $directory, |
| 62 | + private readonly string $terminatedNamespace, |
| 63 | + ) {} |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritdoc} |
| 67 | + */ |
| 68 | + public function withRealpathRoot(): static { |
| 69 | + $clone = clone $this; |
| 70 | + $realpath = realpath($this->directory); |
| 71 | + // @todo Properly handle this case. |
| 72 | + assert($realpath !== false, "Cannot get realpath for '$this->directory'. Perhaps the directory does not exist."); |
| 73 | + $clone->directory = $realpath; |
| 74 | + return $clone; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritdoc} |
| 79 | + */ |
| 80 | + public function getIterator(): \Iterator { |
| 81 | + return self::scan($this->directory, $this->terminatedNamespace); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param string $dir |
| 86 | + * @param string $terminatedNamespace |
| 87 | + * |
| 88 | + * @return \Iterator<string, class-string> |
| 89 | + * Format: $[$file] = $class |
| 90 | + */ |
| 91 | + private static function scan(string $dir, string $terminatedNamespace): \Iterator { |
| 92 | + $candidates = \scandir($dir, \SCANDIR_SORT_ASCENDING); |
| 93 | + if ($candidates === false) { |
| 94 | + throw new \RuntimeException("Failed to scandir('$dir')."); |
| 95 | + } |
| 96 | + foreach ($candidates as $candidate) { |
| 97 | + if ('.' === $candidate[0]) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + $path = $dir . '/' . $candidate; |
| 101 | + if (str_ends_with($candidate, '.php')) { |
| 102 | + if (!is_file($path)) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + $name = substr($candidate, 0, -4); |
| 106 | + if (!preg_match(self::CLASS_NAME_REGEX, $name)) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + // The value is a class-string, but PhpStan does not know. |
| 110 | + // @phpstan-ignore generator.valueType |
| 111 | + yield $path => $terminatedNamespace . $name; |
| 112 | + } |
| 113 | + else { |
| 114 | + if (!is_dir($path)) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + if (!preg_match(self::CLASS_NAME_REGEX, $candidate)) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + // @todo Make PHP 7 version with "yield from". |
| 121 | + yield from self::scan($path, $terminatedNamespace . $candidate . '\\'); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments