Passed
Push — ft/package ( 5ee474...180175 )
by Philippe
05:16 queued 12s
created

ChiefServiceProvider::boot()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 28
nc 2
nop 0
dl 0
loc 46
ccs 25
cts 25
cp 1
crap 2
rs 8.9411
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Providers;
4
5
use Illuminate\Contracts\Debug\ExceptionHandler;
6
use Thinktomorrow\Chief\App\Console\CreateAdmin;
7
use Thinktomorrow\Chief\Pages\Console\GeneratePage;
8
use Thinktomorrow\Chief\App\Console\RefreshDatabase;
9
use Thinktomorrow\Chief\App\Exceptions\Handler;
10
use Thinktomorrow\Chief\Authorization\Console\GeneratePermissionCommand;
11
use Thinktomorrow\Chief\Authorization\Console\GenerateRoleCommand;
12
use Illuminate\Support\Facades\Blade;
13
use Illuminate\Support\ServiceProvider;
14
use Thinktomorrow\Chief\Users\User;
15
16
class ChiefServiceProvider extends ServiceProvider
17
{
18 157
    public function boot()
19
    {
20 157
        $this->registerChiefGuard();
21
22 157
        (new AuthServiceProvider($this->app))->boot();
23 157
        (new EventServiceProvider($this->app))->boot();
24
25 157
        $this->loadRoutesFrom(__DIR__.'/../routes.php');
26 157
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'chief');
27 157
        $this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
28
29 157
        $this->publishes([
30 157
            __DIR__.'/../../config/chief.php' => config_path('thinktomorrow/chief.php'),
31 157
        ], 'chief-config');
32
33 157
        $this->publishes([
34 157
            __DIR__.'/../../public/chief-assets' => public_path('/chief-assets'),
35 157
        ], 'chief-assets');
36
37
        // Register commands
38 157
        if ($this->app->runningInConsole()) {
39 157
            $this->commands([
40
                // Local development
41 157
                'command.chief:refresh',
42
43
                // Project setup tools
44
                'command.chief:permission',
45
                'command.chief:role',
46
                'command.chief:admin',
47
                'command.chief:page',
48
            ]);
49
50
            // Bind our commands to the container
51 157
            $this->app->bind('command.chief:refresh', RefreshDatabase::class);
52 157
            $this->app->bind('command.chief:permission', GeneratePermissionCommand::class);
53 157
            $this->app->bind('command.chief:role', GenerateRoleCommand::class);
54 157
            $this->app->bind('command.chief:admin', CreateAdmin::class);
55
            $this->app->bind('command.chief:page', function ($app) {
56 81
                return new GeneratePage($app['files'], [
57 81
                    'base_path' => base_path()
58
                ]);
59 157
            });
60
        }
61
62 157
        Blade::component('chief::back._layouts._partials.header', 'chiefheader');
63 157
        Blade::component('chief::back._elements.formgroup', 'chiefformgroup');
64 157
    }
65
66 157
    public function register()
67
    {
68
        // TODO: test this logic...
69 157
        $this->mergeConfigFrom(__DIR__.'/../../config/chief.php', 'thinktomorrow.chief');
70
71 157
        $this->setupEnvironmentProviders();
72
73 157
        (new AuthServiceProvider($this->app))->register();
74 157
        (new EventServiceProvider($this->app))->register();
75 157
    }
76
77
    /**
78
     * Conditionally loads providers for specific environments.
79
     *
80
     * The app()->register() will both trigger the register and boot
81
     * methods of the service provider
82
     */
83 157
    private function setupEnvironmentProviders()
84
    {
85 157
        if (!$this->app->environment('production') && $services = config('app.providers-'.app()->environment(), false)) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        if (!$this->app->environment('production') && $services = config('app.providers-'.app()->/** @scrutinizer ignore-call */ environment(), false)) {
Loading history...
86
            foreach ($services as $service) {
87
                $this->app->register($service);
88
            }
89
        }
90 157
    }
91
92 157
    private function registerChiefGuard()
93
    {
94 157
        $this->app['config']["auth.guards.chief"] = [
95
            'driver'   => 'session',
96
            'provider' => 'chief',
97
        ];
98
99 157
        $this->app['config']["auth.providers.chief"] = [
100
            'driver' => 'chief-eloquent',
101
            'model'  => User::class,
102
        ];
103
104 157
        $this->app['config']["auth.passwords.chief"] = [
105
            'provider' => 'chief',
106
            'table'    => 'chief_password_resets',
107
            'expire'   => 60,
108
        ];
109 157
    }
110
}
111