vendor/api-platform/core/src/Util/ReflectionClassRecursiveIterator.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Util;
  12. /**
  13.  * Gets reflection classes for php files in the given directories.
  14.  *
  15.  * @author Antoine Bluchet <soyuka@gmail.com>
  16.  *
  17.  * @internal
  18.  */
  19. final class ReflectionClassRecursiveIterator
  20. {
  21.     private function __construct()
  22.     {
  23.     }
  24.     public static function getReflectionClassesFromDirectories(array $directories): \Iterator
  25.     {
  26.         foreach ($directories as $path) {
  27.             $iterator = new \RegexIterator(
  28.                 new \RecursiveIteratorIterator(
  29.                     new \RecursiveDirectoryIterator($path\FilesystemIterator::SKIP_DOTS),
  30.                     \RecursiveIteratorIterator::LEAVES_ONLY
  31.                 ),
  32.                 '/^.+\.php$/i',
  33.                 \RecursiveRegexIterator::GET_MATCH
  34.             );
  35.             foreach ($iterator as $file) {
  36.                 $sourceFile $file[0];
  37.                 if (!preg_match('(^phar:)i'$sourceFile)) {
  38.                     $sourceFile realpath($sourceFile);
  39.                 }
  40.                 try {
  41.                     require_once $sourceFile;
  42.                 } catch (\Throwable $t) {
  43.                     // invalid PHP file (example: missing parent class)
  44.                     continue;
  45.                 }
  46.                 $includedFiles[$sourceFile] = true;
  47.             }
  48.         }
  49.         $declared array_merge(get_declared_classes(), get_declared_interfaces());
  50.         foreach ($declared as $className) {
  51.             $reflectionClass = new \ReflectionClass($className);
  52.             $sourceFile $reflectionClass->getFileName();
  53.             if (isset($includedFiles[$sourceFile])) {
  54.                 yield $className => $reflectionClass;
  55.             }
  56.         }
  57.     }
  58. }