-
-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathScoutEnhancer.php
168 lines (137 loc) · 5.53 KB
/
ScoutEnhancer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php declare(strict_types=1);
namespace Nuwave\Lighthouse\Scout;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Laravel\Scout\Builder as ScoutBuilder;
use Laravel\Scout\Searchable;
use Nuwave\Lighthouse\Execution\Arguments\Argument;
use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet;
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
use Nuwave\Lighthouse\Support\Utils;
/**
* @template TModel of \Illuminate\Database\Eloquent\Model
*/
class ScoutEnhancer
{
/**
* Provide the actual search value.
*
* @var array<Argument>
*/
protected array $searchArguments = [];
/**
* Should not be there when @search is used.
*
* @var array<Argument>
*/
protected array $argumentsWithOnlyArgBuilders = [];
/**
* Can enhance the scout builders.
*
* @var array<Argument>
*/
protected array $argumentsWithScoutBuilderDirectives = [];
public function __construct(
protected ArgumentSet $argumentSet,
/**
* @var \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<TModel>|\Illuminate\Database\Eloquent\Relations\Relation<TModel>|\Laravel\Scout\Builder $builder
*/
protected QueryBuilder|EloquentBuilder|Relation|ScoutBuilder $builder,
) {
$this->gather($this->argumentSet);
}
public function hasSearchArguments(): bool
{
return $this->searchArguments !== [];
}
public function canEnhanceBuilder(): bool
{
return $this->hasSearchArguments()
|| $this->builder instanceof ScoutBuilder;
}
public function wouldEnhanceBuilder(): bool
{
return $this->hasSearchArguments();
}
/** @param (callable(\Nuwave\Lighthouse\Scout\ScoutBuilderDirective): bool)|null $directiveFilter */
public function enhanceBuilder(callable $directiveFilter = null): ScoutBuilder
{
$scoutBuilder = $this->builder instanceof ScoutBuilder
? $this->builder
: $this->enhanceEloquentBuilder();
foreach ($this->argumentsWithScoutBuilderDirectives as $argument) {
foreach ($argument->directives as $directive) {
if (! ($directive instanceof ScoutBuilderDirective)) {
continue;
}
if ($directiveFilter !== null && ! $directiveFilter($directive)) {
continue;
}
$directive->handleScoutBuilder($scoutBuilder, $argument->toPlain());
}
}
return $scoutBuilder;
}
protected function gather(ArgumentSet $argumentSet): void
{
foreach ($argumentSet->arguments as $argument) {
$argumentHasSearchDirective = $argument
->directives
->contains(Utils::instanceofMatcher(SearchDirective::class));
if ($argumentHasSearchDirective && is_string($argument->value)) {
$this->searchArguments[] = $argument;
}
$argumentHasArgBuilderDirective = $argument
->directives
->contains(Utils::instanceofMatcher(ArgBuilderDirective::class));
$argumentHasScoutBuilderDirective = $argument
->directives
->contains(Utils::instanceofMatcher(ScoutBuilderDirective::class));
if ($argumentHasArgBuilderDirective && ! $argumentHasScoutBuilderDirective) {
$this->argumentsWithOnlyArgBuilders[] = $argument;
}
if ($argumentHasScoutBuilderDirective) {
$this->argumentsWithScoutBuilderDirectives[] = $argument;
}
Utils::applyEach(
function ($value): void {
if ($value instanceof ArgumentSet) {
$this->gather($value);
}
},
$argument->value,
);
}
}
protected function enhanceEloquentBuilder(): ScoutBuilder
{
if (count($this->searchArguments) > 1) {
throw new ScoutException('Found more than 1 argument with @search.');
}
$searchArgument = $this->searchArguments[0];
if ($this->argumentsWithOnlyArgBuilders !== []) {
throw new ScoutException('Found arg builder arguments that do not work with @search.');
}
if (! $this->builder instanceof EloquentBuilder) {
$eloquentBuilderClass = EloquentBuilder::class;
$thisBuilderClass = $this->builder::class;
throw new ScoutException("Can only get Model from {$eloquentBuilderClass}, got: {$thisBuilderClass}.");
}
$model = $this->builder->getModel();
$searchableTraitClass = Searchable::class;
if (! Utils::classUsesTrait($model, $searchableTraitClass)) {
$modelClass = $model::class;
throw new ScoutException("Model class {$modelClass} does not implement trait {$searchableTraitClass}.");
}
// @phpstan-ignore-next-line Can not use traits as types
/** @var \Illuminate\Database\Eloquent\Model&\Laravel\Scout\Searchable $model */
$scoutBuilder = $model::search($searchArgument->toPlain());
$searchDirective = $searchArgument
->directives
->first(Utils::instanceofMatcher(SearchDirective::class));
assert($searchDirective instanceof SearchDirective);
$searchDirective->search($scoutBuilder);
return $scoutBuilder;
}
}