Skip to content

Commit 13df97a

Browse files
Merge pull request #21 from skurfuerst/make-system-work-with-simplesearch
BUGFIX: make dependency with Elasticsearch optional
2 parents 19e2a72 + d15435a commit 13df97a

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

Classes/Flowpack/SearchPlugin/Controller/SuggestController.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* source code.
1212
*/
1313

14+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
1415
use TYPO3\Flow\Annotations as Flow;
1516
use TYPO3\Flow\Mvc\Controller\ActionController;
1617

@@ -20,8 +21,8 @@
2021
class SuggestController extends ActionController
2122
{
2223
/**
23-
* @Flow\Inject
24-
* @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient
24+
* Dynamic dependency; to make the system work with SimpleSearch
25+
* @var ElasticSearchClient
2526
*/
2627
protected $elasticSearchClient;
2728

@@ -32,6 +33,20 @@ class SuggestController extends ActionController
3233
'json' => 'TYPO3\Flow\Mvc\View\JsonView'
3334
];
3435

36+
/**
37+
* Sets the ElasticSearchClient instance needed for this to work. If no client is set,
38+
* this controller cannot be used; but at least the package can otherwise be used with
39+
* e.g. SimpleSearch.
40+
*
41+
* @return void
42+
*/
43+
public function initializeObject()
44+
{
45+
if ($this->objectManager->isRegistered(ElasticSearchClient::class)) {
46+
$this->elasticSearchClient = $this->objectManager->get(ElasticSearchClient::class);
47+
}
48+
}
49+
3550
/**
3651
* @param string $term
3752
* @return void
@@ -47,6 +62,10 @@ public function indexAction($term)
4762
]
4863
];
4964

65+
if ($this->elasticSearchClient === null) {
66+
throw new \RuntimeException('The SuggestController needs an ElasticSearchClient, it seems you run without the flowpack/elasticsearch-contentrepositoryadaptor package, though.', 1487189823);
67+
}
68+
5069
$response = $this->elasticSearchClient->getIndex()->request('POST', '/_suggest', [], json_encode($request))->getTreatedContent();
5170
$suggestions = array_map(function ($option) {
5271
return $option['text'];

0 commit comments

Comments
 (0)