Completed
Push — stable ( 86ecc3...3a45ea )
by Nuno
07:34
created

Provider::isAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 21
    public function isAvailable(): bool
26
    {
27 21
        return class_exists(\Illuminate\Database\DatabaseServiceProvider::class);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 21
    public function register(): void
34
    {
35 21
        if (File::exists(config_path('database.php'))) {
36
            $this->mergeConfigFrom(config_path('database.php'), 'database');
37
        }
38
39 21
        $this->registerDatabaseService();
40
41 21
        $this->registerMigrationService();
42 21
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 21
    public function boot(): void
48
    {
49 21
        if ($this->app->environment() !== 'production') {
50 21
            $this->commands([\Illuminate\Database\Console\Seeds\SeederMakeCommand::class]);
51 21
            $this->commands([\Illuminate\Database\Console\Migrations\MigrateMakeCommand::class]);
52
        }
53
54 21
        $this->commands(
55
            [
56 21
                \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 21
    }
67
68
    /**
69
     * Registers the database service.
70
     *
71
     * Makes this Capsule Instance available globally via static methods.
72
     *
73
     * @return void
74
     */
75 21
    protected function registerDatabaseService(): void
76
    {
77 21
        $this->app['config']->set('database.migrations', 'migrations');
78 21
        $this->app->alias('db', \Illuminate\Database\ConnectionResolverInterface::class);
79
80 21
        $this->app->register(\Illuminate\Database\DatabaseServiceProvider::class);
81
82 21
        $this->app->make(\Illuminate\Database\Capsule\Manager::class)
83 21
            ->setAsGlobal();
84
85 21
        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 21
    }
93
94
    /**
95
     * Registers the migration service.
96
     *
97
     * @return void
98
     */
99 21
    protected function registerMigrationService(): void
100
    {
101 21
        $config = $this->app['config'];
102 21
        $config->set('database.migrations', $config->get('database.migrations') ?: 'migrations');
103 21
        $this->app->register(\Illuminate\Database\MigrationServiceProvider::class);
104
105 21
        $this->app->alias(
106 21
            'migration.repository',
107 21
            \Illuminate\Database\Migrations\MigrationRepositoryInterface::class
108
        );
109
110 21
        $this->app->singleton('migrator', function ($app) {
111 21
            $repository = $app['migration.repository'];
112
113 21
            return new Migrator($repository, $app['db'], $app['files']);
114 21
        });
115
116 21
        $this->app->alias(
117 21
            'migrator',
118 21
            \Illuminate\Database\Migrations\Migrator::class
119
        );
120 21
    }
121
}
122