1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace AsyncSockets\RequestExecutor\Pipeline; |
12
|
|
|
|
13
|
|
|
use AsyncSockets\Operation\OperationInterface; |
14
|
|
|
use AsyncSockets\RequestExecutor\EventHandlerInterface; |
15
|
|
|
use AsyncSockets\RequestExecutor\ExecutionContext; |
16
|
|
|
use AsyncSockets\RequestExecutor\IoHandlerInterface; |
17
|
|
|
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor; |
18
|
|
|
use AsyncSockets\RequestExecutor\RequestExecutorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class DelegatingIoHandler |
22
|
|
|
*/ |
23
|
|
|
class DelegatingIoHandler implements IoHandlerInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* List of nested handlers |
27
|
|
|
* |
28
|
|
|
* @var IoHandlerInterface[] |
29
|
|
|
*/ |
30
|
|
|
private $handlers; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* DelegatingIoHandler constructor. |
34
|
|
|
* |
35
|
|
|
* @param IoHandlerInterface[] $handlers List of nested handlers |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $handlers) |
38
|
|
|
{ |
39
|
|
|
$this->handlers = $handlers; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
|
|
public function supports(OperationInterface $operation) |
46
|
|
|
{ |
47
|
|
|
return $this->getHandler($operation) !== null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
53
|
|
|
public function handle( |
54
|
|
|
OperationInterface $operation, |
55
|
|
|
RequestDescriptor $descriptor, |
56
|
|
|
RequestExecutorInterface $executor, |
57
|
|
|
EventHandlerInterface $eventHandler, |
58
|
|
|
ExecutionContext $executionContext |
59
|
|
|
) { |
60
|
|
|
$handler = $this->getHandler($operation); |
61
|
|
|
return $handler ? |
62
|
|
|
$handler->handle($operation, $descriptor, $executor, $eventHandler, $executionContext) : |
63
|
|
|
null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return handler for given operation if there is any |
68
|
|
|
* |
69
|
|
|
* @param OperationInterface $operation Operation to get handler for |
70
|
|
|
* |
71
|
|
|
* @return IoHandlerInterface|null |
72
|
|
|
*/ |
73
|
|
|
private function getHandler(OperationInterface $operation) |
74
|
|
|
{ |
75
|
|
|
foreach ($this->handlers as $handler) { |
76
|
|
|
if ($handler->supports($operation)) { |
77
|
|
|
return $handler; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|