3
3
namespace CloudCreativity \LaravelJsonApi \Http \Middleware ;
4
4
5
5
use CloudCreativity \LaravelJsonApi \Api \Api ;
6
+ use CloudCreativity \LaravelJsonApi \Codec \Codec ;
6
7
use CloudCreativity \LaravelJsonApi \Codec \Decoding ;
7
8
use CloudCreativity \LaravelJsonApi \Codec \Encoding ;
8
9
use CloudCreativity \LaravelJsonApi \Contracts \ContainerInterface ;
9
10
use CloudCreativity \LaravelJsonApi \Contracts \Http \ContentNegotiatorInterface ;
10
11
use CloudCreativity \LaravelJsonApi \Exceptions \DocumentRequiredException ;
11
12
use CloudCreativity \LaravelJsonApi \Factories \Factory ;
12
13
use CloudCreativity \LaravelJsonApi \Routing \Route ;
14
+ use Illuminate \Contracts \Container \Container ;
13
15
use Illuminate \Http \Request ;
16
+ use Neomerx \JsonApi \Contracts \Http \Headers \AcceptHeaderInterface ;
17
+ use Neomerx \JsonApi \Contracts \Http \Headers \HeaderInterface ;
14
18
use Neomerx \JsonApi \Contracts \Http \Headers \HeaderParametersInterface ;
15
19
use Symfony \Component \HttpKernel \Exception \HttpException ;
16
- use function CloudCreativity \LaravelJsonApi \http_contains_body ;
17
20
18
21
/**
19
22
* Class NegotiateContent
@@ -24,19 +27,14 @@ class NegotiateContent
24
27
{
25
28
26
29
/**
27
- * @var Factory
28
- */
29
- private $ factory ;
30
-
31
- /**
32
- * @var Api
30
+ * @var Container
33
31
*/
34
- private $ api ;
32
+ private $ container ;
35
33
36
34
/**
37
- * @var HeaderParametersInterface
35
+ * @var Factory
38
36
*/
39
- private $ headers ;
37
+ private $ factory ;
40
38
41
39
/**
42
40
* @var Route
@@ -46,16 +44,14 @@ class NegotiateContent
46
44
/**
47
45
* NegotiateContent constructor.
48
46
*
47
+ * @param Container $container
49
48
* @param Factory $factory
50
- * @param Api $api
51
- * @param HeaderParametersInterface $headers
52
49
* @param Route $route
53
50
*/
54
- public function __construct (Factory $ factory , Api $ api , HeaderParametersInterface $ headers , Route $ route )
51
+ public function __construct (Container $ container , Factory $ factory , Route $ route )
55
52
{
53
+ $ this ->container = $ container ;
56
54
$ this ->factory = $ factory ;
57
- $ this ->api = $ api ;
58
- $ this ->headers = $ headers ;
59
55
$ this ->route = $ route ;
60
56
}
61
57
@@ -71,33 +67,44 @@ public function __construct(Factory $factory, Api $api, HeaderParametersInterfac
71
67
*/
72
68
public function handle ($ request , \Closure $ next , string $ default = null )
73
69
{
74
- $ body = http_contains_body ($ request );
70
+ $ api = $ this ->container ->make (Api::class);
71
+ /** @var HeaderParametersInterface $headers */
72
+ $ headers = $ this ->container ->make (HeaderParametersInterface::class);
73
+ $ contentType = $ headers ->getContentTypeHeader ();
75
74
76
- $ this ->matched (
77
- $ this ->matchEncoding ($ request , $ default ),
78
- $ decoder = $ body ? $ this ->matchDecoder ($ request , $ default ) : null
75
+ $ codec = $ this ->factory ->createCodec (
76
+ $ api ->getContainer (),
77
+ $ this ->matchEncoding ($ api , $ request , $ headers ->getAcceptHeader (), $ default ),
78
+ $ decoder = $ contentType ? $ this ->matchDecoder ($ api , $ request , $ contentType , $ default ) : null
79
79
);
80
80
81
- if (!$ body && $ this ->isExpectingContent ($ request )) {
81
+ $ this ->matched ($ codec );
82
+
83
+ if (!$ contentType && $ this ->isExpectingContent ($ request )) {
82
84
throw new DocumentRequiredException ();
83
85
}
84
86
85
87
return $ next ($ request );
86
88
}
87
89
88
90
/**
91
+ * @param Api $api
89
92
* @param Request $request
93
+ * @param AcceptHeaderInterface $accept
90
94
* @param string|null $defaultNegotiator
91
95
* @return Encoding
92
96
*/
93
- protected function matchEncoding ($ request , ?string $ defaultNegotiator ): Encoding
97
+ protected function matchEncoding (
98
+ Api $ api ,
99
+ $ request ,
100
+ AcceptHeaderInterface $ accept ,
101
+ ?string $ defaultNegotiator
102
+ ): Encoding
94
103
{
95
104
$ negotiator = $ this
96
- ->negotiator ($ this ->responseResourceType (), $ defaultNegotiator )
105
+ ->negotiator ($ api -> getContainer (), $ this ->responseResourceType (), $ defaultNegotiator )
97
106
->withRequest ($ request )
98
- ->withApi ($ this ->api );
99
-
100
- $ accept = $ this ->headers ->getAcceptHeader ();
107
+ ->withApi ($ api );
101
108
102
109
if ($ this ->willSeeMany ($ request )) {
103
110
return $ negotiator ->encodingForMany ($ accept );
@@ -107,18 +114,24 @@ protected function matchEncoding($request, ?string $defaultNegotiator): Encoding
107
114
}
108
115
109
116
/**
117
+ * @param Api $api
110
118
* @param Request $request
119
+ * @param HeaderInterface $contentType
111
120
* @param string|null $defaultNegotiator
112
121
* @return Decoding|null
113
122
*/
114
- protected function matchDecoder ($ request , ?string $ defaultNegotiator ): ?Decoding
123
+ protected function matchDecoder (
124
+ Api $ api ,
125
+ $ request ,
126
+ HeaderInterface $ contentType ,
127
+ ?string $ defaultNegotiator
128
+ ): ?Decoding
115
129
{
116
130
$ negotiator = $ this
117
- ->negotiator ($ this ->route ->getResourceType (), $ defaultNegotiator )
<
17AE
tr class="diff-line-row">
131
+ ->negotiator ($ api -> getContainer (), $ this ->route ->getResourceType (), $ defaultNegotiator )
118
132
->withRequest ($ request )
119
- ->withApi ($ this -> api );
133
+ ->withApi ($ api );
120
134
121
- $ contentType = $ this ->headers ->getContentTypeHeader ();
122
135
$ resource = $ this ->route ->getResource ();
123
136
124
137
if ($ resource && $ field = $ this ->route ->getRelationshipName ()) {
@@ -139,18 +152,23 @@ protected function responseResourceType(): ?string
139
152
}
140
153
141
154
/**
155
+ * @param ContainerInterface $container
142
156
* @param string|null $resourceType
143
157
* @param string|null $default
144
158
* @return ContentNegotiatorInterface
145
159
*/
146
- protected function negotiator (?string $ resourceType , ?string $ default ): ContentNegotiatorInterface
160
+ protected function negotiator (
161
+ ContainerInterface $ container ,
162
+ ?string $ resourceType ,
163
+ ?string $ default
164
+ ): ContentNegotiatorInterface
147
165
{
148
- if ($ resourceType && $ negotiator = $ this -> getContainer () ->getContentNegotiatorByResourceType ($ resourceType )) {
166
+ if ($ resourceType && $ negotiator = $ container ->getContentNegotiatorByResourceType ($ resourceType )) {
149
167
return $ negotiator ;
150
168
}
151
169
152
170
if ($ default ) {
153
- return $ this -> getContainer () ->getContentNegotiatorByName ($ default );
171
+ return $ container ->getContentNegotiatorByName ($ default );
154
172
}
155
173
156
174
return $ this ->defaultNegotiator ();
@@ -167,30 +185,15 @@ protected function defaultNegotiator(): ContentNegotiatorInterface
167
185
}
168
186
169
187
/**
170
- * Apply the matched encoding and decoding .
188
+ * Apply the matched codec .
171
189
*
172
- * @param Encoding $encoding
173
- * @param Decoding|null $decoding
190
+ * @param Codec $codec
174
191
*/
175
- protected function matched (Encoding $ encoding , ? Decoding $ decoding ): void
192
+ protected function matched (Codec $ codec ): void
176
193
{
177
- $ codec = $ this ->factory ->createCodec (
178
- $ this ->getContainer (),
179
- $ encoding ,
180
- $ decoding
181
- );
182
-
183
194
$ this ->route ->setCodec ($ codec );
184
195
}
185
196
186
- /**
187
- * @return ContainerInterface
188
- */
189
- protected function getContainer (): ContainerInterface
190
- {
191
- return $ this ->api ->getContainer ();
192
- }
193
-
194
197
/**
195
198
* Will the response contain a specific resource?
196
199
*
0 commit comments