Passed
Push — master ( b4b8e8...3089dd )
by Arthur
26:17 queued 02:28
created

BootstrapServiceProvider::loadServiceProviders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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