You are here

class QueryHelper in Search API 8

Provides methods for creating search queries and statically caching results.

Hierarchy

Expanded class hierarchy of QueryHelper

1 string reference to 'QueryHelper'
search_api.services.yml in ./search_api.services.yml
search_api.services.yml
1 service uses QueryHelper
search_api.query_helper in ./search_api.services.yml
Drupal\search_api\Utility\QueryHelper

File

src/Utility/QueryHelper.php, line 15

Namespace

Drupal\search_api\Utility
View source
class QueryHelper implements QueryHelperInterface {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The parse mode manager.
   *
   * @var \Drupal\search_api\ParseMode\ParseModePluginManager
   */
  protected $parseModeManager;

  /**
   * Storage for the results, keyed by request and search ID.
   *
   * @var \SplObjectStorage
   */
  protected $results;

  /**
   * NULL value to use as a key for the results storage.
   *
   * @var object
   */
  protected $null;

  /**
   * Constructs a QueryHelper object.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler.
   * @param \Drupal\search_api\ParseMode\ParseModePluginManager $parseModeManager
   *   The parse mode manager.
   */
  public function __construct(RequestStack $requestStack, ModuleHandlerInterface $moduleHandler, ParseModePluginManager $parseModeManager) {
    $this->requestStack = $requestStack;
    $this->moduleHandler = $moduleHandler;
    $this->parseModeManager = $parseModeManager;
    $this->results = new \SplObjectStorage();
    $this->null = (object) [];
  }

  /**
   * {@inheritdoc}
   */
  public function createQuery(IndexInterface $index, array $options = []) {
    $query = Query::create($index, $options);
    $query
      ->setModuleHandler($this->moduleHandler);
    $query
      ->setParseModeManager($this->parseModeManager);
    $query
      ->setQueryHelper($this);
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function addResults(ResultSetInterface $results) {
    $search_id = $results
      ->getQuery()
      ->getSearchId();
    $request = $this
      ->getCurrentRequest();
    if (!isset($this->results[$request])) {
      $this->results[$request] = [
        $search_id => $results,
      ];
    }
    else {

      // It's not possible to directly assign array values to an array inside of
      // a \SplObjectStorage object. So we have to first retrieve the array,
      // then add the results to it, then store it again.
      $cache = $this->results[$request];
      $cache[$search_id] = $results;
      $this->results[$request] = $cache;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getResults($search_id) {
    return $this->results[$this
      ->getCurrentRequest()][$search_id] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getAllResults() {
    return $this->results[$this
      ->getCurrentRequest()] ?? [];
  }

  /**
   * {@inheritdoc}
   */
  public function removeResults($search_id) {
    $results = $this->results[$this
      ->getCurrentRequest()];
    unset($results[$search_id]);
    $this->results[$this
      ->getCurrentRequest()] = $results;
  }

  /**
   * Retrieves the current request.
   *
   * If there is no current request, instead of returning NULL this will instead
   * return a unique object to be used in lieu of a NULL key.
   *
   * @return \Symfony\Component\HttpFoundation\Request|object
   *   The current request, if present; or this object's representation of the
   *   NULL key.
   */
  protected function getCurrentRequest() {
    return $this->requestStack
      ->getCurrentRequest() ?: $this->null;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QueryHelper::$moduleHandler protected property The module handler.
QueryHelper::$null protected property NULL value to use as a key for the results storage.
QueryHelper::$parseModeManager protected property The parse mode manager.
QueryHelper::$requestStack protected property The request stack.
QueryHelper::$results protected property Storage for the results, keyed by request and search ID.
QueryHelper::addResults public function Adds a result set to the cache. Overrides QueryHelperInterface::addResults
QueryHelper::createQuery public function Creates a new search query object. Overrides QueryHelperInterface::createQuery
QueryHelper::getAllResults public function Retrieves all results data cached in this request. Overrides QueryHelperInterface::getAllResults
QueryHelper::getCurrentRequest protected function Retrieves the current request.
QueryHelper::getResults public function Retrieves the results data for a search ID. Overrides QueryHelperInterface::getResults
QueryHelper::removeResults public function Removes the result set with the given search ID from the cache. Overrides QueryHelperInterface::removeResults
QueryHelper::__construct public function Constructs a QueryHelper object.