1212
1313class PostgresEngineServiceProvider extends ServiceProvider
1414{
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+
1525 public function boot ()
1626 {
1727 $ this ->app ->make (EngineManager::class)->extend ('pgsql ' , function () {
@@ -21,40 +31,17 @@ public function boot()
2131 );
2232 });
2333
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 );
5236 }
37+ }
5338
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 );
5845 };
5946
6047 return $ this ;
0 commit comments