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
113namespace DTL \PhpcrMigrations ;
124
5+ use DTL \PhpcrMigrations \Exception \MigratorException ;
136use PHPCR \SessionInterface ;
14- use DTL \PhpcrMigrations \VersionStorage ;
15- use DTL \PhpcrMigrations \VersionCollection ;
167use Symfony \Component \Console \Output \OutputInterface ;
178
189class 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}
0 commit comments