Completed
Push — stable ( 386c57...2cb6a4 )
by Nuno
02:05
created

Provider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 102
ccs 40
cts 42
cp 0.9524
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isAvailable() 0 4 1
A register() 0 10 2
A boot() 0 20 2
A registerDatabaseService() 0 18 2
A registerMigrationService() 0 22 2
1
<?php
2
3
/**
4
 * This file is part of Laravel Zero.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace LaravelZero\Framework\Components\Database;
13
14
use Illuminate\Support\Facades\File;
15
use LaravelZero\Framework\Components\AbstractComponentProvider;
16
17
/**
18
 * This is the Laravel Zero Framework Database Component Provider Implementation.
19
 */
20
class Provider extends AbstractComponentProvider
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 19
    public function isAvailable(): bool
26
    {
27 19
        return class_exists(\Illuminate\Database\DatabaseServiceProvider::class);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 19
    public function register(): void
34
    {
35 19
        if (File::exists(config_path('database.php'))) {
36
            $this->mergeConfigFrom(config_path('database.php'), 'database');
37
        }
38
39 19
        $this->registerDatabaseService();
40
41 19
        $this->registerMigrationService();
42 19
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 19
    public function boot(): void
48
    {
49 19
        if ($this->app->environment() !== 'production') {
50 19
            $this->commands([\Illuminate\Database\Console\Seeds\SeederMakeCommand::class]);
51 19
            $this->commands([\Illuminate\Database\Console\Migrations\MigrateMakeCommand::class]);
52
        }
53
54 19
        $this->commands(
55
            [
56 19
                \Illuminate\Database\Console\Migrations\FreshCommand::class,
57
                \Illuminate\Database\Console\Migrations\InstallCommand::class,
58
                \Illuminate\Database\Console\Migrations\MigrateCommand::class,
59
                \Illuminate\Database\Console\Migrations\RefreshCommand::class,
60
                \Illuminate\Database\Console\Migrations\ResetCommand::class,
61
                \Illuminate\Database\Console\Migrations\RollbackCommand::class,
62
                \Illuminate\Database\Console\Migrations\StatusCommand::class,
63
                \Illuminate\Database\Console\Seeds\SeedCommand::class,
64
            ]
65
        );
66 19
    }
67
68
    /**
69
     * Registers the database service.
70
     *
71
     * Makes this Capsule Instance available globally via static methods.
72
     *
73
     * @return void
74
     */
75 19
    protected function registerDatabaseService(): void
76
    {
77 19
        $this->app['config']->set('database.migrations', 'migrations');
78 19
        $this->app->alias('db', \Illuminate\Database\ConnectionResolverInterface::class);
79
80 19
        $this->app->register(\Illuminate\Database\DatabaseServiceProvider::class);
81
82 19
        $this->app->make(\Illuminate\Database\Capsule\Manager::class)
83 19
            ->setAsGlobal();
84
85 19
        if (File::exists($this->app->databasePath('seeds'))) {
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
86 11
            collect(File::files($this->app->databasePath('seeds')))->each(
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
87 11
                function ($file) {
88
                    File::requireOnce($file);
89 11
                }
90
            );
91
        }
92 19
    }
93
94
    /**
95
     * Registers the migration service.
96
     *
97
     * @return void
98
     */
99 19
    protected function registerMigrationService(): void
100
    {
101 19
        $config = $this->app['config'];
102 19
        $config->set('database.migrations', $config->get('database.migrations') ?: 'migrations');
103 19
        $this->app->register(\Illuminate\Database\MigrationServiceProvider::class);
104
105 19
        $this->app->alias(
106 19
            'migration.repository',
107 19
            \Illuminate\Database\Migrations\MigrationRepositoryInterface::class
108
        );
109
110 19
        $this->app->singleton('migrator', function ($app) {
111 19
            $repository = $app['migration.repository'];
112
113 19
            return new Migrator($repository, $app['db'], $app['files']);
114 19
        });
115
116 19
        $this->app->alias(
117 19
            'migrator',
118 19
            \Illuminate\Database\Migrations\Migrator::class
119
        );
120 19
    }
121
}
122