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