Exemple #1
0
 public function testMapFileNameToClassName()
 {
     $expectedResults = array('20150902094024_create_user_table.php' => 'CreateUserTable', '20150902102548_my_first_migration2.php' => 'MyFirstMigration2');
     foreach ($expectedResults as $input => $expectedResult) {
         $this->assertEquals($expectedResult, Util::mapFileNameToClassName($input));
     }
 }
Exemple #2
0
 /**
  * Gets an array of the database migrations.
  *
  * @throws \InvalidArgumentException
  * @return AbstractMigration[]
  */
 public function getMigrations()
 {
     if (null === $this->migrations) {
         $config = $this->getConfig();
         $phpFiles = glob($config->getMigrationPath() . DIRECTORY_SEPARATOR . '*.php', defined('GLOB_BRACE') ? GLOB_BRACE : 0);
         // filter the files to only get the ones that match our naming scheme
         $fileNames = array();
         /** @var AbstractMigration[] $versions */
         $versions = array();
         foreach ($phpFiles as $filePath) {
             if (Util::isValidMigrationFileName(basename($filePath))) {
                 $version = Util::getVersionFromFileName(basename($filePath));
                 if (isset($versions[$version])) {
                     throw new \InvalidArgumentException(sprintf('Duplicate migration - "%s" has the same version as "%s"', $filePath, $versions[$version]->getVersion()));
                 }
                 // convert the filename to a class name
                 $class = Util::mapFileNameToClassName(basename($filePath));
                 if (isset($fileNames[$class])) {
                     throw new \InvalidArgumentException(sprintf('Migration "%s" has the same name as "%s"', basename($filePath), $fileNames[$class]));
                 }
                 $fileNames[$class] = basename($filePath);
                 // load the migration file
                 /** @noinspection PhpIncludeInspection */
                 require_once $filePath;
                 if (!class_exists($class)) {
                     throw new \InvalidArgumentException(sprintf('Could not find class "%s" in file "%s"', $class, $filePath));
                 }
                 // instantiate it
                 $migration = new $class($version, $this->getInput(), $this->getOutput());
                 if (!$migration instanceof AbstractMigration) {
                     throw new \InvalidArgumentException(sprintf('The class "%s" in file "%s" must extend \\Phinx\\Migration\\AbstractMigration', $class, $filePath));
                 }
                 $versions[$version] = $migration;
             }
         }
         ksort($versions);
         $this->setMigrations($versions);
     }
     return $this->migrations;
 }