Skip to content

Commit 423ae4b

Browse files
author
Douglas Greenshields
authored
Merge pull request shieldo#1 from usemarkup/feat/support-solr-datacollector
feat: support Solarium data collector via dispatching events
2 parents 57eff0a + 812fc66 commit 423ae4b

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"php-http/client-common": "^1"
2222
},
2323
"require-dev": {
24-
"phpunit/phpunit": "~4"
24+
"phpunit/phpunit": "~4",
25+
"symfony/event-dispatcher": "^3.4 || ^4.0"
2526
},
2627
"conflict": {
2728
"php-http/message-factory": "<1.0.2"

src/SolariumAsyncPlugin.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
use Solarium\Core\Client\Endpoint;
1616
use Solarium\Core\Client\Request;
1717
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;
1821
use Solarium\Core\Plugin\AbstractPlugin;
1922
use Solarium\Core\Query\AbstractQuery;
23+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2024

2125
class SolariumAsyncPlugin extends AbstractPlugin
2226
{
@@ -30,6 +34,12 @@ class SolariumAsyncPlugin extends AbstractPlugin
3034
*/
3135
private $requestFactory;
3236

37+
/**
38+
* @var EventDispatcherInterface|null
39+
*/
40+
private $eventDispatcher;
41+
42+
3343
/**
3444
* @param AbstractQuery $query
3545
* @param string|Endpoint|null $endpoint
@@ -38,16 +48,23 @@ class SolariumAsyncPlugin extends AbstractPlugin
3848
public function queryAsync($query, $endpoint = null)
3949
{
4050
$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();
4353
$endpoint = $this->client->getEndpoint($endpoint);
4454

55+
if ($this->eventDispatcher) {
56+
$this->eventDispatcher->dispatch(
57+
Events::PRE_EXECUTE_REQUEST,
58+
new PreExecuteRequestEvent($solariumRequest, $endpoint)
59+
);
60+
}
61+
4562
$requestFactory = $this->requestFactory ?: new GuzzleMessageFactory();
4663
$request = $requestFactory->createRequest(
4764
$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)
5168
);
5269

5370
$authData = $endpoint->getAuthentication();
@@ -57,9 +74,10 @@ public function queryAsync($query, $endpoint = null)
5774
$asyncClient = new PluginClient($asyncClient, [$authenticationPlugin]);
5875
}
5976

77+
6078
return $asyncClient->sendAsyncRequest($request)
6179
->then(
62-
function (ResponseInterface $response) {
80+
function (ResponseInterface $response) use ($solariumRequest, $endpoint) {
6381
$responseHeaders = [
6482
"HTTP/{$response->getProtocolVersion()} {$response->getStatusCode()} "
6583
. $response->getReasonPhrase(),
@@ -69,7 +87,16 @@ function (ResponseInterface $response) {
6987
$responseHeaders[] = "{$key}: " . implode(', ', $value);
7088
}
7189

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;
73100
}
74101
);
75102
}
@@ -88,6 +115,13 @@ public function setRequestFactory(RequestFactory $requestFactory)
88115
return $this;
89116
}
90117

118+
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
119+
{
120+
$this->eventDispatcher = $eventDispatcher;
121+
122+
return $this;
123+
}
124+
91125
protected function initPluginType()
92126
{
93127
$this->client->setAdapter(GuzzleAdapter::class);

0 commit comments

Comments
 (0)