|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace As3\Bundle\ModlrBundle\DependencyInjection\ServiceLoader; |
|
4
|
|
|
|
|
5
|
|
|
use As3\Bundle\ModlrBundle\DependencyInjection\Utility; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Loads search client services. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Jacob Bare <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class SearchClients implements ServiceLoaderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Creates the Elastic search client service definition. |
|
19
|
|
|
* Will also load support services. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $clientName |
|
22
|
|
|
* @param array $clientConfig |
|
23
|
|
|
* @param ContainerBuilder $container |
|
24
|
|
|
* @return Definition |
|
25
|
|
|
*/ |
|
26
|
|
|
private function createElasticClient($clientName, array $clientConfig, ContainerBuilder $container) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
// Storage metadata |
|
29
|
|
|
$smfName = sprintf('%s.metadata', $clientName); |
|
30
|
|
|
$definition = new Definition( |
|
31
|
|
|
Utility::getLibraryClass('Search\Elastic\StorageMetadataFactory') |
|
32
|
|
|
); |
|
33
|
|
|
$definition->setPublic(false); |
|
34
|
|
|
$container->setDefinition($smfName, $definition); |
|
35
|
|
|
|
|
36
|
|
|
// Client |
|
37
|
|
|
return new Definition( |
|
38
|
|
|
Utility::getLibraryClass('Search\Elastic\Client'), |
|
39
|
|
|
[new Reference($smfName)] |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
public function load(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$managerDef = $container->getDefinition(Utility::getAliasedName('storage_manager')); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($config['search_clients'] as $name => $clientConfig) { |
|
51
|
|
|
$clientName = Utility::getAliasedName(sprintf('search_client.%s', $name)); |
|
52
|
|
|
if (isset($clientConfig['service'])) { |
|
53
|
|
|
// Custom search client. |
|
54
|
|
|
$container->setAlias($clientName, Utility::cleanServiceName($clientConfig['service'])); |
|
55
|
|
|
} else { |
|
56
|
|
|
// Built-in client. |
|
57
|
|
|
switch ($clientConfig['type']) { |
|
58
|
|
|
case 'elastic': |
|
59
|
|
|
$definition = $this->createElasticClient($clientName, $clientConfig, $container); |
|
60
|
|
|
break; |
|
61
|
|
|
default: |
|
62
|
|
|
throw new \RuntimeException(sprintf('The search client type "%s" is currently not supported.', $clientConfig['type'])); |
|
63
|
|
|
} |
|
64
|
|
|
$container->setDefinition($clientName, $definition); |
|
65
|
|
|
} |
|
66
|
|
|
$managerDef->addMethodCall('addSearchClient', [new Reference($clientName)]); |
|
67
|
|
|
} |
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.