1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Schnittstabil\Dartisan\ServiceProviders; |
4
|
|
|
|
5
|
|
|
use Garden\Cli\Args; |
6
|
|
|
use Illuminate\Database\Migrations\DatabaseMigrationRepository; |
7
|
|
|
use Illuminate\Database\Migrations\MigrationCreator; |
8
|
|
|
use Illuminate\Database\Migrations\Migrator; |
9
|
|
|
use Schnittstabil\Dartisan\Commands\MigrateCommand; |
10
|
|
|
use Schnittstabil\Dartisan\Commands\MigrateInstallCommand; |
11
|
|
|
use Schnittstabil\Dartisan\Commands\MigrateMakeCommand; |
12
|
|
|
use Schnittstabil\Dartisan\Commands\MigrateResetCommand; |
13
|
|
|
use Schnittstabil\Dartisan\Commands\MigrateRollbackCommand; |
14
|
|
|
use Schnittstabil\Dartisan\Commands\MigrateStatusCommand; |
15
|
|
|
use Schnittstabil\Dartisan\Container; |
16
|
|
|
use Schnittstabil\Dartisan\OutputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
20
|
|
|
*/ |
21
|
|
|
class CommandsServiceProvider |
22
|
|
|
{ |
23
|
|
|
protected function registerCommand(Container $container, $class, callable $factory) |
24
|
|
|
{ |
25
|
|
|
$container->set($class, $factory); |
26
|
|
|
|
27
|
|
|
return ['name' => $class::$name, 'class' => $class]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function registerCommands(Container $container) |
31
|
|
|
{ |
32
|
|
|
$commands = []; |
33
|
|
|
|
34
|
|
View Code Duplication |
$commands[] = $this->registerCommand($container, MigrateCommand::class, function (Container $c) { |
35
|
|
|
return new MigrateCommand( |
36
|
|
|
$c->get(Args::class), |
37
|
|
|
$c->get(OutputInterface::class), |
38
|
|
|
$c->get(Migrator::class), |
39
|
|
|
$c->get('migration-path') |
40
|
|
|
); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
$commands[] = $this->registerCommand($container, MigrateInstallCommand::class, function (Container $c) { |
44
|
|
|
return new MigrateInstallCommand( |
45
|
|
|
$c->get(Args::class), |
46
|
|
|
$c->get(OutputInterface::class), |
47
|
|
|
$c->get(DatabaseMigrationRepository::class) |
48
|
|
|
); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
$commands[] = $this->registerCommand($container, MigrateMakeCommand::class, function (Container $c) { |
52
|
|
|
return new MigrateMakeCommand( |
53
|
|
|
$c->get(Args::class), |
54
|
|
|
$c->get(OutputInterface::class), |
55
|
|
|
$c->get(MigrationCreator::class), |
56
|
|
|
$c->get('migration-path') |
57
|
|
|
); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
View Code Duplication |
$commands[] = $this->registerCommand($container, MigrateResetCommand::class, function (Container $c) { |
61
|
|
|
return new MigrateResetCommand( |
62
|
|
|
$c->get(Args::class), |
63
|
|
|
$c->get(OutputInterface::class), |
64
|
|
|
$c->get(Migrator::class), |
65
|
|
|
$c->get('migration-path') |
66
|
|
|
); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
View Code Duplication |
$commands[] = $this->registerCommand($container, MigrateRollbackCommand::class, function (Container $c) { |
70
|
|
|
return new MigrateRollbackCommand( |
71
|
|
|
$c->get(Args::class), |
72
|
|
|
$c->get(OutputInterface::class), |
73
|
|
|
$c->get(Migrator::class), |
74
|
|
|
$c->get('migration-path') |
75
|
|
|
); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
View Code Duplication |
$commands[] = $this->registerCommand($container, MigrateStatusCommand::class, function (Container $c) { |
79
|
|
|
return new MigrateStatusCommand( |
80
|
|
|
$c->get(Args::class), |
81
|
|
|
$c->get(OutputInterface::class), |
82
|
|
|
$c->get(Migrator::class), |
83
|
|
|
$c->get('migration-path') |
84
|
|
|
); |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
return $commands; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function __invoke(Container $container) |
91
|
|
|
{ |
92
|
|
|
$registeredCommands = $this->registerCommands($container); |
93
|
|
|
|
94
|
|
|
$commands = array_reduce($registeredCommands, function ($acc, $command) { |
95
|
|
|
$acc[$command['name']] = $command['class']; |
96
|
|
|
|
97
|
|
|
return $acc; |
98
|
|
|
}, []); |
99
|
|
|
|
100
|
|
|
$container->set('commands', function () use ($commands) { |
101
|
|
|
return $commands; |
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
$container->set('command', function (Container $c) { |
105
|
|
|
$cmdName = $c->get(Args::class)->getCommand(); |
106
|
|
|
$cmdClass = $c->get('commands')[$cmdName]; |
107
|
|
|
|
108
|
|
|
return $c->get($cmdClass); |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|