|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ElasticsearchModule\Service; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayObject; |
|
6
|
|
|
use Elasticsearch\ClientBuilder; |
|
7
|
|
|
use Interop\Container\ContainerInterface; |
|
8
|
|
|
use Psr\Container\ContainerInterface as ContainerInterface2; |
|
9
|
|
|
use Zend\Stdlib\ArrayObject as ZendArrayObject; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Pedro Alves <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class HandlerFactory extends AbstractFactory |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritDoc} |
|
19
|
|
|
*/ |
|
20
|
32 |
|
protected function create(ContainerInterface $container, $config) |
|
21
|
|
|
{ |
|
22
|
32 |
|
return ClientBuilder::defaultHandler( |
|
23
|
32 |
|
$this->getHandlerParams($container, $config, 'multi_handler', 'handle_factory'), |
|
24
|
32 |
|
$this->getHandlerParams($container, $config, 'single_handler', 'factory') |
|
25
|
32 |
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
33 |
|
public function getServiceType() |
|
32
|
|
|
{ |
|
33
|
33 |
|
return 'handler'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param ContainerInterface2 $container |
|
38
|
|
|
* @param array|ArrayObject|ZendArrayObject $handlerConfig |
|
39
|
|
|
* @param string $paramKey |
|
40
|
|
|
* @param string $factoryKey |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
32 |
|
private function getHandlerParams(ContainerInterface $container, $handlerConfig, $paramKey, $factoryKey) |
|
44
|
|
|
{ |
|
45
|
32 |
|
if (!isset($handlerConfig['params'][$paramKey])) { |
|
46
|
8 |
|
return []; |
|
47
|
|
|
} |
|
48
|
31 |
|
return $this->createFactoryInConfig( |
|
49
|
31 |
|
$container, |
|
50
|
31 |
|
$handlerConfig['params'][$paramKey], |
|
51
|
|
|
$factoryKey |
|
52
|
31 |
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param ContainerInterface $container |
|
57
|
|
|
* @param array $handlerParams |
|
58
|
|
|
* @param string $factoryKey |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
31 |
|
private function createFactoryInConfig(ContainerInterface $container, $handlerParams, $factoryKey) |
|
62
|
|
|
{ |
|
63
|
31 |
|
if ($this->hasHandlerFactoryOption($handlerParams, $factoryKey)) { |
|
64
|
4 |
|
$serviceOrClass = $handlerParams[$factoryKey]; |
|
65
|
4 |
|
$handlerParams[$factoryKey] = $this->getServiceOrClassObject($container, $serviceOrClass); |
|
66
|
|
|
|
|
67
|
4 |
|
if (!is_callable($handlerParams[$factoryKey])) { |
|
68
|
2 |
|
unset($handlerParams[$factoryKey]); |
|
69
|
2 |
|
} |
|
70
|
4 |
|
} |
|
71
|
31 |
|
return $handlerParams; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $handlerParams |
|
76
|
|
|
* @param string $factoryKey |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
31 |
|
private function hasHandlerFactoryOption($handlerParams, $factoryKey) |
|
80
|
|
|
{ |
|
81
|
31 |
|
return isset($handlerParams[$factoryKey]) && is_string($handlerParams[$factoryKey]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|