1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElasticsearchModule\Service; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use Elasticsearch\Connections\ConnectionFactoryInterface; |
7
|
|
|
use Interop\Container\ContainerInterface; |
8
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
9
|
|
|
use Zend\Stdlib\ArrayObject as ZendArrayObject; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Pedro Alves <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class ConnectionFactory extends AbstractFactory |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* {@inheritDoc} |
19
|
|
|
*/ |
20
|
22 |
|
protected function create(ContainerInterface $container, $config) |
21
|
|
|
{ |
22
|
22 |
|
$factoryName = $config['factory']; |
23
|
22 |
|
$handler = $container->get($config['handler']); |
24
|
22 |
|
$loggers = $container->get($config['loggers']); |
25
|
22 |
|
$serializer = $this->getServiceOrClassObject($container, $config['serializer']); |
26
|
22 |
|
$params = $this->getConnectionParametersFromConfiguration($config); |
27
|
22 |
|
$connectionFactory = new $factoryName($handler, $params, $serializer, $loggers['logger'], $loggers['tracer']); |
28
|
|
|
|
29
|
22 |
|
if (!$connectionFactory instanceof ConnectionFactoryInterface) { |
30
|
1 |
|
throw new ServiceNotCreatedException(sprintf( |
31
|
1 |
|
"The '%s' class does not implements %s", |
32
|
1 |
|
$factoryName, |
33
|
|
|
ConnectionFactoryInterface::class |
34
|
1 |
|
)); |
35
|
|
|
} |
36
|
21 |
|
return $connectionFactory; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
22 |
|
public function getServiceType() |
43
|
|
|
{ |
44
|
22 |
|
return 'connection_factory'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array|ArrayObject|ZendArrayObject $config |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
22 |
|
private function getConnectionParametersFromConfiguration($config) |
52
|
|
|
{ |
53
|
22 |
|
return (isset($config['params']) && is_array($config['params'])) ? $config['params'] : []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|