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\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.         $sortedClasses get_declared_classes();
  50.         sort($sortedClasses);
  51.         $sortedInterfaces get_declared_interfaces();
  52.         sort($sortedInterfaces);
  53.         $declared array_merge($sortedClasses$sortedInterfaces);
  54.         foreach ($declared as $className) {
  55.             $reflectionClass = new \ReflectionClass($className);
  56.             $sourceFile $reflectionClass->getFileName();
  57.             if (isset($includedFiles[$sourceFile])) {
  58.                 yield $className => $reflectionClass;
  59.             }
  60.         }
  61.     }
  62. }
  63. class_alias(ReflectionClassRecursiveIterator::class, \ApiPlatform\Core\Util\ReflectionClassRecursiveIterator::class);