11
11
use Http \Message \RequestFactory ;
12
12
use Http \Promise \Promise ;
13
13
use Psr \Http \Message \ResponseInterface ;
14
- use Solarium \Core \Client \Adapter \Guzzle ;
14
+ use Solarium \Core \Client \Adapter \Guzzle as GuzzleAdapter ;
15
15
use Solarium \Core \Client \Endpoint ;
16
+ use Solarium \Core \Client \Request ;
16
17
use Solarium \Core \Client \Response ;
17
18
use Solarium \Core \Plugin \AbstractPlugin ;
18
19
use Solarium \Core \Query \AbstractQuery ;
@@ -36,15 +37,7 @@ class AsyncPlugin extends AbstractPlugin
36
37
*/
37
38
public function queryAsync ($ query , $ endpoint = null )
38
39
{
39
- if (null !== $ this ->asyncClient ) {
40
- $ asyncClient = $ this ->asyncClient ;
41
- $ usingGuzzle = false ;
42
- } else {
43
- $ existingSolariumAdapter = $ this ->client ->getAdapter (false );
44
- $ this ->client ->setAdapter (Guzzle::class);
45
- $ asyncClient = new Guzzle6Adapter ($ this ->client ->getAdapter ()->getGuzzleClient ());
46
- $ usingGuzzle = true ;
47
- }
40
+ $ asyncClient = $ this ->asyncClient ?: new Guzzle6Adapter ($ this ->client ->getAdapter ()->getGuzzleClient ());
48
41
$ request = $ this ->client ->createRequest ($ query );
49
42
$ method = $ request ->getMethod ();
50
43
$ endpoint = $ this ->client ->getEndpoint ($ endpoint );
@@ -64,7 +57,7 @@ public function queryAsync($query, $endpoint = null)
64
57
$ asyncClient = new PluginClient ($ asyncClient , [$ authenticationPlugin ]);
65
58
}
66
59
67
- $ promise = $ asyncClient ->sendAsyncRequest ($ request )
60
+ return $ asyncClient ->sendAsyncRequest ($ request )
68
61
->then (
69
62
function (ResponseInterface $ response ) {
70
63
$ responseHeaders = [
@@ -79,13 +72,6 @@ function (ResponseInterface $response) {
79
72
return new Response ((string ) $ response ->getBody (), $ responseHeaders );
80
73
}
81
74
);
82
-
83
- if ($ usingGuzzle ) {
84
- //set the solarium adapter state back
85
- $ this ->client ->setAdapter ($ existingSolariumAdapter );
86
- }
87
-
88
- return $ promise ;
89
75
}
90
76
91
77
public function setAsyncClient (HttpAsyncClient $ asyncClient )
@@ -101,4 +87,43 @@ public function setRequestFactory(RequestFactory $requestFactory)
101
87
102
88
return $ this ;
103
89
}
90
+
91
+ protected function initPluginType ()
92
+ {
93
+ $ this ->client ->setAdapter (GuzzleAdapter::class);
94
+ }
95
+
96
+ private function getRequestBody (Request $ request )
97
+ {
98
+ if ($ request ->getMethod () !== 'POST ' ) {
99
+ return null ;
100
+ }
101
+
102
+ if ($ request ->getFileUpload ()) {
103
+ return fopen ($ request ->getFileUpload (), 'r ' );
104
+ }
105
+
106
+ return $ request ->getRawData ();
107
+ }
108
+
109
+ private function getRequestHeaders (Request $ request )
110
+ {
111
+ $ headers = [];
112
+ foreach ($ request ->getHeaders () as $ headerLine ) {
113
+ list ($ header , $ value ) = explode (': ' , $ headerLine );
114
+ if ($ header = trim ($ header )) {
115
+ $ headers [$ header ] = trim ($ value );
116
+ }
117
+ }
118
+
119
+ if (!isset ($ headers ['Content-Type ' ])) {
120
+ if ($ request ->getMethod () == Request::METHOD_GET ) {
121
+ $ headers ['Content-Type ' ] = 'application/x-www-form-urlencoded; charset=utf-8 ' ;
122
+ } else {
123
+ $ headers ['Content-Type ' ] = 'application/xml; charset=utf-8 ' ;
124
+ }
125
+ }
126
+
127
+ return $ headers ;
128
+ }
104
129
}
0 commit comments