Skip to content

Commit 41d84c1

Browse files
committed
Support migrate to 0
1 parent 768b94d commit 41d84c1

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
PHPCR Migrations
22
================
33

4-
Migrations library for PHPCR based heavily on [Doctrine
4+
Migrations library for PHPCR influenced by [Doctrine
55
migrations](https://github.com/doctrine/migrations).
66

7+
For integration with Symfony see the [PHPCR Migrations
8+
Bundle](https://github.com/dantleech/phpcr-migrations-bundle).
9+
710
Usage
811
-----
912

lib/Migrator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function migrate($to = null, OutputInterface $output)
6363

6464
$to = (string) $to;
6565

66-
if (!$this->versionCollection->has($to)) {
66+
if ($to !== 'V0' && !$this->versionCollection->has($to)) {
6767
throw MigratorException::unknownVersion($to);
6868
}
6969

@@ -93,8 +93,6 @@ public function migrate($to = null, OutputInterface $output)
9393
$this->session->save();
9494
}
9595

96-
97-
9896
return $versionsToExecute;
9997
}
10098
}

lib/MigratorException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,15 @@ public static function cannotInitializeAlreadyHasVersions()
3636
{
3737
return new self('Cannot initiaialize a content repository with previously existing migrations.');
3838
}
39+
40+
public static function noClassesInVersionFile($file)
41+
{
42+
return new self(sprintf('No classes found in version file "%s"', $file));
43+
}
44+
45+
public static function moreThanOneClassInVersionFile($file)
46+
{
47+
return new self(sprintf('More than one class found in version file "%s"', $file));
48+
}
49+
3950
}

lib/VersionFinder.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@ public function getCollection()
3434

3535
foreach ($finder as $versionFile) {
3636
$className = $versionFile->getBasename('.php');
37+
$declaredClasses = get_declared_classes();
3738
require_once($versionFile->getRealPath());
38-
if (!class_exists($className)) {
39-
throw MigratorException::couldNotIntantiateVersionClass($className);
39+
$newClasses = array_diff(get_declared_classes(), $declaredClasses);
40+
41+
if (count($newClasses) === 0) {
42+
throw MigratorException::noClassesInVersionFile($versionFile->getBaseName());
43+
}
44+
45+
if (count($newClasses) !== 1) {
46+
throw MigratorException::moreThanOneClassInVersionFile($versionFile->getBaseName());
4047
}
41-
$version = new $className();
48+
49+
$classFqn = reset($newClasses);
50+
51+
$version = new $classFqn();
4252

4353
if (!$version instanceof VersionInterface) {
4454
throw MigratorException::versionNotInstance($className);

0 commit comments

Comments
 (0)