grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026 'lib/Search/ElasticSearch/ElasticSearchEngine.php', ]; /** @var string Path to the folder where to load custom engines from */ private static $customEnginePath = __DIR__ . '/../../custom/Extension/SearchEngines/'; /** * Perform a search with the given query and engine. * * If no search engine field is provided in the query, the default one will be used. * * @param SearchQuery $query */ public static function searchAndDisplay(SearchQuery $query) { $engine = $query->getEngine() ?: self::getDefaultEngine(); $engine = self::fetchEngine($engine); $engine->searchAndDisplay($query); } /** * Perform a search with the given query and engine. * * Results are grouped by module. * * @param string|SearchEngine $engine * @param SearchQuery $query * * @return SearchResults */ public static function search($engine, SearchQuery $query) { $engine = self::fetchEngine($engine); $results = $engine->search($query); return $results; } /** * Binds a class name / engine name to a file. * * @param string $className * @param string $file */ public static function addEngine($className, $file) { self::$engines[$className] = $file; } /** * Retrieves the available search engines class names. * * @return string[] */ public static function getEngines() { $default = array_keys(self::$engines); $custom = []; foreach (glob(self::$customEnginePath . '*.php') as $file) { $file = pathinfo($file); $custom[] = $file['filename']; } return array_merge($default, $custom); } /** * Retrieves the default search engine name from the global configuration. * * If the default config does not have an engine, the first engine from the engines array is returned. * * @return string */ public static function getDefaultEngine() { $config = self::getSearchConfig('defaultEngine'); return $config === null ? key(self::$engines) // first engine in the array : $config; } /** * Returns the configured search controller from the sugar config. * * If the value is, for some reason, not set, `null` is returned. * * @return string|null */ public static function getController() { return self::getSearchConfig('controller'); } /** * Returns the configured modules to be used with search. * * @return array|null */ public static function getModules() { return SearchModules::getEnabledModules(); } /** * Performs various validation and retrieves an instance of a given search engine. * * It first searches in the default definitions array `self::$engines`, * then attempts to find a matching engine in the folder `self::CUSTOM_ENGINES_PATH`. * * @param string|SearchEngine $engineName * * @throws SearchEngineNotFoundException * @return SearchEngine */ private static function fetchEngine($engineName) { if (is_subclass_of($engineName, SearchEngine::class, false)) { return $engineName; } if (!is_string($engineName)) { throw new SearchEngineNotFoundException('$engineName should either be a string or a SearchEngine'); } if (!preg_match("/^[a-zA-Z0-9_]*$/", $engineName)) { throw new SearchEngineNotFoundException("'$engineName' is not a valid class name. Only letters, digits and underscores are allowed."); } $filename = isset(self::$engines[$engineName]) ? self::$engines[$engineName] : self::$customEnginePath . $engineName . '.php'; if (!file_exists($filename)) { throw new SearchEngineNotFoundException("Unable to find search file '$filename'' for engine '$engineName''."); } /** @noinspection PhpIncludeInspection */ require_once $filename; if (!is_subclass_of($engineName, SearchEngine::class)) { throw new SearchEngineNotFoundException("The provided class '$engineName' is not a subclass of SearchEngine"); } /** @var SearchEngine $engineName */ $engineName = new $engineName(); return $engineName; } /** * Returns a configured parameter from the sugar config. * * If the values is not set, returns `null`. * * @param $key * * @return mixed|null */ private static function getSearchConfig($key) { /** @noinspection PhpVariableNamingConventionInspection */ global $sugar_config; if (!isset($sugar_config['search'][$key])) { return null; } return $sugar_config['search'][$key]; } }