Passed
Push — master ( e5b31a...cf340d )
by Arthur
04:48
created

BootstrapServiceProvider::loadBootstrapService()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Foundation\Providers;
4
5
use Foundation\Console\SeedCommand;
6
use Foundation\Contracts\ModelPolicyContract;
7
use Foundation\Contracts\Ownable;
8
use Foundation\Observers\CacheObserver;
9
use Foundation\Policies\OwnershipPolicy;
10
use Foundation\Services\BootstrapRegistrarService;
11
use Foundation\Traits\Cacheable;
12
use Illuminate\Database\Eloquent\Factory;
13
use Illuminate\Support\Facades\Event;
14
use Illuminate\Support\Facades\Gate;
15
use Illuminate\Support\ServiceProvider;
16
use Route;
17
18
/**
19
 * Class BootstrapServiceProvider.
20
 */
21
class BootstrapServiceProvider extends ServiceProvider
22
{
23
    /**
24
     * @var BootstrapRegistrarService
25
     */
26
    protected $bootstrapService;
27
28 19
    public function boot()
29
    {
30 19
        $this->overrideSeedCommand();
31 19
        $this->loadCacheObservers();
32 19
        $this->loadOwnershipPolicies();
33
34
        /* Register Policies after ownership policies otherwise they would not get overriden */
35 19
        $this->loadPolicies();
36
37
        /* Register all Module Service providers.
38
        ** Always load at the end so the user has the ability to override certain functionality
39
         * */
40 19
        $this->loadServiceProviders();
41 19
    }
42
43 19
    public function register()
44
    {
45
        /* Load BootstrapService here because of the dependencies needed in BootstrapRegistrarService */
46 19
        $this->loadBootstrapService();
47
48 19
        $this->loadCommands();
49 19
        $this->loadRoutes();
50 19
        $this->loadConfigs();
51 19
        $this->loadFactories();
52 19
        $this->loadMigrations();
53 19
        $this->loadListeners();
54 19
    }
55
56 19
    private function loadBootstrapService()
57
    {
58 19
        $this->bootstrapService = new BootstrapRegistrarService();
59
60 19
        if (!($this->app->environment('production') || $this->app->environment('testing'))) {
61
            $this->bootstrapService->recache();
62
        }
63 19
    }
64
65 19
    private function loadCommands()
66
    {
67 19
        $this->commands($this->bootstrapService->getCommands());
68 19
    }
69
70 19
    private function loadRoutes()
71
    {
72 19
        foreach ($this->bootstrapService->getRoutes() as $route) {
73 19
            $path = $route['path'];
74 19
            Route::group([
75 19
                'prefix' => 'v1/' . $route['module'],
76 19
                'namespace' => $route['controller'],
77 19
                'domain' => $route['domain'],
78
                'middleware' => ['api'],
79
            ], function () use ($path) {
80 19
                require $path;
81 19
            });
82 19
            Route::model($route['module'], $route['model']);
83
        }
84 19
    }
85
86
    /**
87
     * Register config.
88
     *
89
     * @return void
90
     */
91 19
    protected function loadConfigs()
92
    {
93 19
        foreach ($this->bootstrapService->getConfigs() as $config) {
94 19
            if ($config['filename'] === 'config.php') {
95 19
                $this->publishes([
96 19
                    $config['path'] => config_path($config['module']),
97 19
                ], 'config');
98 19
                $this->mergeConfigFrom(
99 19
                    $config['path'], basename($config['module'], '.php')
100
                );
101
            }
102
        }
103 19
    }
104
105
    /**
106
     * Register additional directories of factories.
107
     *
108
     * @return void
109
     */
110 19
    public function loadFactories()
111
    {
112 19
        foreach ($this->bootstrapService->getFactories() as $factory) {
113 19
            if (!$this->app->environment('production')) {
114 19
                app(Factory::class)->load($factory['path']);
115
            }
116
        }
117 19
    }
118
119
    /**
120
     * Register additional directories of migrations.
121
     *
122
     * @return void
123
     */
124 19
    public function loadMigrations()
125
    {
126 19
        foreach ($this->bootstrapService->getMigrations() as $migration) {
127 19
            $this->loadMigrationsFrom($migration['path']);
128
        }
129 19
    }
130
131 19
    private function loadPolicies()
132
    {
133 19
        foreach ($this->bootstrapService->getPolicies() as $policy) {
134 19
            if (class_implements_interface($policy['class'], ModelPolicyContract::class)) {
135 19
                Gate::policy($policy['model'], $policy['class']);
136
            }
137
        }
138 19
    }
139
140 19
    private function overrideSeedCommand()
141
    {
142 19
        $app = $this->app;
143 19
        $service = $this->bootstrapService;
144
        $this->app->extend('command.seed', function () use ($app, $service) {
145 19
            return new SeedCommand($app['db'], $service);
146 19
        });
147 19
    }
148
149 19
    private function loadCacheObservers()
150
    {
151 19
        if ((bool)config('model.caching')) {
152 19
            foreach ($this->bootstrapService->getModels() as $model) {
153 19
                if (class_uses_trait($model, Cacheable::class)) {
154 19
                    $model::observe(CacheObserver::class);
155
                }
156
            }
157
        }
158 19
    }
159
160 19
    private function loadOwnershipPolicies()
161
    {
162 19
        foreach ($this->bootstrapService->getModels() as $model) {
163 19
            if (class_implements_interface($model, Ownable::class)) {
164 19
                Gate::policy($model, OwnershipPolicy::class);
165 19
                Gate::define('access', OwnershipPolicy::class . '@access');
166
            }
167
        }
168 19
    }
169
170 19
    private function loadServiceProviders()
171
    {
172 19
        foreach ($this->bootstrapService->getProviders() as $provider) {
173 19
            $this->app->register($provider);
174
        }
175 19
    }
176
177 19
    private function loadListeners()
178
    {
179 19
        foreach ($this->bootstrapService->getEvents() as $event) {
180 19
            foreach ($event['listeners'] as $listener) {
181 19
                Event::listen($event['class'], $listener);
182
            }
183
        }
184 19
    }
185
}
186