Skip to content

Commit d0a7973

Browse files
authored
SearchHandler can now be replaced by you own implementation while is it now resolved via the Laravel container (#2099)
1 parent 23f4d26 commit d0a7973

6 files changed

Lines changed: 38 additions & 15 deletions

File tree

src/DataSource/Processors/Database/Handlers/SearchHandler.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use stdClass;
1212
use Throwable;
1313

14-
class SearchHandler
14+
class SearchHandler implements SearchHandlerContract
1515
{
1616
public function __construct(
17-
private readonly PowerGridComponent $component
17+
protected readonly PowerGridComponent $component
1818
) {}
1919

2020
public function apply(EloquentBuilder|QueryBuilder $query): EloquentBuilder|QueryBuilder
@@ -56,7 +56,7 @@ public function apply(EloquentBuilder|QueryBuilder $query): EloquentBuilder|Quer
5656
return $query;
5757
}
5858

59-
private function filterRelation(EloquentBuilder $query, string $search): void
59+
protected function filterRelation(EloquentBuilder $query, string $search): void
6060
{
6161
foreach ($this->component->relationSearch() as $table => $columns) {
6262
if (is_array($columns)) {
@@ -72,7 +72,7 @@ private function filterRelation(EloquentBuilder $query, string $search): void
7272
}
7373
}
7474

75-
private function filterNestedRelation(EloquentBuilder $query, string $table, array $columns, string $search): void
75+
protected function filterNestedRelation(EloquentBuilder $query, string $table, array $columns, string $search): void
7676
{
7777
foreach ($columns as $nestedTable => $nestedColumns) {
7878
if (is_array($nestedColumns)) {
@@ -113,7 +113,7 @@ private function filterNestedRelation(EloquentBuilder $query, string $table, arr
113113
}
114114
}
115115

116-
private function getColumnList(EloquentBuilder|QueryBuilder $query, string $modelTable): array
116+
protected function getColumnList(EloquentBuilder|QueryBuilder $query, string $modelTable): array
117117
{
118118
$connection = $query instanceof EloquentBuilder
119119
? $query->getModel()->getConnection()->getName()
@@ -131,12 +131,12 @@ private function getColumnList(EloquentBuilder|QueryBuilder $query, string $mode
131131
}
132132
}
133133

134-
private function getDataField(Column|stdClass|array $column): string
134+
protected function getDataField(Column|stdClass|array $column): string
135135
{
136136
return strval(data_get($column, 'dataField')) ?: strval(data_get($column, 'field'));
137137
}
138138

139-
private function getBeforeSearchMethod(string $field, ?string $search): ?string
139+
protected function getBeforeSearchMethod(string $field, ?string $search): ?string
140140
{
141141
$method = 'beforeSearch'.str($field)->headline()->replace(' ', '');
142142

@@ -151,7 +151,7 @@ private function getBeforeSearchMethod(string $field, ?string $search): ?string
151151
return $search;
152152
}
153153

154-
private function splitField(EloquentBuilder|QueryBuilder $query, string $field): array
154+
protected function splitField(EloquentBuilder|QueryBuilder $query, string $field): array
155155
{
156156
$table = $query instanceof QueryBuilder ? $query->from : $query->getModel()->getTable();
157157

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PowerComponents\LivewirePowerGrid\DataSource\Processors\Database\Handlers;
4+
5+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6+
use Illuminate\Database\Query\Builder as QueryBuilder;
7+
8+
interface SearchHandlerContract
9+
{
10+
public function apply(EloquentBuilder|QueryBuilder $query): EloquentBuilder|QueryBuilder;
11+
}

src/DataSource/Processors/Database/Pipelines/Filters.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PowerComponents\LivewirePowerGrid\DataSource\Processors\Database\Pipelines;
44

55
use Closure;
6-
use PowerComponents\LivewirePowerGrid\DataSource\Processors\Database\Handlers\{FilterHandler, SearchHandler};
6+
use PowerComponents\LivewirePowerGrid\DataSource\Processors\Database\Handlers\{FilterHandler, SearchHandlerContract};
77
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
88

99
class Filters
@@ -12,7 +12,9 @@ public function __construct(protected PowerGridComponent $component) {}
1212

1313
public function handle(mixed $query, Closure $next): mixed
1414
{
15-
(new SearchHandler($this->component))->apply($query);
15+
app()->makeWith(SearchHandlerContract::class, [
16+
'component' => $this->component,
17+
])->apply($query);
1618
(new FilterHandler($this->component))->apply($query);
1719

1820
return $next($query);

src/Providers/PowerGridServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use PowerComponents\LivewirePowerGrid\Commands\{CreateCommand, PublishCommand, UpdateCommand};
1212
use PowerComponents\LivewirePowerGrid\Components\Filters\FilterManager;
1313
use PowerComponents\LivewirePowerGrid\Components\Rules\RuleManager;
14-
use PowerComponents\LivewirePowerGrid\{Livewire\Detail,
14+
use PowerComponents\LivewirePowerGrid\{DataSource\Processors\Database\Handlers\SearchHandler,
15+
DataSource\Processors\Database\Handlers\SearchHandlerContract,
16+
Livewire\Detail,
1517
Livewire\LazyChild,
1618
Livewire\PerformanceCard,
1719
PowerGridManager,
@@ -70,6 +72,10 @@ public function register(): void
7072
Macros::builder();
7173

7274
$this->app->singleton(SupportLivewireVersions::class, fn () => new SupportLivewireVersions());
75+
76+
$this->app->bind(SearchHandlerContract::class, function ($app, array $params) {
77+
return new SearchHandler($params['component']);
78+
});
7379
}
7480

7581
private function publishViews(): void

src/Traits/ExportableJob.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PowerComponents\LivewirePowerGrid\{DataSource\DataTransformer,
88
DataSource\ProcessDataSource,
99
DataSource\Processors\Database\Handlers\FilterHandler,
10-
DataSource\Processors\Database\Handlers\SearchHandler,
10+
DataSource\Processors\Database\Handlers\SearchHandlerContract,
1111
PowerGridComponent};
1212

1313
/** @codeCoverageIgnore */
@@ -63,7 +63,9 @@ private function prepareToExport(array $properties = []): Eloquent\Collection|Co
6363

6464
$results = $this->componentTable->datasource($this->properties ?? []) // @phpstan-ignore-line
6565
->where(function ($query) {
66-
(new SearchHandler($this->componentTable))->apply($query);
66+
app()->makeWith(SearchHandlerContract::class, [
67+
'component' => $this->componentTable,
68+
])->apply($query);
6769
(new FilterHandler($this->componentTable))->apply($query);
6870
})
6971
->when($filtered, function ($query, $filtered) use ($property) {

src/Traits/WithExport.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
DataSource\DataTransformer,
1414
DataSource\ProcessDataSource,
1515
DataSource\Processors\Database\Handlers\FilterHandler,
16-
DataSource\Processors\Database\Handlers\SearchHandler};
16+
DataSource\Processors\Database\Handlers\SearchHandlerContract};
1717
use PowerComponents\LivewirePowerGrid\Jobs\ExportJob;
1818
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1919
use Throwable;
@@ -197,7 +197,9 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Col
197197

198198
$results = $processDataSource->component->datasource()
199199
->where(function ($query) {
200-
(new SearchHandler($this))->apply($query);
200+
app()->makeWith(SearchHandlerContract::class, [
201+
'component' => $this,
202+
])->apply($query);
201203
(new FilterHandler($this))->apply($query);
202204
})
203205
->when($filtered, function ($query, $filtered) use ($property) {

0 commit comments

Comments
 (0)