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( |
|
37 | DocumentManager $documentManager, |
||
38 | $name = null |
||
39 | ) { |
||
40 | 4 | parent::__construct($name); |
|
41 | |||
42 | 4 | $this->documentManager = $documentManager; |
|
43 | 4 | } |
|
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) && array_key_exists($searchName, $index['keys'])) { |
||
85 | if (array_key_exists('options', $index) && !empty($index['options'])) { |
||
86 | $collection = $this->documentManager->getDocumentCollection($metadata->getName()); |
||
87 | if (!$collection) { |
||
88 | continue; |
||
89 | } |
||
90 | $newIndex = []; |
||
91 | $weights = []; |
||
92 | foreach ($index['options'] as $optionName => $optionsValue) { |
||
93 | if (substr($optionName, 0, 6) == 'search') { |
||
94 | $optionName = substr($optionName, 6); |
||
95 | $newIndex[$optionName] = 'text'; |
||
96 | $weights[$optionName] = floatval($optionsValue); |
||
97 | } |
||
98 | } |
||
99 | if (empty($weights)) { |
||
100 | continue; |
||
101 | } |
||
102 | foreach ($collection->getIndexInfo() as $indexInfo) { |
||
103 | // When using doctrine name may have a _1 |
||
104 | if (strpos($indexInfo['name'], $searchName) !== false) { |
||
105 | $output->writeln("Deleting Custom Text index {$searchName}"); |
||
106 | $this->documentManager->getDocumentDatabase($metadata->getName())->command( |
||
107 | [ |
||
108 | "deleteIndexes" => $collection->getName(), |
||
109 | "index" => $indexInfo['name'] |
||
110 | ] |
||
111 | ); |
||
112 | break; |
||
113 | } |
||
114 | } |
||
115 | $output->writeln($metadata->getName().": created custom Text index {$searchName}"); |
||
116 | $collection->ensureIndex( |
||
117 | $newIndex, |
||
118 | [ |
||
119 | 'weights' => $weights, |
||
120 | 'name' => $searchName, |
||
121 | 'default_language' => 'de', |
||
122 | 'language_override' => 'none' |
||
123 | ] |
||
124 | ); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Gets the installed MongoDB Version |
||
133 | * @param String $className The Classname of the collection, needed to fetch the right DB connection |
||
134 | * @return String getMongoDBVersion The version of the MongoDB as a string |
||
135 | */ |
||
136 | private function getMongoDBVersion($className) |
||
145 | } |
||
146 |
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.