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 ONGR\ElasticsearchBundle\Service\IndexService; |
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
use Symfony\Component\Finder\Finder; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Compiles elastic search data. |
24
|
|
|
*/ |
25
|
|
|
class MappingPass implements CompilerPassInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
|
|
public function process(ContainerBuilder $container) |
32
|
|
|
{ |
33
|
|
|
$analysis = $container->getParameter(Configuration::ONGR_ANALYSIS_CONFIG); |
|
|
|
|
34
|
|
|
$additionalDirs = $container->getParameter(Configuration::ONGR_INCLUDE_DIR_CONFIG); |
35
|
|
|
|
36
|
|
|
$collector = $container->get('es.metadata_collector'); |
37
|
|
|
|
38
|
|
|
$kernelProjectDir = $container->getParameter('kernel.project_dir'); |
39
|
|
|
|
40
|
|
|
$namespaces = $this->getNamespaces($kernelProjectDir . '/src'); |
41
|
|
|
|
42
|
|
|
foreach ($additionalDirs as $directory) { |
43
|
|
|
$namespaces = array_merge($namespaces, $this->getNamespaces($directory)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
foreach ($namespaces as $namespace) { |
47
|
|
|
$indexMapping = $collector->getMapping($namespace); |
|
|
|
|
48
|
|
|
$definition = new Definition(IndexService::class, [ |
|
|
|
|
49
|
|
|
|
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
// $container->autowire($namespace, IndexService::class); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function getNamespaces($directory) { |
57
|
|
|
$documentsDirectory = DIRECTORY_SEPARATOR . str_replace('\\', '/', $directory) . DIRECTORY_SEPARATOR; |
58
|
|
|
|
59
|
|
|
if (!is_dir($directory)) { |
60
|
|
|
return []; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory)); |
64
|
|
|
$files = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH); |
65
|
|
|
|
66
|
|
|
$documents = []; |
67
|
|
|
|
68
|
|
View Code Duplication |
foreach ($files as $file => $v) { |
|
|
|
|
69
|
|
|
$documents[] = str_replace( |
70
|
|
|
DIRECTORY_SEPARATOR, |
71
|
|
|
'\\', |
72
|
|
|
substr(strstr($file, $documentsDirectory), strlen($documentsDirectory), -4) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $documents; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// |
80
|
|
|
// private function getFullNamespace($filename) { |
81
|
|
|
// $lines = file($filename); |
82
|
|
|
// $namespaceLine = array_shift(preg_grep('/^namespace /', $lines)); |
83
|
|
|
// $match = array(); |
84
|
|
|
// preg_match('/^namespace (.*);$/', $namespaceLine, $match); |
85
|
|
|
// $fullNamespace = array_pop($match); |
86
|
|
|
// |
87
|
|
|
// return $fullNamespace; |
88
|
|
|
// } |
89
|
|
|
// |
90
|
|
|
// private function getClassname($filename) { |
91
|
|
|
// $directoriesAndFilename = explode('/', $filename); |
92
|
|
|
// $filename = array_pop($directoriesAndFilename); |
93
|
|
|
// $nameAndExtension = explode('.', $filename); |
94
|
|
|
// $className = array_shift($nameAndExtension); |
95
|
|
|
// |
96
|
|
|
// return $className; |
97
|
|
|
// } |
98
|
|
|
} |
99
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.