FoundationServiceProvider.php
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Illuminate\Foundation\Providers;
use Illuminate\Http\Request;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Support\AggregateServiceProvider;
use Illuminate\Support\Facades\URL;
use Illuminate\Testing\LoggedExceptionCollection;
use Illuminate\Testing\ParallelTestingServiceProvider;
use Illuminate\Validation\ValidationException;
class FoundationServiceProvider extends AggregateServiceProvider
{
/**
* The provider class names.
*
* @var string[]
*/
protected $providers = [
FormRequestServiceProvider::class,
ParallelTestingServiceProvider::class,
];
/**
* Boot the service provider.
*
* @return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../Exceptions/views' => $this->app->resourcePath('views/errors/'),
], 'laravel-errors');
}
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
parent::register();
$this->registerRequestValidation();
$this->registerRequestSignatureValidation();
$this->registerExceptionTracking();
}
/**
* Register the "validate" macro on the request.
*
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
public function registerRequestValidation()
{
Request::macro('validate', function (array $rules, ...$params) {
return validator()->validate($this->all(), $rules, ...$params);
});
Request::macro('validateWithBag', function (string $errorBag, array $rules, ...$params) {
try {
return $this->validate($rules, ...$params);
} catch (ValidationException $e) {
$e->errorBag = $errorBag;
throw $e;
}
});
}
/**
* Register the "hasValidSignature" macro on the request.
*
* @return void
*/
public function registerRequestSignatureValidation()
{
Request::macro('hasValidSignature', function ($absolute = true) {
return URL::hasValidSignature($this, $absolute);
});
Request::macro('hasValidRelativeSignature', function () {
return URL::hasValidSignature($this, $absolute = false);
});
}
/**
* Register an event listener to track logged exceptions.
*
* @return void
*/
protected function registerExceptionTracking()
{
if (! $this->app->runningUnitTests()) {
return;
}
$this->app->instance(
LoggedExceptionCollection::class,
new LoggedExceptionCollection
);
$this->app->make('events')->listen(MessageLogged::class, function ($event) {
if (isset($event->context['exception'])) {
$this->app->make(LoggedExceptionCollection::class)
->push($event->context['exception']);
}
});
}
}