Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Providers;
4
 
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Contracts\Foundation\MaintenanceMode as MaintenanceModeContract;
8
use Illuminate\Contracts\View\Factory;
9
use Illuminate\Database\ConnectionInterface;
10
use Illuminate\Database\Grammar;
11
use Illuminate\Foundation\Console\CliDumper;
12
use Illuminate\Foundation\Http\HtmlDumper;
13
use Illuminate\Foundation\MaintenanceModeManager;
14
use Illuminate\Foundation\Precognition;
15
use Illuminate\Foundation\Vite;
16
use Illuminate\Http\Request;
17
use Illuminate\Log\Events\MessageLogged;
18
use Illuminate\Support\AggregateServiceProvider;
19
use Illuminate\Support\Facades\URL;
20
use Illuminate\Testing\LoggedExceptionCollection;
21
use Illuminate\Testing\ParallelTestingServiceProvider;
22
use Illuminate\Validation\ValidationException;
23
use Symfony\Component\VarDumper\Caster\StubCaster;
24
use Symfony\Component\VarDumper\Cloner\AbstractCloner;
25
 
26
class FoundationServiceProvider extends AggregateServiceProvider
27
{
28
    /**
29
     * The provider class names.
30
     *
31
     * @var string[]
32
     */
33
    protected $providers = [
34
        FormRequestServiceProvider::class,
35
        ParallelTestingServiceProvider::class,
36
    ];
37
 
38
    /**
39
     * The singletons to register into the container.
40
     *
41
     * @var array
42
     */
43
    public $singletons = [
44
        Vite::class => Vite::class,
45
    ];
46
 
47
    /**
48
     * Boot the service provider.
49
     *
50
     * @return void
51
     */
52
    public function boot()
53
    {
54
        if ($this->app->runningInConsole()) {
55
            $this->publishes([
56
                __DIR__.'/../Exceptions/views' => $this->app->resourcePath('views/errors/'),
57
            ], 'laravel-errors');
58
        }
59
    }
60
 
61
    /**
62
     * Register the service provider.
63
     *
64
     * @return void
65
     */
66
    public function register()
67
    {
68
        parent::register();
69
 
70
        $this->registerDumper();
71
        $this->registerRequestValidation();
72
        $this->registerRequestSignatureValidation();
73
        $this->registerExceptionTracking();
74
        $this->registerMaintenanceModeManager();
75
    }
76
 
77
    /**
78
     * Register an var dumper (with source) to debug variables.
79
     *
80
     * @return void
81
     */
82
    public function registerDumper()
83
    {
84
        AbstractCloner::$defaultCasters[ConnectionInterface::class] = [StubCaster::class, 'cutInternals'];
85
        AbstractCloner::$defaultCasters[Container::class] = [StubCaster::class, 'cutInternals'];
86
        AbstractCloner::$defaultCasters[Dispatcher::class] = [StubCaster::class, 'cutInternals'];
87
        AbstractCloner::$defaultCasters[Factory::class] = [StubCaster::class, 'cutInternals'];
88
        AbstractCloner::$defaultCasters[Grammar::class] = [StubCaster::class, 'cutInternals'];
89
 
90
        $basePath = $this->app->basePath();
91
 
92
        $compiledViewPath = $this->app['config']->get('view.compiled');
93
 
94
        $format = $_SERVER['VAR_DUMPER_FORMAT'] ?? null;
95
 
96
        match (true) {
97
            'html' == $format => HtmlDumper::register($basePath, $compiledViewPath),
98
            'cli' == $format => CliDumper::register($basePath, $compiledViewPath),
99
            'server' == $format => null,
100
            $format && 'tcp' == parse_url($format, PHP_URL_SCHEME) => null,
101
            default => in_array(PHP_SAPI, ['cli', 'phpdbg']) ? CliDumper::register($basePath, $compiledViewPath) : HtmlDumper::register($basePath, $compiledViewPath),
102
        };
103
    }
104
 
105
    /**
106
     * Register the "validate" macro on the request.
107
     *
108
     * @return void
109
     *
110
     * @throws \Illuminate\Validation\ValidationException
111
     */
112
    public function registerRequestValidation()
113
    {
114
        Request::macro('validate', function (array $rules, ...$params) {
115
            return tap(validator($this->all(), $rules, ...$params), function ($validator) {
116
                if ($this->isPrecognitive()) {
117
                    $validator->after(Precognition::afterValidationHook($this))
118
                        ->setRules(
119
                            $this->filterPrecognitiveRules($validator->getRulesWithoutPlaceholders())
120
                        );
121
                }
122
            })->validate();
123
        });
124
 
125
        Request::macro('validateWithBag', function (string $errorBag, array $rules, ...$params) {
126
            try {
127
                return $this->validate($rules, ...$params);
128
            } catch (ValidationException $e) {
129
                $e->errorBag = $errorBag;
130
 
131
                throw $e;
132
            }
133
        });
134
    }
135
 
136
    /**
137
     * Register the "hasValidSignature" macro on the request.
138
     *
139
     * @return void
140
     */
141
    public function registerRequestSignatureValidation()
142
    {
143
        Request::macro('hasValidSignature', function ($absolute = true) {
144
            return URL::hasValidSignature($this, $absolute);
145
        });
146
 
147
        Request::macro('hasValidRelativeSignature', function () {
148
            return URL::hasValidSignature($this, $absolute = false);
149
        });
150
 
151
        Request::macro('hasValidSignatureWhileIgnoring', function ($ignoreQuery = [], $absolute = true) {
152
            return URL::hasValidSignature($this, $absolute, $ignoreQuery);
153
        });
154
    }
155
 
156
    /**
157
     * Register an event listener to track logged exceptions.
158
     *
159
     * @return void
160
     */
161
    protected function registerExceptionTracking()
162
    {
163
        if (! $this->app->runningUnitTests()) {
164
            return;
165
        }
166
 
167
        $this->app->instance(
168
            LoggedExceptionCollection::class,
169
            new LoggedExceptionCollection
170
        );
171
 
172
        $this->app->make('events')->listen(MessageLogged::class, function ($event) {
173
            if (isset($event->context['exception'])) {
174
                $this->app->make(LoggedExceptionCollection::class)
175
                        ->push($event->context['exception']);
176
            }
177
        });
178
    }
179
 
180
    /**
181
     * Register the maintenance mode manager service.
182
     *
183
     * @return void
184
     */
185
    public function registerMaintenanceModeManager()
186
    {
187
        $this->app->singleton(MaintenanceModeManager::class);
188
 
189
        $this->app->bind(
190
            MaintenanceModeContract::class,
191
            fn () => $this->app->make(MaintenanceModeManager::class)->driver()
192
        );
193
    }
194
}