1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Portiny\Elasticsearch\Adapter\Nette\DI; |
4
|
|
|
|
5
|
|
|
use Elastica\Client; |
6
|
|
|
use Nette\DI\CompilerExtension; |
7
|
|
|
use Nette\PhpGenerator\ClassType; |
8
|
|
|
use Portiny\Elasticsearch\Adapter\Nette\Tracy\ElasticsearchPanel; |
9
|
|
|
|
10
|
|
|
class ElasticsearchExtension extends CompilerExtension |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
public const ELASTICSEARCH_PANEL = ElasticsearchPanel::class; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $defaults = [ |
21
|
|
|
'debug' => '%debugMode%', |
22
|
|
|
'clientClassName' => Client::class, |
23
|
|
|
'connections' => [ |
24
|
|
|
[ |
25
|
|
|
'host' => 'localhost', |
26
|
|
|
'port' => 9200, |
27
|
|
|
], |
28
|
|
|
], |
29
|
|
|
'path' => null, |
30
|
|
|
'url' => null, |
31
|
|
|
'proxy' => null, |
32
|
|
|
'transport' => null, |
33
|
|
|
'persistent' => true, |
34
|
|
|
'timeout' => null, |
35
|
|
|
'roundRobin' => false, |
36
|
|
|
'log' => false, |
37
|
|
|
'retryOnConflict' => 0, |
38
|
|
|
'bigintConversion' => false, |
39
|
|
|
'username' => null, |
40
|
|
|
'password' => null, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
1 |
|
public function loadConfiguration(): void |
47
|
|
|
{ |
48
|
1 |
|
$config = $this->validateConfig($this->defaults); |
49
|
1 |
|
$builder = $this->getContainerBuilder(); |
50
|
|
|
|
51
|
1 |
|
if (count($config['connections']) > 1) { |
52
|
1 |
|
unset($config['connections'][0]); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
$builder->addDefinition($this->prefix('client')) |
56
|
1 |
|
->setType($config['clientClassName']) |
57
|
1 |
|
->setArguments([[ |
58
|
1 |
|
'connections' => $config['connections'], |
59
|
1 |
|
'path' => $config['path'], |
60
|
1 |
|
'url' => $config['url'], |
61
|
1 |
|
'proxy' => $config['proxy'], |
62
|
1 |
|
'transport' => $config['transport'], |
63
|
1 |
|
'persistent' => $config['persistent'], |
64
|
1 |
|
'timeout' => $config['timeout'], |
65
|
1 |
|
'roundRobin' => $config['roundRobin'], |
66
|
1 |
|
'log' => $config['log'], |
67
|
1 |
|
'retryOnConflict' => $config['retryOnConflict'], |
68
|
1 |
|
'bigintConversion' => $config['bigintConversion'], |
69
|
1 |
|
'username' => $config['username'], |
70
|
1 |
|
'password' => $config['password'], |
71
|
|
|
], null, null]) |
72
|
1 |
|
->setAutowired(true); |
73
|
|
|
|
74
|
1 |
|
if ($config['debug'] === true) { |
75
|
|
|
$builder->addDefinition($this->prefix('diagnosticsPanel')) |
76
|
|
|
->setType(self::ELASTICSEARCH_PANEL); |
77
|
|
|
} |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
1 |
|
public function afterCompile(ClassType $classType): void |
84
|
|
|
{ |
85
|
1 |
|
$config = $this->validateConfig($this->defaults); |
86
|
|
|
|
87
|
1 |
|
if ($config['debug'] === true) { |
88
|
|
|
$initialize = $classType->methods['initialize']; |
89
|
|
|
$initialize->addBody('$this->getByType(\'' . self::ELASTICSEARCH_PANEL . '\')->bindToBar();'); |
90
|
|
|
} |
91
|
1 |
|
} |
92
|
|
|
} |
93
|
|
|
|