Skip to content

Commit 5dc1525

Browse files
committed
Support for actions
1 parent 91a450c commit 5dc1525

17 files changed

+273
-138
lines changed

.php_cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
$header = <<<EOF
4+
/*
5+
* This file is part of the PHPCR Migrations package
6+
*
7+
* (c) Daniel Leech <daniel@dantleech.com>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
EOF;
13+
14+
Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header);
15+
16+
return Symfony\CS\Config\Config::create()
17+
->fixers(array(
18+
'header_comment',
19+
'symfony',
20+
'concat_with_spaces',
21+
'ordered_use',
22+
'-concat_without_spaces',
23+
'-phpdoc_indent',
24+
'-phpdoc_params',
25+
))
26+
->finder(
27+
Symfony\CS\Finder\DefaultFinder::create()
28+
->exclude('vendor')
29+
->in(__DIR__)
30+
)
31+
;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace DTL\PhpcrMigrations\Exception;
4+
5+
class MigratorException extends \Exception
6+
{
7+
}

lib/Migrator.php

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
<?php
2-
/*
3-
* This file is part of the <package> package.
4-
*
5-
* (c) Daniel Leech <daniel@dantleech.com>
6-
*
7-
* For the full copyright and license information, please view the LICENSE
8-
* file that was distributed with this source code.
9-
*/
102

113
namespace DTL\PhpcrMigrations;
124

5+
use DTL\PhpcrMigrations\Exception\MigratorException;
136
use PHPCR\SessionInterface;
14-
use DTL\PhpcrMigrations\VersionStorage;
15-
use DTL\PhpcrMigrations\VersionCollection;
167
use Symfony\Component\Console\Output\OutputInterface;
178

189
class Migrator
1910
{
2011
private $session;
2112
private $versionCollection;
2213
private $versionStorage;
14+
private $actions = array(
15+
'up', 'down', 'top', 'bottom',
16+
);
2317

2418
public function __construct(
2519
SessionInterface $session,
2620
VersionCollection $versionCollection,
2721
VersionStorage $versionStorage
28-
)
29-
{
22+
) {
3023
$this->session = $session;
3124
$this->versionCollection = $versionCollection;
3225
$this->versionStorage = $versionStorage;
@@ -39,7 +32,7 @@ public function __construct(
3932
public function initialize()
4033
{
4134
if ($this->versionStorage->hasVersioningNode()) {
42-
throw MigratorException::cannotInitializeAlreadyHasVersions();
35+
throw MigratorException('Will not re-initialize');
4336
}
4437

4538
foreach (array_keys($this->versionCollection->getAllVersions()) as $timestamp) {
@@ -62,21 +55,13 @@ public function initialize()
6255
*/
6356
public function migrate($to = null, OutputInterface $output)
6457
{
65-
if ($to === null) {
66-
$to = $this->versionCollection->getLatestVersion();
67-
}
68-
6958
if (false === $to) {
7059
return array();
7160
}
7261

73-
$to = (string) $to;
74-
75-
if ($to !== 'V0' && !$this->versionCollection->has($to)) {
76-
throw MigratorException::unknownVersion($to);
77-
}
78-
7962
$from = $this->versionStorage->getCurrentVersion();
63+
$to = $this->resolveTo($to, $from);
64+
8065
$direction = $from > $to ? 'down' : 'up';
8166

8267
$versionsToExecute = $this->versionCollection->getVersions($from, $to, $direction);
@@ -109,4 +94,51 @@ public function getVersions()
10994
{
11095
return $this->versionCollection->getAllVersions();
11196
}
97+
98+
/**
99+
* Resolve the "to" version.
100+
*
101+
* @param string $to
102+
* @param string $from
103+
*
104+
* @return string
105+
*/
106+
private function resolveTo($to, $from)
107+
{
108+
if (is_string($to)) {
109+
$to = strtolower($to);
110+
}
111+
112+
if ($to === 'down') {
113+
$to = $this->versionCollection->getPreviousVersion($from);
114+
}
115+
116+
if ($to === 'up') {
117+
$to = $this->versionCollection->getNextVersion($from);
118+
}
119+
120+
if ($to === 'bottom') {
121+
$to = 0;
122+
}
123+
124+
if ($to === 'top' || null === $to) {
125+
$to = $this->versionCollection->getLatestVersion();
126+
}
127+
128+
if (0 !== $to && false === strtotime($to)) {
129+
throw new MigratorException(sprintf(
130+
'Unknown migration action "%s". Known actions: "%s"',
131+
$to,
132+
implode('", "', $this->actions)
133+
));
134+
}
135+
136+
if (0 !== $to && !$this->versionCollection->has($to)) {
137+
throw new MigratorException(sprintf(
138+
'Unknown version "%s"', $to
139+
));
140+
}
141+
142+
return $to;
143+
}
112144
}

lib/MigratorFactory.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
<?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-
*/
102

113
namespace DTL\PhpcrMigrations;
124

13-
use DTL\PhpcrMigrations\VersionStorage;
14-
use DTL\PhpcrMigrations\VersionFinder;
155
use PHPCR\SessionInterface;
166

177
class MigratorFactory
@@ -24,8 +14,7 @@ public function __construct(
2414
VersionStorage $storage,
2515
VersionFinder $finder,
2616
SessionInterface $session
27-
)
28-
{
17+
) {
2918
$this->storage = $storage;
3019
$this->finder = $finder;
3120
$this->session = $session;

lib/Util.php renamed to lib/MigratorUtil.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
<?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-
*/
102

113
namespace DTL\PhpcrMigrations;
124

13-
class Util
5+
class MigratorUtil
146
{
157
/**
16-
* Return the class name from a file
8+
* Return the class name from a file.
179
*
1810
* Taken from http://stackoverflow.com/questions/7153000/get-class-name-from-file
1911
*
2012
* @param string $file
21-
*
22-
* @return string
13+
*
14+
* @return string
2315
*/
2416
public static function getClassNameFromFile($file)
2517
{
@@ -29,36 +21,40 @@ public static function getClassNameFromFile($file)
2921
$i = 0;
3022

3123
while (!$class) {
32-
if (feof($fp)) break;
24+
if (feof($fp)) {
25+
break;
26+
}
3327

3428
$buffer .= fread($fp, 512);
3529
$tokens = token_get_all($buffer);
3630

37-
if (strpos($buffer, '{') === false) continue;
31+
if (strpos($buffer, '{') === false) {
32+
continue;
33+
}
3834

39-
for (;$i<count($tokens);$i++) {
35+
for (;$i < count($tokens);$i++) {
4036
if ($tokens[$i][0] === T_NAMESPACE) {
41-
for ($j=$i+1;$j<count($tokens); $j++) {
37+
for ($j = $i + 1;$j < count($tokens); $j++) {
4238
if ($tokens[$j][0] === T_STRING) {
43-
$namespace .= '\\'.$tokens[$j][1];
44-
} else if ($tokens[$j] === '{' || $tokens[$j] === ';') {
39+
$namespace .= '\\' . $tokens[$j][1];
40+
} elseif ($tokens[$j] === '{' || $tokens[$j] === ';') {
4541
break;
4642
}
4743
}
4844
}
4945

5046
if ($tokens[$i][0] === T_CLASS) {
51-
for ($j=$i+1;$j<count($tokens);$j++) {
47+
for ($j = $i + 1;$j < count($tokens);$j++) {
5248
if ($tokens[$j] === '{') {
53-
$class = $tokens[$i+2][1];
49+
$class = $tokens[$i + 2][1];
5450
}
5551
}
5652
}
5753
}
5854
};
5955

6056
if (!$class) {
61-
return null;
57+
return;
6258
}
6359

6460
return $namespace . '\\' . $class;

lib/VersionCollection.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
<?php
2-
/*
3-
* This file is part of the <package> package.
4-
*
5-
* (c) Daniel Leech <daniel@dantleech.com>
6-
*
7-
* For the full copyright and license information, please view the LICENSE
8-
* file that was distributed with this source code.
9-
*/
102

113
namespace DTL\PhpcrMigrations;
124

@@ -30,7 +22,7 @@ public function getAllVersions()
3022
return $this->versions;
3123
}
3224

33-
public function getVersions($from, $to)
25+
public function getVersions($from, $to)
3426
{
3527
$direction = $from > $to ? 'down' : 'up';
3628
$result = array();
@@ -69,20 +61,63 @@ public function getVersions($from, $to)
6961
break;
7062
}
7163

72-
7364
$result[$timestamp] = $version;
7465
}
7566

76-
7767
return $result;
7868
}
7969

8070
public function getLatestVersion()
8171
{
8272
end($this->versions);
73+
8374
return key($this->versions);
8475
}
8576

77+
/**
78+
* Return the version after the given version.
79+
*
80+
* @param string $from
81+
*/
82+
public function getNextVersion($from)
83+
{
84+
$found = false;
85+
foreach (array_keys($this->versions) as $timestamp) {
86+
if ($from === null) {
87+
return $timestamp;
88+
}
89+
90+
if ($timestamp == $from) {
91+
$found = true;
92+
continue;
93+
}
94+
95+
if ($found) {
96+
return $timestamp;
97+
}
98+
}
99+
100+
return;
101+
}
102+
103+
/**
104+
* Return the version before the given version.
105+
*
106+
* @param string $from
107+
*/
108+
public function getPreviousVersion($from)
109+
{
110+
$lastTimestamp = 0;
111+
foreach (array_keys($this->versions) as $timestamp) {
112+
if ($timestamp == $from) {
113+
return $lastTimestamp;
114+
}
115+
$lastTimestamp = $timestamp;
116+
}
117+
118+
return 0;
119+
}
120+
86121
private function normalizeTs($ts)
87122
{
88123
return $ts ? $ts : null;

lib/VersionFinder.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
<?php
2-
/*
3-
* This file is part of the <package> package.
4-
*
5-
* (c) Daniel Leech <daniel@dantleech.com>
6-
*
7-
* For the full copyright and license information, please view the LICENSE
8-
* file that was distributed with this source code.
9-
*/
102

113
namespace DTL\PhpcrMigrations;
124

135
use Symfony\Component\Finder\Finder;
14-
use DTL\PhpcrMigrations\Util;
156

167
class VersionFinder
178
{
@@ -40,13 +31,16 @@ public function getCollection()
4031

4132
foreach ($finder as $versionFile) {
4233
$className = $versionFile->getBasename('.php');
43-
require_once($versionFile->getRealPath());
44-
$classFqn = Util::getClassNameFromFile($versionFile->getRealPath());
34+
require_once $versionFile->getRealPath();
35+
$classFqn = MigratorUtil::getClassNameFromFile($versionFile->getRealPath());
4536

4637
$version = new $classFqn();
4738

4839
if (!$version instanceof VersionInterface) {
49-
throw MigratorException::versionNotInstance($className);
40+
throw new MigratorException(sprintf(
41+
'Version class "%s" must implement VersionInterface',
42+
$className
43+
));
5044
}
5145

5246
$versionTimestamp = substr($versionFile->getBaseName(), 7, 12);

0 commit comments

Comments
 (0)