1 | <?php |
||
22 | class GenerateBuildIndexesCommand extends Command |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var documentManager |
||
27 | */ |
||
28 | private $documentManager; |
||
29 | |||
30 | /** |
||
31 | * GenerateBuildIndexesCommand constructor. |
||
32 | * |
||
33 | * @param DocumentManager $documentManager The Doctrine Document Manager |
||
34 | * @param String $name The Name of this Command |
||
|
|||
35 | */ |
||
36 | 4 | public function __construct( |
|
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | * |
||
48 | * @return void |
||
49 | */ |
||
50 | 4 | protected function configure() |
|
60 | |||
61 | /** |
||
62 | * {@inheritDoc} |
||
63 | * |
||
64 | * @param InputInterface $input input |
||
65 | * @param OutputInterface $output output |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | protected function execute(InputInterface $input, OutputInterface $output) |
||
70 | { |
||
71 | // Check mongo db version |
||
72 | $mongoVersion = $this->getMongoDBVersion('Graviton\\CoreBundle\\Document\\App'); |
||
73 | if ((float) $mongoVersion < 2.6) { |
||
74 | $output->writeln("MongoDB Version < 2.6 installed: " . $mongoVersion); |
||
75 | exit(); |
||
76 | } |
||
77 | |||
78 | $metadatas = $this->documentManager->getMetadataFactory()->getAllMetadata(); |
||
79 | /** @var ClassMetadata $metadata */ |
||
80 | foreach ($metadatas as $metadata) { |
||
81 | $indexes = $metadata->getIndexes(); |
||
82 | $searchName = 'search_'.$metadata->getCollection().'_index'; |
||
83 | foreach ($indexes as $index) { |
||
84 | if (array_key_exists('keys', $index) && |
||
85 | array_key_exists('options', $index) && |
||
86 | array_key_exists('name', $index['options']) && |
||
87 | $searchName == $index['options']['name'] && |
||
88 | is_array($index['keys']) && !empty($index['keys']) |
||
89 | ) { |
||
90 | $collection = $this->documentManager->getDocumentCollection($metadata->getName()); |
||
91 | if (!$collection) { |
||
92 | continue; |
||
93 | } |
||
94 | |||
95 | $newIndex = []; |
||
96 | $weights = []; |
||
97 | foreach (array_keys($index['keys']) as $optionKeyName) { |
||
98 | if (substr($optionKeyName, 0, 7) == 'search_') { |
||
99 | $options = explode('-', substr($optionKeyName, 7)); |
||
100 | $value = end($options); |
||
101 | array_pop($options); |
||
102 | $fieldName = implode('-', $options); |
||
103 | $newIndex[$fieldName] = 'text'; |
||
104 | $weights[$fieldName] = floatval($value); |
||
105 | } |
||
106 | } |
||
107 | if (empty($weights)) { |
||
108 | $output->writeln("No Custom Text index for: {$searchName}"); |
||
109 | continue; |
||
110 | } |
||
111 | |||
112 | $output->writeln("Deleting Custom Text index {$searchName}"); |
||
113 | $this->documentManager->getDocumentDatabase($metadata->getName())->command( |
||
114 | [ |
||
115 | "deleteIndexes" => $collection->getName(), |
||
116 | "index" => $searchName |
||
117 | ] |
||
118 | ); |
||
119 | |||
120 | $newIndexName = str_replace('_', '', $searchName); |
||
121 | $output->writeln($metadata->getName().": creating custom Text index {$newIndexName}"); |
||
122 | $collection->ensureIndex( |
||
123 | $newIndex, |
||
124 | [ |
||
125 | 'weights' => $weights, |
||
126 | 'name' => $newIndexName, |
||
127 | 'default_language' => 'de', |
||
128 | 'language_override' => 'none' |
||
129 | ] |
||
130 | ); |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Gets the installed MongoDB Version |
||
138 | * @param String $className The Classname of the collection, needed to fetch the right DB connection |
||
139 | * @return String getMongoDBVersion The version of the MongoDB as a string |
||
140 | */ |
||
141 | private function getMongoDBVersion($className) |
||
150 | } |
||
151 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.