8
8
use Laminas \Mvc \Console \Controller \AbstractConsoleController ;
9
9
use Psr \Container \ContainerInterface ;
10
10
use Yawik \Migration \Contracts \MigratorInterface ;
11
+ use Yawik \Migration \Handler \MigrationHandler ;
11
12
use Yawik \Migration \Migrator \Version36 ;
12
13
13
14
class ConsoleController extends AbstractConsoleController
@@ -16,31 +17,46 @@ class ConsoleController extends AbstractConsoleController
16
17
* @var iterable|MigratorInterface[]
17
18
*/
18
19
private iterable $ migrators ;
20
+ /**
21
+ * @var MigrationHandler
22
+ */
23
+ private MigrationHandler $ handler ;
19
24
20
25
/**
21
26
* MigrationController constructor.
22
27
*
28
+ * @param MigrationHandler $handler
23
29
* @param iterable $migrators
24
30
*/
25
31
public function __construct (
32
+ MigrationHandler $ handler ,
26
33
iterable $ migrators
27
34
)
28
35
{
29
36
$ this ->migrators = $ migrators ;
37
+ $ this ->handler = $ handler ;
30
38
}
31
39
32
40
public static function factory (ContainerInterface $ container )
33
41
{
42
+ $ handler = $ container ->get (MigrationHandler::class);
34
43
$ migrators = [];
35
44
$ migrators [] = $ container ->get (Version36::class);
36
45
37
- return new self ($ migrators );
46
+ return new self ($ handler , $ migrators );
38
47
}
39
48
40
49
public function migrateAction ()
41
50
{
51
+ $ handler = $ this ->handler ;
42
52
foreach ($ this ->migrators as $ migrator ){
43
- $ migrator ->migrate ();
53
+ $ status = $ handler ->findOrCreate ($ migrator , true );
54
+ if (!$ status ->isMigrated ()){
55
+ $ success = $ migrator ->migrate ();
56
+ if ($ success ){
57
+ $ handler ->migrated ($ migrator );
58
+ }
59
+ }
44
60
}
45
61
}
46
62
0 commit comments