storeManager = $storeManager; $this->localeResolver = $localeResolver; $this->readFactory = $readFactory; $this->configCache = $configCache; $this->esConfig = $esConfig; $this->moduleDirReader = $moduleDirReader; $this->stopwordsModule = $stopwordsModule; $this->stopwordsDirectory = $stopwordsDirectory; } /** * {@inheritdoc} * @since 100.1.0 */ public function process($query) { $stopwords = $this->getStopwordsList(); $queryParts = explode(' ', $query); $query = implode(' ', array_diff($queryParts, $stopwords)); return trim($query); } /** * Get stopwords list for current locale * * @return array * @since 100.1.0 */ protected function getStopwordsList() { $filename = $this->getStopwordsFile(); $fileDir = $this->moduleDirReader->getModuleDir(Dir::MODULE_ETC_DIR, $this->stopwordsModule) . '/' . $this->stopwordsDirectory; $source = $this->readFactory->create($fileDir); $fileStats = $source->stat($filename); if (((time() - $fileStats['mtime']) > self::STOPWORDS_FILE_MODIFICATION_TIME_GAP) && ($cachedValue = $this->configCache->load(self::CACHE_ID))) { $stopwords = $this->getSerializer()->unserialize($cachedValue); } else { $fileContent = $source->readFile($filename); $stopwords = explode("\n", $fileContent); $this->configCache->save($this->getSerializer()->serialize($stopwords), self::CACHE_ID); } return $stopwords; } /** * Get stopwords file for current locale * * @return string * @since 100.1.0 */ protected function getStopwordsFile() { $stopwordsInfo = $this->esConfig->getStopwordsInfo(); $storeId = $this->storeManager->getStore()->getId(); $this->localeResolver->emulate($storeId); $locale = $this->localeResolver->getLocale(); $stopwordsFile = isset($stopwordsInfo[$locale]) ? $stopwordsInfo[$locale] : $stopwordsInfo['default']; return $stopwordsFile; } /** * Get serializer * * @return \Magento\Framework\Serialize\SerializerInterface * @deprecated 100.2.0 */ private function getSerializer() { if (null === $this->serializer) { $this->serializer = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\Serialize\SerializerInterface::class); } return $this->serializer; } }