15
15
use Solarium \Core \Client \Endpoint ;
16
16
use Solarium \Core \Client \Request ;
17
17
use Solarium \Core \Client \Response ;
18
+ use Solarium \Core \Event \Events ;
19
+ use Solarium \Core \Event \PostExecuteRequest as PostExecuteRequestEvent ;
20
+ use Solarium \Core \Event \PreExecuteRequest as PreExecuteRequestEvent ;
18
21
use Solarium \Core \Plugin \AbstractPlugin ;
19
22
use Solarium \Core \Query \AbstractQuery ;
23
+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
20
24
21
25
class SolariumAsyncPlugin extends AbstractPlugin
22
26
{
@@ -30,6 +34,12 @@ class SolariumAsyncPlugin extends AbstractPlugin
30
34
*/
31
35
private $ requestFactory ;
32
36
37
+ /**
38
+ * @var EventDispatcherInterface|null
39
+ */
40
+ private $ eventDispatcher ;
41
+
42
+
33
43
/**
34
44
* @param AbstractQuery $query
35
45
* @param string|Endpoint|null $endpoint
@@ -38,16 +48,23 @@ class SolariumAsyncPlugin extends AbstractPlugin
38
48
public function queryAsync ($ query , $ endpoint = null )
39
49
{
40
50
$ asyncClient = $ this ->asyncClient ?: new Guzzle6Adapter ($ this ->client ->getAdapter ()->getGuzzleClient ());
41
- $ request = $ this ->client ->createRequest ($ query );
42
- $ method = $ request ->getMethod ();
51
+ $ solariumRequest = $ this ->client ->createRequest ($ query );
52
+ $ method = $ solariumRequest ->getMethod ();
43
53
$ endpoint = $ this ->client ->getEndpoint ($ endpoint );
44
54
55
+ if ($ this ->eventDispatcher ) {
56
+ $ this ->eventDispatcher ->dispatch (
57
+ Events::PRE_EXECUTE_REQUEST ,
58
+ new PreExecuteRequestEvent ($ solariumRequest , $ endpoint )
59
+ );
60
+ }
61
+
45
62
$ requestFactory = $ this ->requestFactory ?: new GuzzleMessageFactory ();
46
63
$ request = $ requestFactory ->createRequest (
47
64
$ method ,
48
- $ endpoint ->getBaseUri ().$ request ->getUri (),
49
- $ this ->getRequestHeaders ($ request ),
50
- $ this ->getRequestBody ($ request )
65
+ $ endpoint ->getBaseUri ().$ solariumRequest ->getUri (),
66
+ $ this ->getRequestHeaders ($ solariumRequest ),
67
+ $ this ->getRequestBody ($ solariumRequest )
51
68
);
52
69
53
70
$ authData = $ endpoint ->getAuthentication ();
@@ -57,9 +74,10 @@ public function queryAsync($query, $endpoint = null)
57
74
$ asyncClient = new PluginClient ($ asyncClient , [$ authenticationPlugin ]);
58
75
}
59
76
77
+
60
78
return $ asyncClient ->sendAsyncRequest ($ request )
61
79
->then (
62
- function (ResponseInterface $ response ) {
80
+ function (ResponseInterface $ response ) use ( $ solariumRequest , $ endpoint ) {
63
81
$ responseHeaders = [
64
82
"HTTP/ {$ response ->getProtocolVersion ()} {$ response ->getStatusCode ()} "
65
83
. $ response ->getReasonPhrase (),
@@ -69,7 +87,16 @@ function (ResponseInterface $response) {
69
87
$ responseHeaders [] = "{$ key }: " . implode (', ' , $ value );
70
88
}
71
89
72
- return new Response ((string ) $ response ->getBody (), $ responseHeaders );
90
+ $ response = new Response ((string ) $ response ->getBody (), $ responseHeaders );
91
+
92
+ if ($ this ->eventDispatcher ) {
93
+ $ this ->eventDispatcher ->dispatch (
94
+ Events::POST_EXECUTE_REQUEST ,
95
+ new PostExecuteRequestEvent ($ solariumRequest , $ endpoint , $ response )
96
+ );
97
+ }
98
+
99
+ return $ response ;
73
100
}
74
101
);
75
102
}
@@ -88,6 +115,13 @@ public function setRequestFactory(RequestFactory $requestFactory)
88
115
return $ this ;
89
116
}
90
117
118
+ public function setEventDispatcher (EventDispatcherInterface $ eventDispatcher )
119
+ {
120
+ $ this ->eventDispatcher = $ eventDispatcher ;
121
+
122
+ return $ this ;
123
+ }
124
+
91
125
protected function initPluginType ()
92
126
{
93
127
$ this ->client ->setAdapter (GuzzleAdapter::class);
0 commit comments