|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Swis\JsonApi\Client\Providers; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Http\Client\PendingRequest; |
|
8
|
|
|
use Illuminate\Http\Client\Response; |
|
9
|
|
|
use Illuminate\Support\Facades\Http; |
|
10
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
|
11
|
|
|
use Swis\JsonApi\Client\Client; |
|
12
|
|
|
use Swis\JsonApi\Client\DocumentClient; |
|
13
|
|
|
use Swis\JsonApi\Client\Facades\ResponseParserFacade; |
|
14
|
|
|
use Swis\JsonApi\Client\Interfaces\ClientInterface; |
|
15
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface; |
|
16
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentInterface; |
|
17
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentParserInterface; |
|
18
|
|
|
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface; |
|
19
|
|
|
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface; |
|
20
|
|
|
use Swis\JsonApi\Client\Parsers\DocumentParser; |
|
21
|
|
|
use Swis\JsonApi\Client\Parsers\ResponseParser; |
|
22
|
|
|
use Swis\JsonApi\Client\TypeMapper; |
|
23
|
|
|
|
|
24
|
|
|
class ServiceProvider extends BaseServiceProvider |
|
25
|
|
|
{ |
|
26
|
12 |
|
public function register() |
|
27
|
|
|
{ |
|
28
|
12 |
|
$this->mergeConfigFrom( |
|
29
|
12 |
|
dirname(__DIR__, 2).'/config/json-api-client.php', |
|
30
|
12 |
|
'json-api-client' |
|
31
|
6 |
|
); |
|
32
|
|
|
|
|
33
|
12 |
|
$this->registerSharedTypeMapper(); |
|
34
|
12 |
|
$this->registerParsers(); |
|
35
|
12 |
|
$this->registerClients(); |
|
36
|
|
|
|
|
37
|
12 |
|
$this->registerLaravelHttpClientMacros(); |
|
38
|
6 |
|
} |
|
39
|
|
|
|
|
40
|
12 |
|
public function boot() |
|
41
|
|
|
{ |
|
42
|
12 |
|
$this->publishes([ |
|
43
|
12 |
|
dirname(__DIR__, 2).'/config/' => config_path(), |
|
44
|
12 |
|
], 'config'); |
|
45
|
6 |
|
} |
|
46
|
|
|
|
|
47
|
12 |
|
protected function registerSharedTypeMapper() |
|
48
|
|
|
{ |
|
49
|
12 |
|
$this->app->bind(TypeMapperInterface::class, TypeMapper::class); |
|
50
|
12 |
|
$this->app->singleton(TypeMapper::class); |
|
51
|
6 |
|
} |
|
52
|
|
|
|
|
53
|
12 |
|
protected function registerParsers() |
|
54
|
|
|
{ |
|
55
|
12 |
|
$this->app->bind(DocumentParserInterface::class, DocumentParser::class); |
|
56
|
12 |
|
$this->app->bind(ResponseParserInterface::class, ResponseParser::class); |
|
57
|
6 |
|
} |
|
58
|
|
|
|
|
59
|
12 |
|
protected function registerClients() |
|
60
|
|
|
{ |
|
61
|
12 |
|
$this->app->extend( |
|
62
|
12 |
|
ClientInterface::class, |
|
63
|
12 |
|
static function (ClientInterface $client) { |
|
64
|
|
|
if ($baseUri = config('json-api-client.base_uri')) { |
|
65
|
|
|
$client->setBaseUri($baseUri); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $client; |
|
69
|
12 |
|
} |
|
70
|
6 |
|
); |
|
71
|
|
|
|
|
72
|
12 |
|
$this->app->bind(ClientInterface::class, Client::class); |
|
73
|
12 |
|
$this->app->bind(DocumentClientInterface::class, DocumentClient::class); |
|
74
|
6 |
|
} |
|
75
|
|
|
|
|
76
|
12 |
|
protected function registerLaravelHttpClientMacros(): void |
|
77
|
|
|
{ |
|
78
|
12 |
|
if (! class_exists(Http::class)) { |
|
79
|
|
|
// N.B. The HTTP Client was introduced in Laravel 7. |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
12 |
|
if (! PendingRequest::hasMacro('asJsonApi')) { |
|
84
|
4 |
|
PendingRequest::macro('asJsonApi', function (): PendingRequest { |
|
85
|
|
|
/* @var \Illuminate\Http\Client\PendingRequest $this */ |
|
86
|
4 |
|
return $this->bodyFormat('json') |
|
87
|
4 |
|
->contentType('application/vnd.api+json') |
|
88
|
4 |
|
->accept('application/vnd.api+json'); |
|
89
|
4 |
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
12 |
|
if (! Response::hasMacro('jsonApi')) { |
|
93
|
4 |
|
Response::macro('jsonApi', function (): DocumentInterface { |
|
94
|
|
|
/* @var \Illuminate\Http\Client\Response $this */ |
|
95
|
4 |
|
return ResponseParserFacade::parse($this->toPsrResponse()); |
|
96
|
4 |
|
}); |
|
97
|
|
|
} |
|
98
|
6 |
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|