-
Notifications
You must be signed in to change notification settings - Fork 189
/
MappingPass.php
175 lines (149 loc) · 6.18 KB
/
MappingPass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchBundle\DependencyInjection\Compiler;
use ONGR\ElasticsearchBundle\Annotation\Index;
use ONGR\ElasticsearchBundle\DependencyInjection\Configuration;
use ONGR\ElasticsearchBundle\Mapping\Converter;
use ONGR\ElasticsearchBundle\Mapping\DocumentParser;
use ONGR\ElasticsearchBundle\Mapping\IndexSettings;
use ONGR\ElasticsearchBundle\Service\IndexService;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
class MappingPass implements CompilerPassInterface
{
/**
* @var array
*/
private $indexes = [];
/**
* @var string
*/
private $defaultIndex = null;
public function process(ContainerBuilder $container)
{
$kernelDir = $container->getParameter('kernel.project_dir');
foreach ($container->getParameter(Configuration::ONGR_SOURCE_DIR) as $dir) {
$this->handleDirectoryMapping($container, $kernelDir . $dir);
}
$container->setParameter(Configuration::ONGR_INDEXES, $this->indexes);
$container->setParameter(
Configuration::ONGR_DEFAULT_INDEX,
$this->defaultIndex ?? current(array_keys($this->indexes))
);
}
/**
* @param ContainerBuilder $container
* @param string $dir
*
* @throws \ReflectionException
*/
private function handleDirectoryMapping(ContainerBuilder $container, string $dir): void
{
/** @var DocumentParser $parser */
$parser = $container->get(DocumentParser::class);
$indexesOverride = $container->getParameter(Configuration::ONGR_INDEXES_OVERRIDE);
$converterDefinition = $container->getDefinition(Converter::class);
foreach ($this->getNamespaces($dir) as $namespace) {
$class = new \ReflectionClass($namespace);
if (isset($indexesOverride[$namespace]['alias']) && $indexesOverride[$namespace]['alias']) {
$indexAlias = $indexesOverride[$namespace]['alias'];
} else {
$indexAlias = $parser->getIndexAliasName($class);
}
/** @var Index $document */
$document = $parser->getIndexAnnotation($class);
$indexMetadata = $parser->getIndexMetadata($class);
if (!empty($indexMetadata)) {
$indexMetadata['settings'] = array_filter(array_merge_recursive(
$indexMetadata['settings'] ?? [],
[
'number_of_replicas' => $document->numberOfReplicas,
'number_of_shards' => $document->numberOfShards,
],
$indexesOverride[$namespace]['settings'] ?? []
));
$indexSettings = new Definition(
IndexSettings::class,
[
$namespace,
$indexAlias,
$indexAlias,
$indexMetadata,
$indexesOverride[$namespace]['hosts'] ?? $document->hosts,
$indexesOverride[$namespace]['default'] ?? $document->default,
$indexesOverride[$namespace]['type'] ?? $document->typeName
]
);
$indexServiceDefinition = new Definition(IndexService::class, [
$namespace,
$converterDefinition,
$container->getDefinition('event_dispatcher'),
$indexSettings,
$container->getParameter(Configuration::ONGR_PROFILER_CONFIG)
? $container->getDefinition('ongr.esb.tracer') : null
]);
$indexServiceDefinition->setPublic(true);
$converterDefinition->addMethodCall(
'addClassMetadata',
[
$namespace,
$parser->getPropertyMetadata($class)
]
);
$container->setDefinition($namespace, $indexServiceDefinition);
$this->indexes[$indexAlias] = $namespace;
$isCurrentIndexDefault = $parser->isDefaultIndex($class);
if ($this->defaultIndex && $isCurrentIndexDefault) {
throw new \RuntimeException(
sprintf(
'Only one index can be set as default. We found 2 indexes as default ones `%s` and `%s`',
$this->defaultIndex,
$indexAlias
)
);
}
if ($isCurrentIndexDefault) {
$this->defaultIndex = $indexAlias;
}
}
}
}
private function getNamespaces($directory): array
{
if (!is_dir($directory)) {
return [];
}
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
$files = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
$documents = [];
foreach ($files as $file => $v) {
$documents[] = $this->getFullNamespace($file) . '\\' . $this->getClassname($file);
}
return $documents;
}
private function getFullNamespace($filename)
{
$lines = preg_grep('/^namespace /', file($filename));
$namespaceLine = array_shift($lines);
$match = array();
preg_match('/^namespace (.*);$/', $namespaceLine, $match);
$fullNamespace = array_pop($match);
return $fullNamespace;
}
private function getClassname($filename)
{
$directoriesAndFilename = explode('/', $filename);
$filename = array_pop($directoriesAndFilename);
$nameAndExtension = explode('.', $filename);
$className = array_shift($nameAndExtension);
return $className;
}
}