|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cerbero\LazyJsonPages\Providers; |
|
6
|
|
|
|
|
7
|
|
|
use Cerbero\LazyJsonPages\LazyJsonPages; |
|
8
|
|
|
use Cerbero\LazyJsonPages\Middleware\Tap; |
|
9
|
|
|
use Illuminate\Http\Client\Events\ConnectionFailed; |
|
10
|
|
|
use Illuminate\Http\Client\Events\RequestSending; |
|
11
|
|
|
use Illuminate\Http\Client\Events\ResponseReceived; |
|
12
|
|
|
use Illuminate\Http\Client\Request; |
|
13
|
|
|
use Illuminate\Http\Client\Response; |
|
14
|
|
|
use Illuminate\Support\ServiceProvider; |
|
15
|
|
|
use Psr\Http\Message\RequestInterface; |
|
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
17
|
|
|
use Throwable; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The service provider to integrate with Laravel. |
|
21
|
|
|
* |
|
22
|
|
|
* @property-read array{events: \Illuminate\Contracts\Events\Dispatcher} $app |
|
23
|
|
|
*/ |
|
24
|
|
|
final class LazyJsonPagesServiceProvider extends ServiceProvider |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Bootstrap the services. |
|
28
|
|
|
*/ |
|
29
|
2 |
|
public function boot(): void |
|
30
|
|
|
{ |
|
31
|
2 |
|
$fireEvents = Tap::once($this->onRequest(...), $this->onResponse(...), $this->onError(...)); |
|
32
|
|
|
|
|
33
|
2 |
|
LazyJsonPages::globalMiddleware('laravel_events', $fireEvents); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Handle HTTP requests before they are sent. |
|
38
|
|
|
*/ |
|
39
|
2 |
|
private function onRequest(RequestInterface $request): void |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
2 |
|
$this->app['events']->dispatch(new RequestSending(new Request($request))); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Handle HTTP responses after they are received. |
|
46
|
|
|
*/ |
|
47
|
1 |
|
private function onResponse(ResponseInterface $response, RequestInterface $request): void |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->app['events']->dispatch(new ResponseReceived(new Request($request), new Response($response))); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Handle a transaction error. |
|
54
|
|
|
*/ |
|
55
|
1 |
|
private function onError(Throwable $e, RequestInterface $request): void |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->app['events']->dispatch(new ConnectionFailed(new Request($request))); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.