vendor/doctrine/migrations/src/Finder/Finder.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Finder;
  4. use Doctrine\Migrations\Finder\Exception\InvalidDirectory;
  5. use Doctrine\Migrations\Finder\Exception\NameIsReserved;
  6. use ReflectionClass;
  7. use function assert;
  8. use function get_declared_classes;
  9. use function in_array;
  10. use function is_dir;
  11. use function realpath;
  12. use function strlen;
  13. use function strncmp;
  14. /**
  15.  * The Finder class is responsible for for finding migrations on disk at a given path.
  16.  */
  17. abstract class Finder implements MigrationFinder
  18. {
  19.     protected static function requireOnce(string $path): void
  20.     {
  21.         require_once $path;
  22.     }
  23.     /** @throws InvalidDirectory */
  24.     protected function getRealPath(string $directory): string
  25.     {
  26.         $dir realpath($directory);
  27.         if ($dir === false || ! is_dir($dir)) {
  28.             throw InvalidDirectory::new($directory);
  29.         }
  30.         return $dir;
  31.     }
  32.     /**
  33.      * @param string[] $files
  34.      *
  35.      * @return string[]
  36.      *
  37.      * @throws NameIsReserved
  38.      */
  39.     protected function loadMigrations(array $filesstring|null $namespace): array
  40.     {
  41.         $includedFiles = [];
  42.         foreach ($files as $file) {
  43.             static::requireOnce($file);
  44.             $realFile realpath($file);
  45.             assert($realFile !== false);
  46.             $includedFiles[] = $realFile;
  47.         }
  48.         $classes  $this->loadMigrationClasses($includedFiles$namespace);
  49.         $versions = [];
  50.         foreach ($classes as $class) {
  51.             $versions[] = $class->getName();
  52.         }
  53.         return $versions;
  54.     }
  55.     /**
  56.      * Look up all declared classes and find those classes contained
  57.      * in the given `$files` array.
  58.      *
  59.      * @param string[]    $files     The set of files that were `required`
  60.      * @param string|null $namespace If not null only classes in this namespace will be returned
  61.      *
  62.      * @return ReflectionClass<object>[] the classes in `$files`
  63.      */
  64.     protected function loadMigrationClasses(array $filesstring|null $namespace null): array
  65.     {
  66.         $classes = [];
  67.         foreach (get_declared_classes() as $class) {
  68.             $reflectionClass = new ReflectionClass($class);
  69.             if (! in_array($reflectionClass->getFileName(), $filestrue)) {
  70.                 continue;
  71.             }
  72.             if ($namespace !== null && ! $this->isReflectionClassInNamespace($reflectionClass$namespace)) {
  73.                 continue;
  74.             }
  75.             $classes[] = $reflectionClass;
  76.         }
  77.         return $classes;
  78.     }
  79.     /** @param ReflectionClass<object> $reflectionClass */
  80.     private function isReflectionClassInNamespace(ReflectionClass $reflectionClassstring $namespace): bool
  81.     {
  82.         return strncmp($reflectionClass->getName(), $namespace '\\'strlen($namespace) + 1) === 0;
  83.     }
  84. }