Skip to content

Commit 7205437

Browse files
committed
Removed 'V' hack
1 parent ac656e5 commit 7205437

File tree

6 files changed

+87
-28
lines changed

6 files changed

+87
-28
lines changed

lib/Migrator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public function migrate($to = null, OutputInterface $output)
6464
{
6565
if ($to === null) {
6666
$to = $this->versionCollection->getLatestVersion();
67-
} else {
68-
$to = 'V' . $to;
6967
}
7068

7169
if (false === $to) {
@@ -109,6 +107,6 @@ public function migrate($to = null, OutputInterface $output)
109107

110108
public function getVersions()
111109
{
112-
return $this->versionCollection;
110+
return $this->versionCollection->getAllVersions();
113111
}
114112
}

lib/Util.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/*
3+
* This file is part of the <package> package.
4+
*
5+
* (c) 2011-2015 Daniel Leech
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace DTL\PhpcrMigrations;
12+
13+
class Util
14+
{
15+
/**
16+
* Return the class name from a file
17+
*
18+
* Taken from http://stackoverflow.com/questions/7153000/get-class-name-from-file
19+
*
20+
* @param string $file
21+
*
22+
* @return string
23+
*/
24+
public static function getClassNameFromFile($file)
25+
{
26+
$fp = fopen($file, 'r');
27+
28+
$class = $namespace = $buffer = '';
29+
$i = 0;
30+
31+
while (!$class) {
32+
if (feof($fp)) break;
33+
34+
$buffer .= fread($fp, 512);
35+
$tokens = token_get_all($buffer);
36+
37+
if (strpos($buffer, '{') === false) continue;
38+
39+
for (;$i<count($tokens);$i++) {
40+
if ($tokens[$i][0] === T_NAMESPACE) {
41+
for ($j=$i+1;$j<count($tokens); $j++) {
42+
if ($tokens[$j][0] === T_STRING) {
43+
$namespace .= '\\'.$tokens[$j][1];
44+
} else if ($tokens[$j] === '{' || $tokens[$j] === ';') {
45+
break;
46+
}
47+
}
48+
}
49+
50+
if ($tokens[$i][0] === T_CLASS) {
51+
for ($j=$i+1;$j<count($tokens);$j++) {
52+
if ($tokens[$j] === '{') {
53+
$class = $tokens[$i+2][1];
54+
}
55+
}
56+
}
57+
}
58+
};
59+
60+
if (!$class) {
61+
return null;
62+
}
63+
64+
return $namespace . '\\' . $class;
65+
}
66+
}

lib/VersionCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ public function getLatestVersion()
8585

8686
private function normalizeTs($ts)
8787
{
88-
return $ts ? 'V' . $ts : null;
88+
return $ts ? $ts : null;
8989
}
9090
}

lib/VersionFinder.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
namespace DTL\PhpcrMigrations;
1212

1313
use Symfony\Component\Finder\Finder;
14+
use DTL\PhpcrMigrations\Util;
1415

1516
class VersionFinder
1617
{
1718
private $paths;
19+
private $collection;
1820

1921
public function __construct(array $paths)
2022
{
@@ -23,6 +25,10 @@ public function __construct(array $paths)
2325

2426
public function getCollection()
2527
{
28+
if ($this->collection) {
29+
return $this->collection;
30+
}
31+
2632
$versions = array();
2733
$finder = new Finder();
2834
$finder->name('/Version[0-9]{12}.php/');
@@ -34,19 +40,8 @@ public function getCollection()
3440

3541
foreach ($finder as $versionFile) {
3642
$className = $versionFile->getBasename('.php');
37-
$declaredClasses = get_declared_classes();
3843
require_once($versionFile->getRealPath());
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());
47-
}
48-
49-
$classFqn = reset($newClasses);
44+
$classFqn = Util::getClassNameFromFile($versionFile->getRealPath());
5045

5146
$version = new $classFqn();
5247

@@ -55,11 +50,11 @@ public function getCollection()
5550
}
5651

5752
$versionTimestamp = substr($versionFile->getBaseName(), 7, 12);
58-
$versions['V'.$versionTimestamp] = $version;
53+
$versions[$versionTimestamp] = $version;
5954
}
6055

61-
$collection = new VersionCollection($versions);
56+
$this->collection = new VersionCollection($versions);
6257

63-
return $collection;
58+
return $this->collection;
6459
}
6560
}

lib/VersionStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class VersionStorage
1818
private $storageNodeName;
1919
private $initialized = false;
2020

21-
public function __construct(SessionInterface $session, $storageNodeName = 'jcr:versions')
21+
public function __construct(SessionInterface $session, $storageNodeName = 'phpcrmig:versions')
2222
{
2323
$this->session = $session;
2424
$this->storageNodeName = $storageNodeName;
@@ -33,7 +33,7 @@ public function init()
3333
$this->workspace = $this->session->getWorkspace();
3434
$nodeTypeManager = $this->workspace->getNodeTypeManager();
3535

36-
if (!$nodeTypeManager->hasNodeType('phpcrMigration:version')) {
36+
if (!$nodeTypeManager->hasNodeType('phpcrmig:version')) {
3737
$nodeTypeManager->registerNodeTypesCnd(<<<EOT
3838
<phpcrmig = 'http://www.danteech.com/phpcr-migrations'>
3939
[phpcrmig:version] > nt:base, mix:created

tests/Functional/MigrationTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public function testMigration()
5050
$this->assertTrue($this->session->nodeExists('/hello/world'));
5151
$this->assertTrue($this->session->nodeExists('/hello/dan'));
5252

53-
$nodes = $this->session->getNode('/phpcrMigrations:versions')->getNodes();
53+
$nodes = $this->session->getNode('/phpcrmig:versions')->getNodes();
5454
$names = array_keys((array) $nodes);
5555

56-
$this->assertContains('V201501011200', $names);
57-
$this->assertContains('V201501011212', $names);
58-
$this->assertContains('V201501011215', $names);
56+
$this->assertContains('201501011200', $names);
57+
$this->assertContains('201501011212', $names);
58+
$this->assertContains('201501011215', $names);
5959
}
6060

6161
/**
@@ -106,10 +106,10 @@ public function testMigrateDown()
106106
// migrate down one version
107107
$migratedVersions = $this->getMigrator()->migrate(self::VERSION2, $this->output);
108108
$this->assertCount(1, $migratedVersions);
109-
$nodes = $this->session->getNode('/phpcrMigrations:versions')->getNodes();
109+
$nodes = $this->session->getNode('/phpcrmig:versions')->getNodes();
110110
$names = array_keys((array) $nodes);
111111
$this->assertCount(2, $names);
112-
$this->assertNotContains('V' . self::VERSION3, $names);
112+
$this->assertNotContains(self::VERSION3, $names);
113113

114114
$this->assertFalse($this->session->nodeExists('/hello/dan'));
115115

@@ -145,7 +145,7 @@ public function testInitialize()
145145
$this->addVersion(self::VERSION2);
146146
$this->getMigrator()->initialize();
147147

148-
$nodes = $this->session->getNode('/phpcrMigrations:versions')->getNodes();
148+
$nodes = $this->session->getNode('/phpcrmig:versions')->getNodes();
149149

150150
$this->assertCount(2, $nodes);
151151
}

0 commit comments

Comments
 (0)