|
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\DependencyInjection\Compiler; |
|
13
|
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchBundle\DependencyInjection\Configuration; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
19
|
|
|
use Symfony\Component\Finder\Finder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Compiles elastic search data. |
|
23
|
|
|
*/ |
|
24
|
|
|
class MappingPass implements CompilerPassInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
* @throws \Exception |
|
29
|
|
|
*/ |
|
30
|
|
|
public function process(ContainerBuilder $container) |
|
31
|
|
|
{ |
|
32
|
|
|
$analysis = $container->getParameter(Configuration::ONGR_ANALYSIS_CONFIG); |
|
33
|
|
|
$additionalDirs = $container->getParameter(Configuration::ONGR_INCLUDE_DIR_CONFIG); |
|
34
|
|
|
|
|
35
|
|
|
$collector = $container->get('es.metadata_collector'); |
|
36
|
|
|
|
|
37
|
|
|
$kernelProjectDir = $container->getParameter('kernel.project_dir'); |
|
38
|
|
|
|
|
39
|
|
|
$namespaces = array(); |
|
40
|
|
|
|
|
41
|
|
|
$finder = new Finder(); |
|
42
|
|
|
$projectFiles = $finder->files()->in( |
|
43
|
|
|
array_merge([ |
|
44
|
|
|
$kernelProjectDir . '/src' |
|
45
|
|
|
], $additionalDirs) |
|
46
|
|
|
)->name('*.php'); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($projectFiles as $file) { |
|
49
|
|
|
$namespaces[] = $this->getFullNamespace($file) . '\\' . $this->getClassname($file); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$indexDefinition = new Definition( |
|
53
|
|
|
'ONGR\ElasticsearchBundle\Service\IndexService', |
|
54
|
|
|
[] |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$container->autowire() |
|
58
|
|
|
|
|
59
|
|
|
} |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
private function getFullNamespace($filename) { |
|
63
|
|
|
$lines = file($filename); |
|
64
|
|
|
$namespaceLine = array_shift(preg_grep('/^namespace /', $lines)); |
|
65
|
|
|
$match = array(); |
|
66
|
|
|
preg_match('/^namespace (.*);$/', $namespaceLine, $match); |
|
67
|
|
|
$fullNamespace = array_pop($match); |
|
68
|
|
|
|
|
69
|
|
|
return $fullNamespace; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function getClassname($filename) { |
|
73
|
|
|
$directoriesAndFilename = explode('/', $filename); |
|
74
|
|
|
$filename = array_pop($directoriesAndFilename); |
|
75
|
|
|
$nameAndExtension = explode('.', $filename); |
|
76
|
|
|
$className = array_shift($nameAndExtension); |
|
77
|
|
|
|
|
78
|
|
|
return $className; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
// |
|
87
|
|
|
// |
|
88
|
|
|
// foreach ($managers as $managerName => $manager) { |
|
89
|
|
|
// $connection = $manager['index']; |
|
90
|
|
|
// $managerName = strtolower($managerName); |
|
91
|
|
|
// |
|
92
|
|
|
// $managerDefinition = new Definition( |
|
93
|
|
|
// 'ONGR\ElasticsearchBundle\Service\Manager', |
|
94
|
|
|
// [ |
|
95
|
|
|
// $managerName, |
|
96
|
|
|
// $connection, |
|
97
|
|
|
// $analysis, |
|
98
|
|
|
// $manager, |
|
99
|
|
|
// ] |
|
100
|
|
|
// ); |
|
101
|
|
|
// $managerDefinition->setFactory( |
|
102
|
|
|
// [ |
|
103
|
|
|
// new Reference('es.manager_factory'), |
|
104
|
|
|
// 'createManager', |
|
105
|
|
|
// ] |
|
106
|
|
|
// ); |
|
107
|
|
|
// |
|
108
|
|
|
// $container->setDefinition(sprintf('es.manager.%s', $managerName), $managerDefinition); |
|
109
|
|
|
// |
|
110
|
|
|
// // Make es.manager.default as es.manager service. |
|
111
|
|
|
// if ($managerName === 'default') { |
|
112
|
|
|
// $container->setAlias('es.manager', 'es.manager.default'); |
|
113
|
|
|
// } |
|
114
|
|
|
// |
|
115
|
|
|
// $mappings = $collector->getMappings($manager['mappings']); |
|
116
|
|
|
// |
|
117
|
|
|
// // Building repository services. |
|
118
|
|
|
// foreach ($mappings as $repositoryType => $repositoryDetails) { |
|
119
|
|
|
// $repositoryDefinition = new Definition( |
|
120
|
|
|
// 'ONGR\ElasticsearchBundle\Service\Repository', |
|
121
|
|
|
// [$repositoryDetails['namespace']] |
|
122
|
|
|
// ); |
|
123
|
|
|
// |
|
124
|
|
|
// $repositoryDefinition->setFactory( |
|
125
|
|
|
// [ |
|
126
|
|
|
// new Reference(sprintf('es.manager.%s', $managerName)), |
|
127
|
|
|
// 'getRepository', |
|
128
|
|
|
// ] |
|
129
|
|
|
// ); |
|
130
|
|
|
// |
|
131
|
|
|
// $repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType); |
|
132
|
|
|
// $container->setDefinition($repositoryId, $repositoryDefinition); |
|
133
|
|
|
// } |
|
134
|
|
|
// } |
|
135
|
|
|
// } |
|
136
|
|
|
} |
|
137
|
|
|
|