Test Setup Failed
Push — 0.7 ( 098c24...38226e )
by
unknown
05:57
created

ChiefServiceProvider::bootChiefSquanto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Providers;
4
5
use Illuminate\Auth\Events\Login;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Event;
9
use Illuminate\Support\ServiceProvider;
10
use Spatie\Sitemap\SitemapServiceProvider;
11
use Thinktomorrow\AssetLibrary\AssetLibraryServiceProvider;
12
use Thinktomorrow\Chief\Admin\Authorization\ChiefUserProvider;
13
use Thinktomorrow\Chief\Admin\Nav\Nav;
14
use Thinktomorrow\Chief\Admin\Settings\SettingFields;
15
use Thinktomorrow\Chief\Admin\Settings\Settings;
16
use Thinktomorrow\Chief\Admin\Users\Application\EnableUser;
17
use Thinktomorrow\Chief\Admin\Users\Invites\Application\SendInvite;
18
use Thinktomorrow\Chief\Admin\Users\Invites\Events\InviteAccepted;
19
use Thinktomorrow\Chief\Admin\Users\Invites\Events\UserInvited;
20
use Thinktomorrow\Chief\Admin\Users\User;
21
use Thinktomorrow\Chief\App\Console\GenerateSitemap;
22
use Thinktomorrow\Chief\App\Http\Controllers\Back\System\SettingsController;
23
use Thinktomorrow\Chief\App\Listeners\LogSuccessfulLogin;
24
use Thinktomorrow\Chief\Forms\FormsServiceProvider;
25
use Thinktomorrow\Chief\Fragments\Actions\DeleteFragment;
26
use Thinktomorrow\Chief\Fragments\Actions\UpdateFragmentMetadata;
27
use Thinktomorrow\Chief\Fragments\Database\FragmentModel;
28
use Thinktomorrow\Chief\Fragments\Events\FragmentAdded;
29
use Thinktomorrow\Chief\Fragments\Events\FragmentDetached;
30
use Thinktomorrow\Chief\Fragments\Events\FragmentDuplicated;
31
use Thinktomorrow\Chief\ManagedModels\Events\ManagedModelCreated;
32
use Thinktomorrow\Chief\Managers\Register\Registry;
33
use Thinktomorrow\Chief\Shared\AdminEnvironment;
34
use Thinktomorrow\Chief\Site\Urls\Application\CreateUrlForPage;
35
use Thinktomorrow\Squanto\SquantoManagerServiceProvider;
36
use Thinktomorrow\Squanto\SquantoServiceProvider;
37
38
class ChiefServiceProvider extends ServiceProvider
39
{
40
    public function boot(): void
41
    {
42
        /*
43
         * ------------------------------------
44
         * Boot required for frontend
45
         * ------------------------------------
46
         */
47
        (new SquantoServiceProvider($this->app))->boot();
48
        (new RoutesServiceProvider($this->app))->boot();
49
50
        Relation::morphMap(['fragmentmodel' => FragmentModel::class]);
51
52
        if (! $this->app->make(AdminEnvironment::class)->check()) {
53
            return;
54
        }
55
56
        /*
57
         * ------------------------------------
58
         * Boot required for admin
59
         * ------------------------------------
60
         */
61
        $this->bootChiefAuth();
62
        $this->bootChiefSquanto();
63
        $this->bootEvents();
64
65
        (new ViewServiceProvider($this->app))->boot();
66
        (new FormsServiceProvider($this->app))->boot();
67
        (new SquantoManagerServiceProvider($this->app))->boot();
68
        (new AssetLibraryServiceProvider($this->app))->boot();
69
        (new SitemapServiceProvider($this->app))->boot();
70
71
        // Sitemap command is used by both cli and web scripts
72
        $this->commands(['command.chief:sitemap']);
73
        $this->app->bind('command.chief:sitemap', GenerateSitemap::class);
74
75
        if ($this->app->runningInConsole()) {
76
            (new ConsoleServiceProvider($this->app))->boot();
77
        }
78
    }
79
80
    public function register()
81
    {
82
        $this->mergeConfigFrom(__DIR__.'/../../config/chief.php', 'chief');
83
        $this->mergeConfigFrom(__DIR__.'/../../config/chief-settings.php', 'chief-settings');
84
85
        if ($this->app->runningInConsole()) {
86
            (new ConsoleServiceProvider($this->app))->register();
87
        }
88
89
        $this->app->singleton(Registry::class, function () {
90
            return new Registry([]);
91
        });
92
93
        $this->app->singleton(Settings::class, function () {
94
            return new Settings();
95
        });
96
97
        (new SquantoServiceProvider($this->app))->register();
98
99
        if ($this->app->make(AdminEnvironment::class)->check()) {
100
            $this->app->when(SettingsController::class)
101
                ->needs(SettingFields::class)
102
                ->give(function () {
103
                    return new SettingFields(new Settings());
104
                })
105
            ;
106
107
            // Global chief nav singleton
108
            $this->app->singleton(Nav::class, function () {
109
                return new Nav();
110
            });
111
112
            (new SquantoManagerServiceProvider($this->app))->register();
113
            (new AssetLibraryServiceProvider($this->app))->register();
114
            (new SitemapServiceProvider($this->app))->register();
115
        }
116
    }
117
118
    private function bootChiefAuth(): void
119
    {
120
        $this->app['config']['auth.guards.chief'] = [
121
            'driver' => 'session',
122
            'provider' => 'chief',
123
        ];
124
125
        $this->app['config']['auth.providers.chief'] = [
126
            'driver' => 'chief-eloquent',
127
            'model' => User::class,
128
        ];
129
130
        $this->app['config']['auth.passwords.chief'] = [
131
            'provider' => 'chief',
132
            'table' => 'chief_password_resets',
133
            'expire' => 60,
134
        ];
135
136
        // Custom models for permission
137
        $this->app['config']['permission.models'] = [
138
            'permission' => \Thinktomorrow\Chief\Admin\Authorization\Permission::class,
139
            'role' => \Thinktomorrow\Chief\Admin\Authorization\Role::class,
140
        ];
141
142
        Auth::provider('chief-eloquent', function ($app, array $config) {
143
            return new ChiefUserProvider($app['hash'], $config['model']);
144
        });
145
    }
146
147
    private function bootChiefSquanto(): void
148
    {
149
        // Project specific squanto files
150
        $this->app['view']->addNamespace('squanto', __DIR__.'/../../resources/views/vendor/squanto');
151
152
        // Chief squanto defaults
153
        $this->app['view']->addNamespace('squanto', base_path().'/resources/views/vendor/thinktomorrow/chief/vendor/squanto');
154
155
        // Use the chief routing
156
        $this->app['config']['squanto.use_default_routes'] = false;
157
    }
158
159
    private function bootEvents(): void
160
    {
161
        Event::listen(Login::class, LogSuccessfulLogin::class);
162
        Event::listen(UserInvited::class, SendInvite::class);
163
        Event::listen(InviteAccepted::class, EnableUser::class.'@onAcceptingInvite');
164
165
        Event::listen(ManagedModelCreated::class, CreateUrlForPage::class.'@onManagedModelCreated');
166
        Event::listen(FragmentDetached::class, DeleteFragment::class.'@onFragmentDetached');
167
        Event::listen(FragmentDetached::class, UpdateFragmentMetadata::class.'@onFragmentDetached');
168
        Event::listen(FragmentAdded::class, UpdateFragmentMetadata::class.'@onFragmentAdded');
169
        Event::listen(FragmentDuplicated::class, UpdateFragmentMetadata::class.'@onFragmentDuplicated');
170
    }
171
}
172