From dc54747b88288143507660b543659c7813987881 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Wed, 18 Oct 2023 17:26:56 +0100 Subject: [PATCH] fix: extension searcher fails without filters --- extensions/tags/extend.php | 1 + .../api/tags/ListWithFulltextSearchTest.php | 4 +- framework/core/src/Search/SearchManager.php | 2 +- .../core/src/Search/SearchServiceProvider.php | 60 +++++++++---------- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/extensions/tags/extend.php b/extensions/tags/extend.php index 4ba4735e81..f17c16904f 100644 --- a/extensions/tags/extend.php +++ b/extensions/tags/extend.php @@ -139,6 +139,7 @@ ->addFilter(PostSearcher::class, PostTagFilter::class) ->addFilter(DiscussionSearcher::class, TagFilter::class) ->addMutator(DiscussionSearcher::class, HideHiddenTagsFromAllDiscussionsPage::class) + ->addSearcher(Tag::class, TagSearcher::class) ->setFulltext(TagSearcher::class, FulltextFilter::class), (new Extend\ModelUrl(Tag::class)) diff --git a/extensions/tags/tests/integration/api/tags/ListWithFulltextSearchTest.php b/extensions/tags/tests/integration/api/tags/ListWithFulltextSearchTest.php index 059e15da5e..d6ff6920ee 100644 --- a/extensions/tags/tests/integration/api/tags/ListWithFulltextSearchTest.php +++ b/extensions/tags/tests/integration/api/tags/ListWithFulltextSearchTest.php @@ -49,9 +49,9 @@ public function can_search_for_tags(string $search, array $expected) ]) ); - $data = json_decode($response->getBody()->getContents(), true)['data']; + $data = json_decode($contents = $response->getBody()->getContents(), true)['data'] ?? []; - $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode(), $contents); $this->assertEquals($expected, Arr::pluck($data, 'id')); } diff --git a/framework/core/src/Search/SearchManager.php b/framework/core/src/Search/SearchManager.php index 484d7953bd..dfaafaa31a 100644 --- a/framework/core/src/Search/SearchManager.php +++ b/framework/core/src/Search/SearchManager.php @@ -27,7 +27,7 @@ public function __construct( ) { } - public function driver(string $name): AbstractDriver + public function driver(?string $name): AbstractDriver { $driver = Arr::first($this->drivers, fn ($driver) => $driver::name() === $name); diff --git a/framework/core/src/Search/SearchServiceProvider.php b/framework/core/src/Search/SearchServiceProvider.php index 1d2b543754..95c1452f94 100644 --- a/framework/core/src/Search/SearchServiceProvider.php +++ b/framework/core/src/Search/SearchServiceProvider.php @@ -120,37 +120,37 @@ public function boot(Container $container): void ->when($driverClass) ->needs('$searchers') ->give($searchers); - } - - foreach ($container->make('flarum.search.filters') as $searcher => $filterClasses) { - $container - ->when($searcher) - ->needs(FilterManager::class) - ->give(function () use ($container, $searcher) { - $fulltext = $container->make('flarum.search.fulltext'); - $fulltextClass = $fulltext[$searcher] ?? null; - - $manager = new FilterManager( - $fulltextClass ? $container->make($fulltextClass) : null - ); - foreach (Arr::get($container->make('flarum.search.filters'), $searcher, []) as $filter) { - $manager->add($container->make($filter)); - } - - return $manager; - }); - - $container - ->when($searcher) - ->needs('$mutators') - ->give(function () use ($container, $searcher) { - $searchMutators = Arr::get($container->make('flarum.search.mutators'), $searcher, []); - - return array_map(function ($mutator) { - return ContainerUtil::wrapCallback($mutator, $this->container); - }, $searchMutators); - }); + foreach ($searchers as $searcher) { + $container + ->when($searcher) + ->needs(FilterManager::class) + ->give(function () use ($container, $searcher) { + $fulltext = $container->make('flarum.search.fulltext'); + $fulltextClass = $fulltext[$searcher] ?? null; + + $manager = new FilterManager( + $fulltextClass ? $container->make($fulltextClass) : null + ); + + foreach (Arr::get($container->make('flarum.search.filters'), $searcher, []) as $filter) { + $manager->add($container->make($filter)); + } + + return $manager; + }); + + $container + ->when($searcher) + ->needs('$mutators') + ->give(function () use ($container, $searcher) { + $searchMutators = Arr::get($container->make('flarum.search.mutators'), $searcher, []); + + return array_map(function ($mutator) { + return ContainerUtil::wrapCallback($mutator, $this->container); + }, $searchMutators); + }); + } } /** @var \Flarum\Database\AbstractModel $modelClass */