Failed Conditions
Push — feature/streamline-console-com... ( 0c27b2...d385c8 )
by
unknown
09:42
created

MigrationServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Migrations\DatabaseMigrationRepository;
13
use LaravelFreelancerNL\Aranguent\Migrations\MigrationCreator;
14
15
class MigrationServiceProvider extends IlluminateMigrationServiceProvider
16
{
17
    use ArangoCommands;
18
19
    protected bool $defer = true;
20
21
    /**
22
     * @var string[]
23
     */
24
    protected $aliases = [
25
        'Migrator' => 'migrator',
26
        'Creator' => 'migration.creator',
27
        'Repository' => 'migration.repository',
28
        'MigrateMake' => 'migrate.make',
29
//        'Migrate' => MigrateCommand::class,
30
//        'MigrateFresh' => FreshCommand::class,
31
//        'MigrateInstall' => InstallCommand::class,
32
//        'MigrateRefresh' => RefreshCommand::class,
33
//        'MigrateReset' => ResetCommand::class,
34
//        'MigrateRollback' => RollbackCommand::class,
35
//        'MigrateStatus' => StatusCommand::class,
36
    ];
37
38
    /**
39
     * Create a new service provider instance.
40
     *
41
     * @param  \Illuminate\Contracts\Foundation\Application  $app
42
     * @return void
43
     */
44
    public function __construct($app)
45
    {
46
        parent::__construct($app);
47
48
        foreach($this->aliases as $key => $alias) {
49
            $this->aliases[$key] = $alias;
50
        }
51
    }
52
53
    public function boot(): void
54
    {
55
        if ($this->app->runningInConsole()) {
56
            $this->commands([
57
                MigrateMakeCommand::class,
58
                MigrationsConvertCommand::class
59
            ]);
60
        }
61
    }
62
63
    /**
64
     * Register the service provider.
65
     *
66
     * @return void
67
     */
68
    public function register()
69
    {
70
        $this->registerRepository();
71
72
        $this->registerMigrator();
73
74
        $this->registerCreator();
75
76
        $this->registerCommands($this->commands);
77
    }
78
79
    /**
80
     * Register the migration repository service.
81
     *
82
     * @return void
83
     */
84
    protected function registerRepository()
85
    {
86
        $this->app->singleton($this->aliases['Repository'], function ($app) {
87
            $table = $app['config']['database.migrations'];
88
89
            return new DatabaseMigrationRepository($app['db'], $table);
90
        });
91
    }
92
93
94
    /**
95
     * Register the migrator service.
96
     *
97
     * @return void
98
     */
99
    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
        $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
        });
109
    }
110
111
    protected function registerCreator()
112
    {
113
        $this->app->singleton($this->aliases['Creator'], function ($app) {
114
            $customStubPath = __DIR__ . '/../Migrations/stubs';
115
116
            return new MigrationCreator($app['files'], $customStubPath);
117
        });
118
    }
119
120
    /**
121
     * Register the command.
122
     *
123
     * @return void
124
     */
125
    protected function registerMigrateMakeCommand()
126
    {
127
128
        $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
        });
135
    }
136
137
    protected function registerMigrationsConvertCommand(): void
138
    {
139
        $this->app->singleton($this->commands['MigrationsConvert'], function ($app) {
140
            return new MigrationsConvertCommand($app['migrator']);
141
        });
142
    }
143
144
    /**
145
     * @return string[]
146
     */
147
    public function provides()
148
    {
149
        return array_values($this->aliases);
150
    }
151
}
152