Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Stopwords.php
Go to the documentation of this file.
1 <?php
7 
12 
18 {
22  const CACHE_ID = 'elasticsearch_stopwords';
23 
28 
33  protected $storeManager;
34 
39  protected $localeResolver;
40 
45  protected $readFactory;
46 
51  protected $configCache;
52 
57  protected $esConfig;
58 
63  protected $moduleDirReader;
64 
68  private $stopwordsModule;
69 
73  private $stopwordsDirectory;
74 
78  private $serializer;
79 
92  public function __construct(
93  \Magento\Store\Model\StoreManagerInterface $storeManager,
94  \Magento\Framework\Locale\Resolver $localeResolver,
96  \Magento\Framework\App\Cache\Type\Config $configCache,
98  \Magento\Framework\Module\Dir\Reader $moduleDirReader,
99  $stopwordsModule = '',
100  $stopwordsDirectory = ''
101  ) {
102  $this->storeManager = $storeManager;
103  $this->localeResolver = $localeResolver;
104  $this->readFactory = $readFactory;
105  $this->configCache = $configCache;
106  $this->esConfig = $esConfig;
107  $this->moduleDirReader = $moduleDirReader;
108  $this->stopwordsModule = $stopwordsModule;
109  $this->stopwordsDirectory = $stopwordsDirectory;
110  }
111 
116  public function process($query)
117  {
118  $stopwords = $this->getStopwordsList();
119  $queryParts = explode(' ', $query);
120  $query = implode(' ', array_diff($queryParts, $stopwords));
121  return trim($query);
122  }
123 
130  protected function getStopwordsList()
131  {
132  $filename = $this->getStopwordsFile();
133  $fileDir = $this->moduleDirReader->getModuleDir(Dir::MODULE_ETC_DIR, $this->stopwordsModule)
134  . '/' . $this->stopwordsDirectory;
135  $source = $this->readFactory->create($fileDir);
136  $fileStats = $source->stat($filename);
137  if (((time() - $fileStats['mtime']) > self::STOPWORDS_FILE_MODIFICATION_TIME_GAP)
138  && ($cachedValue = $this->configCache->load(self::CACHE_ID))) {
139  $stopwords = $this->getSerializer()->unserialize($cachedValue);
140  } else {
141  $fileContent = $source->readFile($filename);
142  $stopwords = explode("\n", $fileContent);
143  $this->configCache->save($this->getSerializer()->serialize($stopwords), self::CACHE_ID);
144  }
145  return $stopwords;
146  }
147 
154  protected function getStopwordsFile()
155  {
156  $stopwordsInfo = $this->esConfig->getStopwordsInfo();
157  $storeId = $this->storeManager->getStore()->getId();
158  $this->localeResolver->emulate($storeId);
159  $locale = $this->localeResolver->getLocale();
160  $stopwordsFile = isset($stopwordsInfo[$locale]) ? $stopwordsInfo[$locale] : $stopwordsInfo['default'];
161  return $stopwordsFile;
162  }
163 
170  private function getSerializer()
171  {
172  if (null === $this->serializer) {
174  ->get(\Magento\Framework\Serialize\SerializerInterface::class);
175  }
176  return $this->serializer;
177  }
178 }
$source
Definition: source.php:23
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Locale\Resolver $localeResolver, ReadFactory $readFactory, \Magento\Framework\App\Cache\Type\Config $configCache, EsConfigInterface $esConfig, \Magento\Framework\Module\Dir\Reader $moduleDirReader, $stopwordsModule='', $stopwordsDirectory='')
Definition: Stopwords.php:92