Skip to content

Commit

Permalink
fix: extension searcher fails without filters
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Oct 18, 2023
1 parent f27c5e9 commit dc54747
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
1 change: 1 addition & 0 deletions extensions/tags/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
->addFilter(PostSearcher::class, PostTagFilter::class)
->addFilter(DiscussionSearcher::class, TagFilter::class)
->addMutator(DiscussionSearcher::class, HideHiddenTagsFromAllDiscussionsPage::class)
->addSearcher(Tag::class, TagSearcher::class)

Check failure on line 142 in extensions/tags/extend.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Parameter #1 $modelClass of method Flarum\Extend\SearchDriver::addSearcher() expects class-string<Flarum\Search\Filter\FilterInterface>, string given.

Check failure on line 142 in extensions/tags/extend.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Parameter #1 $modelClass of method Flarum\Extend\SearchDriver::addSearcher() expects class-string<Flarum\Search\Filter\FilterInterface>, string given.
->setFulltext(TagSearcher::class, FulltextFilter::class),

(new Extend\ModelUrl(Tag::class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down
2 changes: 1 addition & 1 deletion framework/core/src/Search/SearchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
60 changes: 30 additions & 30 deletions framework/core/src/Search/SearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit dc54747

Please sign in to comment.