DbConfigServiceProvider::insertNewConfiguration()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 5
nop 0
1
<?php
2
3
namespace Nzsakib\DbConfig;
4
5
use Illuminate\Support\ServiceProvider;
6
7
class DbConfigServiceProvider extends ServiceProvider
8
{
9
    /**
10
     * Bootstrap the application services.
11
     */
12
    public function boot()
13
    {
14
        /*
15
         * Optional methods to load your package assets
16
         */
17
18
        $this->loadViewsFrom(__DIR__ . '/resources/views', 'db-config');
19
20
        $this->loadRoutesFrom(__DIR__ . '/routes/web.php');
21
        // $this->loadMigrationsFrom(__DIR__ . '/database/migrations');
22
23
        if ($this->app->runningInConsole()) {
24
            $this->publishes([
25
                __DIR__ . '/../config/config.php' => config_path('db-config.php'),
26
            ], 'config');
27
28
            $this->publishes([
29
                __DIR__ . '/database/migrations/' => database_path('migrations'),
30
            ], 'migrations');
31
32
            // Publishing the views.
33
            /*$this->publishes([
34
                __DIR__.'/../resources/views' => resource_path('views/vendor/db-config'),
35
            ], 'views');*/
36
37
            // Publishing assets.
38
            /*$this->publishes([
39
                __DIR__.'/../resources/assets' => public_path('vendor/db-config'),
40
            ], 'assets');*/
41
42
            // Registering package commands.
43
            // $this->commands([]);
44
        } // end $this->app->runningInConsole()
45
46
        $this->insertNewConfiguration();
47
    }
48
49
    /**
50
     * Register the application services.
51
     */
52
    public function register()
53
    {
54
        // Automatically apply the package configuration
55
        $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'db-config');
56
57
        // Register the main class to use with the facade
58
        $this->app->singleton('db-config', function () {
59
            return new DbConfig;
60
        });
61
    }
62
63
    /**
64
     * Insert new configuration into the service container
65
     *
66
     * @return void
67
     */
68
    protected function insertNewConfiguration()
69
    {
70
        try {
71
            $toMerge = (new DbConfig())->getCachedData();
72
73
            foreach ($toMerge as $key => $value) {
74
                config()->set($key, $value);
75
            }
76
        } catch (\Exception $e) {
77
            //
78
        }
79
    }
80
}
81