12
12
13
13
class PostgresEngineServiceProvider extends ServiceProvider
14
14
{
15
+ public static function builderMacros ()
16
+ {
17
+ return [
18
+ 'usingPhraseQuery ' => PhraseToTsQuery::class,
19
+ 'usingPlainQuery ' => PlainToTsQuery::class,
20
+ 'usingTsQuery ' => ToTsQuery::class,
21
+ 'usingWebSearchQuery ' => WebSearchToTsQuery::class,
22
+ ];
23
+ }
24
+
15
25
public function boot ()
16
26
{
17
27
$ this ->app ->make (EngineManager::class)->extend ('pgsql ' , function () {
@@ -21,40 +31,17 @@ public function boot()
21
31
);
22
32
});
23
33
24
- if (! Builder::hasMacro ('usingPhraseQuery ' )) {
25
- Builder::macro ('usingPhraseQuery ' , function () {
26
- $ this ->callback = function ($ builder , $ config ) {
27
- return new PhraseToTsQuery ($ builder ->query , $ config );
28
- };
29
-
30
- return $ this ;
31
- });
32
- }
33
-
34
- if (! Builder::hasMacro ('usingPlainQuery ' )) {
35
- Builder::macro ('usingPlainQuery ' , function () {
36
- $ this ->callback = function ($ builder , $ config ) {
37
- return new PlainToTsQuery ($ builder ->query , $ config );
38
- };
39
-
40
- return $ this ;
41
- });
42
- }
43
-
44
- if (! Builder::hasMacro ('usingTsQuery ' )) {
45
- Builder::macro ('usingTsQuery ' , function () {
46
- $ this ->callback = function ($ builder , $ config ) {
47
- return new ToTsQuery ($ builder ->query , $ config );
48
- };
49
-
50
- return $ this ;
51
- });
34
+ foreach (self ::builderMacros () as $ macro => $ class ) {
35
+ $ this ->registerBuilderMacro ($ macro , $ class );
52
36
}
37
+ }
53
38
54
- if (! Builder::hasMacro ('usingWebSearchQuery ' )) {
55
- Builder::macro ('usingWebSearchQuery ' , function () {
56
- $ this ->callback = function ($ builder , $ config ) {
57
- return new WebSearchToTsQuery ($ builder ->query , $ config );
39
+ protected function registerBuilderMacro ($ name , $ class )
40
+ {
41
+ if (! Builder::hasMacro ($ name )) {
42
+ Builder::macro ($ name , function () use ($ class ) {
43
+ $ this ->callback = function ($ builder , $ config ) use ($ class ) {
44
+ return new $ class ($ builder ->query , $ config );
58
45
};
59
46
60
47
return $ this ;
0 commit comments