Skip to content

Commit 1ed04c7

Browse files
committed
Allow processing search results as they come in via handlers. This gives more granular control over processing referral results / entry results and lets library users process results as they come in instead of waiting until the end of the result set.
1 parent ee276ba commit 1ed04c7

File tree

4 files changed

+133
-9
lines changed

4 files changed

+133
-9
lines changed

src/FreeDSx/Ldap/Operation/Request/SearchRequest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use FreeDSx\Ldap\Exception\RuntimeException;
2727
use FreeDSx\Ldap\Protocol\Factory\FilterFactory;
2828
use FreeDSx\Ldap\Search\Filter\FilterInterface;
29+
use FreeDSx\Ldap\Search\Handler\EntryHandlerInterface;
30+
use FreeDSx\Ldap\Search\Handler\ReferralHandlerInterface;
2931
use function array_map;
3032

3133
/**
@@ -106,6 +108,10 @@ class SearchRequest implements RequestInterface
106108
*/
107109
private array $attributes = [];
108110

111+
private ?EntryHandlerInterface $entryHandler = null;
112+
113+
private ?ReferralHandlerInterface $referralHandler = null;
114+
109115
public function __construct(
110116
FilterInterface $filter,
111117
Attribute|string ...$attributes
@@ -282,6 +288,30 @@ public function setFilter(FilterInterface $filter): self
282288
return $this;
283289
}
284290

291+
public function getEntryHandler(): ?EntryHandlerInterface
292+
{
293+
return $this->entryHandler;
294+
}
295+
296+
public function useEntryHandler(?EntryHandlerInterface $entryHandler): self
297+
{
298+
$this->entryHandler = $entryHandler;
299+
300+
return $this;
301+
}
302+
303+
public function getReferralHandler(): ?ReferralHandlerInterface
304+
{
305+
return $this->referralHandler;
306+
}
307+
308+
public function useReferralHandler(?ReferralHandlerInterface $referralHandler): self
309+
{
310+
$this->referralHandler = $referralHandler;
311+
312+
return $this;
313+
}
314+
285315
/**
286316
* {@inheritDoc}
287317
*

src/FreeDSx/Ldap/Protocol/ClientProtocolHandler/ClientSearchHandler.php

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use FreeDSx\Ldap\Protocol\LdapMessageRequest;
2828
use FreeDSx\Ldap\Protocol\LdapMessageResponse;
2929
use FreeDSx\Ldap\Protocol\Queue\ClientQueue;
30+
use FreeDSx\Ldap\Search\Handler\EntryHandlerInterface;
31+
use FreeDSx\Ldap\Search\Handler\ReferralHandlerInterface;
3032
use FreeDSx\Ldap\Search\Result\EntryResult;
3133
use FreeDSx\Ldap\Search\Result\ReferralResult;
3234
use FreeDSx\Socket\Exception\ConnectionException;
@@ -69,6 +71,63 @@ public function handleResponse(
6971
ClientQueue $queue,
7072
ClientOptions $options,
7173
): ?LdapMessageResponse {
74+
/** @var SearchRequest $searchRequest */
75+
$searchRequest = $messageTo->getRequest();
76+
77+
$entryHandler = $searchRequest->getEntryHandler();
78+
$referralHandler = $searchRequest->getReferralHandler();
79+
80+
if ($entryHandler || $referralHandler) {
81+
$finalResponse = $this->searchWithHandlers(
82+
$messageFrom,
83+
$messageTo,
84+
$entryHandler,
85+
$referralHandler,
86+
$queue,
87+
);
88+
} else {
89+
$finalResponse = $this->searchWithoutHandlers(
90+
$messageFrom,
91+
$messageTo,
92+
$queue,
93+
);
94+
}
95+
96+
return parent::handleResponse(
97+
$messageTo,
98+
$finalResponse,
99+
$queue,
100+
$options
101+
);
102+
}
103+
104+
private function searchWithHandlers(
105+
LdapMessageResponse $messageFrom,
106+
LdapMessageRequest $messageTo,
107+
?EntryHandlerInterface $entryHandler,
108+
?ReferralHandlerInterface $referralHandler,
109+
ClientQueue $queue,
110+
): LdapMessageResponse {
111+
while (!$messageFrom->getResponse() instanceof SearchResultDone) {
112+
$response = $messageFrom->getResponse();
113+
114+
if ($response instanceof SearchResultEntry) {
115+
$entryHandler?->handleEntry(new EntryResult($messageFrom));
116+
} elseif ($response instanceof SearchResultReference) {
117+
$referralHandler?->handleReferral(new ReferralResult($messageFrom));
118+
}
119+
120+
$messageFrom = $queue->getMessage($messageTo->getMessageId());
121+
}
122+
123+
return $messageFrom;
124+
}
125+
126+
private function searchWithoutHandlers(
127+
LdapMessageResponse $messageFrom,
128+
LdapMessageRequest $messageTo,
129+
ClientQueue $queue,
130+
): LdapMessageResponse {
72131
$entryResults = [];
73132
$referralResults = [];
74133

@@ -86,22 +145,15 @@ public function handleResponse(
86145

87146
$ldapResult = $messageFrom->getResponse();
88147

89-
$finalResponse = new LdapMessageResponse(
148+
return new LdapMessageResponse(
90149
$messageFrom->getMessageId(),
91150
new SearchResponse(
92151
$ldapResult,
93152
$entryResults,
94153
$referralResults,
95154
),
96155
...$messageFrom->controls()
97-
->toArray()
98-
);
99-
100-
return parent::handleResponse(
101-
$messageTo,
102-
$finalResponse,
103-
$queue,
104-
$options
156+
->toArray()
105157
);
106158
}
107159
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the FreeDSx LDAP package.
5+
*
6+
* (c) Chad Sikorra <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace FreeDSx\Ldap\Search\Handler;
15+
16+
use FreeDSx\Ldap\Search\Result\EntryResult;
17+
18+
interface EntryHandlerInterface
19+
{
20+
public function handleEntry(EntryResult $entryResult): void;
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the FreeDSx LDAP package.
5+
*
6+
* (c) Chad Sikorra <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace FreeDSx\Ldap\Search\Handler;
15+
16+
use FreeDSx\Ldap\Search\Result\ReferralResult;
17+
18+
interface ReferralHandlerInterface
19+
{
20+
public function handleReferral(ReferralResult $referralResult): void;
21+
}

0 commit comments

Comments
 (0)