Passed
Push — next ( a64239...381d12 )
by Bas
06:24 queued 02:50
created

MigrationServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Providers;
6
7
use Illuminate\Database\Migrations\Migrator;
8
use Illuminate\Database\MigrationServiceProvider as IlluminateMigrationServiceProvider;
9
use LaravelFreelancerNL\Aranguent\Console\Concerns\ArangoCommands;
10
use LaravelFreelancerNL\Aranguent\Console\Migrations\FreshCommand;
11
use LaravelFreelancerNL\Aranguent\Console\Migrations\MigrationsConvertCommand;
12
use LaravelFreelancerNL\Aranguent\Console\Migrations\MigrateMakeCommand;
13
use LaravelFreelancerNL\Aranguent\Console\Migrations\MigrateInstallCommand;
14
use LaravelFreelancerNL\Aranguent\Migrations\DatabaseMigrationRepository;
15
use LaravelFreelancerNL\Aranguent\Migrations\MigrationCreator;
16
17
class MigrationServiceProvider extends IlluminateMigrationServiceProvider
18
{
19
    use ArangoCommands;
20
21
    protected bool $defer = true;
22
23
    /**
24
     * @var string[]
25
     */
26
    protected $aliases = [
27
        'Migrator' => 'migrator',
28
        'Creator' => 'migration.creator',
29
        'Repository' => 'migration.repository',
30
        'MigrateMake' => 'migrate.make',
31
        'MigrateInstall' => MigrateInstallCommand::class,
32
        'MigrateFresh' => FreshCommand::class,
33
    ];
34
35
    /**
36
     * Create a new service provider instance.
37
     *
38
     * @param  \Illuminate\Contracts\Foundation\Application  $app
39
     * @return void
40
     */
41 451
    public function __construct($app)
42
    {
43 451
        parent::__construct($app);
44
45 451
        foreach ($this->aliases as $key => $alias) {
46 451
            $this->aliases[$key] = $alias;
47
        }
48
    }
49
50 451
    public function boot(): void
51
    {
52 451
        if ($this->app->runningInConsole()) {
53 451
            $this->commands([
54 451
                MigrateMakeCommand::class,
55 451
                MigrationsConvertCommand::class,
56 451
                FreshCommand::class,
57 451
            ]);
58
        }
59
    }
60
61
    /**
62
     * Register the service provider.
63
     *
64
     * @return void
65
     */
66 451
    public function register()
67
    {
68 451
        $this->registerRepository();
69
70 451
        $this->registerMigrator();
71
72 451
        $this->registerCreator();
73
74 451
        $this->registerCommands($this->commands);
75
    }
76
77
    /**
78
     * Register the migration repository service.
79
     *
80
     * @return void
81
     */
82 451
    protected function registerRepository()
83
    {
84 451
        $this->app->singleton($this->aliases['Repository'], function ($app) {
85
            $table = $app['config']['database.migrations.table'];
86
            if (!$table) {
87
                $table = $app['config']['database.migrations'];
88
            }
89
90
            return new DatabaseMigrationRepository($app['db'], $table);
91 451
        });
92
    }
93
94
    /**
95
     * Register the migrator service.
96
     *
97
     * @return void
98
     */
99 451
    protected function registerMigrator()
100
    {
101
        // The migrator is responsible for actually running and rollback the migration
102
        // files in the application. We'll pass in our database connection resolver
103
        // so the migrator can resolve any of these connections when it needs to.
104 451
        $this->app->singleton($this->aliases['Migrator'], function ($app) {
105
            $repository = $app[$this->aliases['Repository']];
106
107
            return new Migrator($repository, $app['db'], $app['files'], $app['events']);
108 451
        });
109
    }
110
111 451
    protected function registerCreator()
112
    {
113 451
        $this->app->singleton($this->aliases['Creator'], function ($app) {
114
            $customStubPath = __DIR__ . '/../Migrations/stubs';
115
116
            return new MigrationCreator($app['files'], $customStubPath);
117 451
        });
118
    }
119
120
    /**
121
     * Register the command.
122
     *
123
     * @return void
124
     */
125 451
    protected function registerMigrateMakeCommand()
126
    {
127
128 451
        $this->app->singleton($this->commands['MigrateMake'], function ($app) {
129
            $creator = $app['migration.creator'];
130
131
            $composer = $app['composer'];
132
133
            return new MigrateMakeCommand($creator, $composer);
134 451
        });
135
    }
136
137
    /**
138
     * Register the command.
139
     *
140
     * @return void
141
     */
142 451
    protected function registerMigrateInstallCommand()
143
    {
144
145 451
        $this->app->singleton($this->commands['MigrateInstall'], function ($app) {
146
            $repository = $app[$this->aliases['Repository']];
147
148
            return new MigrateInstallCommand($repository);
149 451
        });
150
    }
151
152
    protected function registerMigrationsConvertCommand(): void
153
    {
154
        $this->app->singleton($this->commands['MigrationsConvert'], function ($app) {
155
            return new MigrationsConvertCommand($app['migrator']);
156
        });
157
    }
158
159
    /**
160
     * Register the command.
161
     *
162
     * @return void
163
     */
164 451
    protected function registerMigrateFreshCommand()
165
    {
166 451
        $this->app->singleton(FreshCommand::class, function ($app) {
167 451
            return new FreshCommand($app['migrator']);
168 451
        });
169
    }
170
171
    /**
172
     * @return string[]
173
     */
174
    public function provides()
175
    {
176
        return array_values($this->aliases);
177
    }
178
}
179