This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
<?php
namespace Doctrine\DBAL\Migrations\Finder;
/**
* Abstract base class for MigrationFinders
*
* @since 1.0.0-alpha3
*/
abstract class AbstractFinder implements MigrationFinderInterface
{
protected static function requireOnce($path)
{
require_once $path;
}
protected function getRealPath($directory)
{
$dir = realpath($directory);
if (false === $dir || ! is_dir($dir)) {
throw new \InvalidArgumentException(sprintf(
'Cannot load migrations from "%s" because it is not a valid directory',
$directory
));
}
return $dir;
}
/**
* Load the migrations and return an array of thoses loaded migrations
* @param array $files array of migration filename found
* @param string $namespace namespace of thoses migrations
* @return array constructed with the migration name as key and the value is the fully qualified name of the migration
*/
protected function loadMigrations($files, $namespace)
{
$migrations = [];
uasort($files, $this->getFileSortCallback());
foreach ($files as $file) {
static::requireOnce($file);
$className = basename($file, '.php');
$version = (string) substr($className, 7);
if ($version === '0') {
throw new \InvalidArgumentException(sprintf(
'Cannot load a migrations with the name "%s" because it is a reserved number by doctrine migrations' . PHP_EOL .
'It\'s used to revert all migrations including the first one.',
$version
));
}
$migrations[$version] = sprintf('%s\\%s', $namespace, $className);
}
return $migrations;
}
/**
* Return callable for files basename uasort
*
* @return callable
*/
protected function getFileSortCallback()
{
return function ($a, $b) {
return (basename($a) < basename($b)) ? -1 : 1;
};
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Doctrine\DBAL\Migrations\Finder;
/**
* A MigrationFinderInterface implementation that uses `glob` and some special file and
* class names to load migrations from a directory.
*
* The migrations are expected to reside in files with the filename
* `VersionYYYYMMDDHHMMSS.php`. Each file should contain one class named
* `VersionYYYYMMDDHHMMSS`.
*
* @since 1.0.0-alpha3
*/
final class GlobFinder extends AbstractFinder
{
/**
* {@inheritdoc}
*/
public function findMigrations($directory, $namespace = null)
{
$dir = $this->getRealPath($directory);
$files = glob(rtrim($dir, '/') . '/Version*.php');
return $this->loadMigrations($files, $namespace);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Doctrine\DBAL\Migrations\Finder;
/**
* A MigrationDeepFinderInterface is a MigrationFinderInterface, which locates
* migrations not only in a directory itself, but in subdirectories of this directory,
* too.
*/
interface MigrationDeepFinderInterface extends MigrationFinderInterface
{
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Doctrine\DBAL\Migrations\Finder;
/**
* MigrationFinderInterface implementations locate migrations (classes that extend
* `Doctrine\DBAL\Migrations\AbstractMigration`) in a directory.
*
* @since 1.0.0-alpha3
*/
interface MigrationFinderInterface
{
/**
* Find all the migrations in a directory for the given path and namespace.
*
* @param string $directory The directory in which to look for migrations
* @param string|null $namespace The namespace of the classes to load
* @throws \InvalidArgumentException if the directory does not exist
* @return string[] An array of class names that were found with the version
* as keys.
*/
public function findMigrations($directory, $namespace = null);
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Doctrine\DBAL\Migrations\Finder;
/**
* A MigrationFinderInterface implementation that uses a RegexIterator along with a
* RecursiveDirectoryIterator.
*
* @since 1.0.0-alpha3
*/
final class RecursiveRegexFinder extends AbstractFinder implements MigrationDeepFinderInterface
{
/**
* {@inheritdoc}
*/
public function findMigrations($directory, $namespace = null)
{
$dir = $this->getRealPath($directory);
return $this->loadMigrations($this->getMatches($this->createIterator($dir)), $namespace);
}
/**
* Create a recursive iterator to find all the migrations in the subdirectories.
* @param string $dir
* @return \RegexIterator
*/
private function createIterator($dir)
{
return new \RegexIterator(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
\RecursiveIteratorIterator::LEAVES_ONLY
),
$this->getPattern(),
\RegexIterator::GET_MATCH
);
}
private function getPattern()
{
return sprintf('#^.+\\%sVersion[^\\%s]{1,255}\\.php$#i', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
}
/**
* Transform the recursiveIterator result array of array into the expected array of migration file
* @param iterable $iteratorFilesMatch
* @return array
*/
private function getMatches($iteratorFilesMatch)
{
$files = [];
foreach ($iteratorFilesMatch as $file) {
$files[] = $file[0];
}
return $files;
}
}