Passed
Push — feature/laravel-11-support ( 6c6616...4492de )
by Bas
04:19 queued 15s
created

MigrationServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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