Test Setup Failed
Push — master ( 40b2f2...aac3f3 )
by Ben
09:14 queued 11s
created

ChiefServiceProvider::bootFrontendEssentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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