class QueryHelper in Search API 8
Provides methods for creating search queries and statically caching results.
Hierarchy
- class \Drupal\search_api\Utility\QueryHelper implements QueryHelperInterface
Expanded class hierarchy of QueryHelper
1 string reference to 'QueryHelper'
1 service uses QueryHelper
File
- src/
Utility/ QueryHelper.php, line 15
Namespace
Drupal\search_api\UtilityView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
QueryHelper:: |
protected | property | The module handler. | |
QueryHelper:: |
protected | property | NULL value to use as a key for the results storage. | |
QueryHelper:: |
protected | property | The parse mode manager. | |
QueryHelper:: |
protected | property | The request stack. | |
QueryHelper:: |
protected | property | Storage for the results, keyed by request and search ID. | |
QueryHelper:: |
public | function |
Adds a result set to the cache. Overrides QueryHelperInterface:: |
|
QueryHelper:: |
public | function |
Creates a new search query object. Overrides QueryHelperInterface:: |
|
QueryHelper:: |
public | function |
Retrieves all results data cached in this request. Overrides QueryHelperInterface:: |
|
QueryHelper:: |
protected | function | Retrieves the current request. | |
QueryHelper:: |
public | function |
Retrieves the results data for a search ID. Overrides QueryHelperInterface:: |
|
QueryHelper:: |
public | function |
Removes the result set with the given search ID from the cache. Overrides QueryHelperInterface:: |
|
QueryHelper:: |
public | function | Constructs a QueryHelper object. |