Passed
Push — 5.0.0 ( 337324...37581b )
by Fèvre
05:07
created

AppServiceProvider::configureCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Providers;
6
7
use Carbon\CarbonImmutable;
8
use Illuminate\Auth\Notifications\ResetPassword;
9
use Illuminate\Contracts\Foundation\Application;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Support\Facades\App;
12
use Illuminate\Support\Facades\Blade;
13
use Illuminate\Support\Facades\Date;
14
use Illuminate\Support\Facades\DB;
15
use Illuminate\Support\Facades\Route;
16
use Illuminate\Support\Facades\URL;
17
use Illuminate\Support\Facades\View;
18
use Illuminate\Support\Facades\Vite;
19
use Illuminate\Support\ServiceProvider;
20
use Illuminate\Validation\Rules\Password;
21
use Xetaravel\Settings\Settings;
22
use Xetaravel\View\Composers\Blog\SidebarComposer as BlogSidebarComposer;
23
use Xetaravel\View\Composers\Discuss\SidebarComposer as DiscussSidebarComposer;
24
use Xetaravel\View\Composers\NotificationsComposer;
25
26
class AppServiceProvider extends ServiceProvider
27
{
28
    /**
29
     * Bootstrap any application services.
30
     *
31
     * @return void
32
     */
33
    public function boot(): void
34
    {
35
        $this->configureCommands();
36
        $this->configureDates();
37
        $this->configureModels();
38
        $this->configurePasswords();
39
        $this->configureRoutes();
40
        $this->configureUrls();
41
        $this->configureViews();
42
        $this->configureVite();
43
    }
44
45
    /**
46
     * Register any application services.
47
     *
48
     * @return void
49
     */
50
    public function register(): void
51
    {
52
        // Register the Settings class
53
        $this->app->singleton(Settings::class, function (Application $app) {
54
            return new Settings($app['cache.store']);
55
        });
56
    }
57
58
    /**
59
     * Configure the application's commands.
60
     */
61
    private function configureCommands(): void
62
    {
63
        DB::prohibitDestructiveCommands(
64
            $this->app->isProduction(),
65
        );
66
    }
67
68
    /**
69
     * Configure the application's dates.
70
     */
71
    private function configureDates(): void
72
    {
73
        Date::use(CarbonImmutable::class);
74
    }
75
76
    /**
77
     * Configure the application's models.
78
     */
79
    private function configureModels(): void
80
    {
81
        Model::shouldBeStrict();
82
    }
83
84
    /**
85
     * Configure the application's passwords.
86
     */
87
    private function configurePasswords(): void
88
    {
89
        // Set default password rule for the application.
90
        Password::defaults(function () {
91
            $rule = Password::min(8);
92
93
            return App::isProduction() || App::isLocal()
94
                ? $rule->letters()
95
                    ->mixedCase()
96
                    ->numbers()
97
                    ->symbols()
98
                : $rule;
99
        });
100
101
        // ResetPassword
102
        ResetPassword::createUrlUsing(function ($notifiable, $token) {
103
            // Add `auth.` to the route to respect the namespace.
104
            return url(route('auth.password.reset', [
105
                'token' => $token,
106
                'email' => $notifiable->getEmailForPasswordReset(),
107
            ], false));
108
        });
109
    }
110
111
    /**
112
     * Configure the application's routes.
113
     */
114
    private function configureRoutes(): void
115
    {
116
        Route::pattern('id', '[0-9]+');
117
    }
118
119
    /**
120
     * Configure the application's URLs.
121
     */
122
    private function configureUrls(): void
123
    {
124
        URL::forceScheme('https');
125
    }
126
127
    /**
128
     * Configure the application's views.
129
     */
130
    private function configureViews(): void
131
    {
132
        View::addNamespace('Admin', base_path() . '/resources/views/Admin');
133
        View::addNamespace('Blog', base_path() . '/resources/views/Blog');
134
        View::addNamespace('Auth', base_path() . '/resources/views/Auth');
135
        View::addNamespace('Discuss', base_path() . '/resources/views/Discuss');
136
        View::composer('partials._notifications', NotificationsComposer::class);
137
        View::composer('Blog::partials._sidebar', BlogSidebarComposer::class);
138
        View::composer('Discuss::partials._sidebar', DiscussSidebarComposer::class);
139
140
        // Pagination
141
        //Paginator::defaultView('vendor.pagination.tailwind');
142
143
        /**
144
         * All credits from this blade directive goes to Konrad Kalemba.
145
         * Just copied and modified for my very specifc use case.
146
         *
147
         * https://github.com/konradkalemba/blade-components-scoped-slots
148
         */
149
        Blade::directive('scope', function ($expression) {
150
            // Split the expression by `top-level` commas (not in parentheses)
151
            $directiveArguments = preg_split("/,(?![^\(\(]*[\)\)])/", $expression);
152
            $directiveArguments = array_map('trim', $directiveArguments);
153
154
            [$name, $functionArguments] = $directiveArguments;
155
156
            /**
157
             *  Slot names can`t contains dot , eg: `user.city`.
158
             *  So we convert `user.city` to `user___city`
159
             *
160
             *  Later, on component you must replace it back.
161
             */
162
            $name = str_replace('.', '___', $name);
163
164
            return "<?php \$__env->slot({$name}, function({$functionArguments}) use (\$__env) { ?>";
165
        });
166
167
        Blade::directive('endscope', function () {
168
            return '<?php }); ?>';
169
        });
170
    }
171
172
    /**
173
     * Configure the application's Vite instance.
174
     */
175
    private function configureVite(): void
176
    {
177
        Vite::useAggressivePrefetching();
178
    }
179
}
180