Example #1
0
 /**
  * @return string Path to config file
  * @throws \Exception
  */
 public function makeConfig()
 {
     if (!$this->directory->isExist($this->basePath)) {
         //$this->directory->delete($this->basePath);
         $this->directory->create($this->basePath);
         $this->directory->changePermissions($this->basePath, 0777);
     }
     $jsonData = [];
     $sphinxData = ['time' => date('d.m.Y H:i:s'), 'host' => $this->host, 'port' => $this->port, 'fallback_port' => $this->port - 1, 'logdir' => $this->directory->getAbsolutePath($this->basePath), 'sphinxdir' => $this->directory->getAbsolutePath($this->basePath), 'indexes' => '', 'localdir' => dirname(dirname(__FILE__)), 'custom' => $this->config->getAdditionalSearchdConfig()];
     $sphinxTemplate = $this->config->getSphinxConfigurationTemplate();
     $indexTemplate = $this->config->getSphinxIndexConfigurationTemplate();
     /** @var \Mirasvit\Search\Model\Index $index */
     foreach ($this->indexCollectionFactory->create() as $index) {
         foreach (array_keys($this->storeManager->getStores()) as $storeId) {
             $indexName = $index->getIndexInstance()->getIndexer()->getIndexName($storeId);
             $data = ['name' => $indexName, 'min_word_len' => 1, 'path' => $this->directory->getAbsolutePath($this->basePath) . '/' . $indexName, 'custom' => $this->config->getAdditionalIndexConfig()];
             $jsonAttributes = [];
             $attributes = [];
             foreach (array_keys($index->getIndexInstance()->getAttributes(true)) as $attribute) {
                 $attributes[] = "    rt_field = {$attribute}";
                 $jsonAttributes[] = $attribute;
                 if (count($attributes) > 250) {
                     break;
                 }
             }
             $attributes[] = "    rt_field = options";
             $jsonAttributes[] = "options";
             $data['attributes'] = implode(PHP_EOL, $attributes);
             $sphinxData['indexes'] .= $this->helper->filterTemplate($indexTemplate, $data);
             $jsonData[$indexName] = $jsonAttributes;
         }
     }
     $config = $this->helper->filterTemplate($sphinxTemplate, $sphinxData);
     if ($this->directory->isWritable($this->basePath)) {
         $this->directory->writeFile($this->configFilePath, $config);
         $this->directory->writeFile($this->configFilePath . '.attr', json_encode($jsonData));
     } else {
         if ($this->directory->isExist($this->configFilePath)) {
             throw new \Exception(__('File %1 does not writable', $this->configFilePath));
         } else {
             throw new \Exception(__('Directory %1 does not writable', $this->basePath));
         }
     }
     return $this->directory->getAbsolutePath($this->configFilePath);
 }