123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- declare(strict_types=1);
- namespace Doctrine\Migrations\Finder;
- use Doctrine\Migrations\Finder\Exception\InvalidDirectory;
- use Doctrine\Migrations\Finder\Exception\NameIsReserved;
- use ReflectionClass;
- use function assert;
- use function get_declared_classes;
- use function in_array;
- use function is_dir;
- use function ksort;
- use function realpath;
- use function strlen;
- use function strncmp;
- use const SORT_STRING;
- /**
- * The Finder class is responsible for for finding migrations on disk at a given path.
- */
- abstract class Finder implements MigrationFinder
- {
- protected static function requireOnce(string $path): void
- {
- require_once $path;
- }
- /**
- * @throws InvalidDirectory
- */
- protected function getRealPath(string $directory): string
- {
- $dir = realpath($directory);
- if ($dir === false || ! is_dir($dir)) {
- throw InvalidDirectory::new($directory);
- }
- return $dir;
- }
- /**
- * @param string[] $files
- *
- * @return string[]
- *
- * @throws NameIsReserved
- */
- protected function loadMigrations(array $files, ?string $namespace): array
- {
- $includedFiles = [];
- foreach ($files as $file) {
- static::requireOnce($file);
- $realFile = realpath($file);
- assert($realFile !== false);
- $includedFiles[] = $realFile;
- }
- $classes = $this->loadMigrationClasses($includedFiles, $namespace);
- $versions = [];
- foreach ($classes as $class) {
- $versions[] = $class->getName();
- }
- ksort($versions, SORT_STRING);
- return $versions;
- }
- /**
- * Look up all declared classes and find those classes contained
- * in the given `$files` array.
- *
- * @param string[] $files The set of files that were `required`
- * @param string|null $namespace If not null only classes in this namespace will be returned
- *
- * @return ReflectionClass<object>[] the classes in `$files`
- */
- protected function loadMigrationClasses(array $files, ?string $namespace = null): array
- {
- $classes = [];
- foreach (get_declared_classes() as $class) {
- $reflectionClass = new ReflectionClass($class);
- if (! in_array($reflectionClass->getFileName(), $files, true)) {
- continue;
- }
- if ($namespace !== null && ! $this->isReflectionClassInNamespace($reflectionClass, $namespace)) {
- continue;
- }
- $classes[] = $reflectionClass;
- }
- return $classes;
- }
- /**
- * @param ReflectionClass<object> $reflectionClass
- */
- private function isReflectionClassInNamespace(ReflectionClass $reflectionClass, string $namespace): bool
- {
- return strncmp($reflectionClass->getName(), $namespace . '\\', strlen($namespace) + 1) === 0;
- }
- }
|