Passed
Push — master ( 139939...b4b8e8 )
by Arthur
21:54 queued 17s
created

BootstrapServiceProvider::loadPolicies()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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