1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchBundle\Service; |
13
|
|
|
|
14
|
|
|
use Elasticsearch\ClientBuilder; |
15
|
|
|
use ONGR\ElasticsearchBundle\Event\Events; |
16
|
|
|
use ONGR\ElasticsearchBundle\Event\PostCreateManagerEvent; |
17
|
|
|
use ONGR\ElasticsearchBundle\Event\PreCreateManagerEvent; |
18
|
|
|
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector; |
19
|
|
|
use ONGR\ElasticsearchBundle\Result\Converter; |
20
|
|
|
use PackageVersions\Versions; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
23
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Elasticsearch Manager factory class. |
27
|
|
|
*/ |
28
|
|
|
class ManagerFactory |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var MetadataCollector |
32
|
|
|
*/ |
33
|
|
|
private $metadataCollector; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Converter |
37
|
|
|
*/ |
38
|
|
|
private $converter; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var LoggerInterface |
42
|
|
|
*/ |
43
|
|
|
private $logger; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var LoggerInterface |
47
|
|
|
*/ |
48
|
|
|
private $tracer; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var EventDispatcherInterface |
52
|
|
|
*/ |
53
|
|
|
private $eventDispatcher; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var Stopwatch |
57
|
|
|
*/ |
58
|
|
|
private $stopwatch; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param MetadataCollector $metadataCollector Metadata collector service. |
62
|
|
|
* @param Converter $converter Converter service to transform arrays to objects and visa versa. |
63
|
|
|
* @param LoggerInterface $tracer |
64
|
|
|
* @param LoggerInterface $logger |
65
|
|
|
*/ |
66
|
|
|
public function __construct($metadataCollector, $converter, $tracer = null, $logger = null) |
67
|
|
|
{ |
68
|
|
|
$this->metadataCollector = $metadataCollector; |
69
|
|
|
$this->converter = $converter; |
70
|
|
|
$this->tracer = $tracer; |
71
|
|
|
$this->logger = $logger; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
76
|
|
|
*/ |
77
|
|
|
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
78
|
|
|
{ |
79
|
|
|
$this->eventDispatcher = $eventDispatcher; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Stopwatch $stopwatch |
84
|
|
|
*/ |
85
|
|
|
public function setStopwatch(Stopwatch $stopwatch) |
86
|
|
|
{ |
87
|
|
|
$this->stopwatch = $stopwatch; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Factory function to create a manager instance. |
92
|
|
|
* |
93
|
|
|
* @param string $managerName Manager name. |
94
|
|
|
* @param array $connection Connection configuration. |
95
|
|
|
* @param array $analysis Analyzers, filters and tokenizers config. |
96
|
|
|
* @param array $managerConfig Manager configuration. |
97
|
|
|
* |
98
|
|
|
* @return Manager |
99
|
|
|
*/ |
100
|
|
|
public function createManager($managerName, $connection, $analysis, $managerConfig) |
101
|
|
|
{ |
102
|
|
|
$mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']); |
103
|
|
|
|
104
|
|
|
$client = ClientBuilder::create(); |
105
|
|
|
$client->setHosts($connection['hosts']); |
106
|
|
|
|
107
|
|
|
if ($this->tracer) { |
108
|
|
|
$client->setTracer($this->tracer); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($this->logger && $managerConfig['logger']['enabled']) { |
112
|
|
|
$client->setLogger($this->logger); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$indexSettings = [ |
116
|
|
|
'index' => $connection['index_name'], |
117
|
|
|
'body' => array_filter( |
118
|
|
|
[ |
119
|
|
|
'settings' => array_merge( |
120
|
|
|
$connection['settings'], |
121
|
|
|
[ |
122
|
|
|
'analysis' => |
123
|
|
|
$this->metadataCollector->getClientAnalysis($managerConfig['mappings'], $analysis), |
124
|
|
|
] |
125
|
|
|
), |
126
|
|
|
'mappings' => $mappings, |
127
|
|
|
] |
128
|
|
|
), |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
if (class_exists(Versions::class)) { |
132
|
|
|
$elasticSearchVersion = explode('@', Versions::getVersion('ongr/elasticsearch-dsl'))[0]; |
133
|
|
|
if (0 === strpos($elasticSearchVersion, 'v')) { |
134
|
|
|
$elasticSearchVersion = substr($elasticSearchVersion, 1); |
135
|
|
|
} |
136
|
|
|
if (version_compare($elasticSearchVersion, '7.0.0', '>=')) { |
137
|
|
|
$indexSettings['include_type_name'] = true; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->eventDispatcher && |
142
|
|
|
$this->eventDispatcher->dispatch( |
143
|
|
|
Events::PRE_MANAGER_CREATE, |
144
|
|
|
$preCreateEvent = new PreCreateManagerEvent($client, $indexSettings) |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$manager = new Manager( |
148
|
|
|
$managerName, |
149
|
|
|
$managerConfig, |
150
|
|
|
$client->build(), |
151
|
|
|
$preCreateEvent->getIndexSettings(), |
|
|
|
|
152
|
|
|
$this->metadataCollector, |
153
|
|
|
$this->converter |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
if (isset($this->stopwatch)) { |
157
|
|
|
$manager->setStopwatch($this->stopwatch); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$manager->setCommitMode($managerConfig['commit_mode']); |
161
|
|
|
$manager->setEventDispatcher($this->eventDispatcher); |
162
|
|
|
$manager->setCommitMode($managerConfig['commit_mode']); |
163
|
|
|
$manager->setBulkCommitSize($managerConfig['bulk_size']); |
164
|
|
|
|
165
|
|
|
$this->eventDispatcher && |
166
|
|
|
$this->eventDispatcher->dispatch(Events::POST_MANAGER_CREATE, new PostCreateManagerEvent($manager)); |
167
|
|
|
|
168
|
|
|
return $manager; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: