Skip to content

Commit 54e5519

Browse files
committed
Dry service provider code
1 parent 02a578c commit 54e5519

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

src/PostgresEngineServiceProvider.php

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212

1313
class 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;

tests/PostgresEngineServiceProviderTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,10 @@ public function test_it_creates_macros()
3030

3131
$builder = new Builder(Mockery::mock(Model::class), '');
3232

33-
foreach ([
34-
'usingPhraseQuery' => PhraseToTsQuery::class,
35-
'usingPlainQuery' => PlainToTsQuery::class,
36-
'usingTsQuery' => ToTsQuery::class,
37-
'usingWebSearchQuery' => WebSearchToTsQuery::class,
38-
] as $macro => $class) {
39-
$this->assertTrue(Builder::hasMacro($macro));
33+
foreach (PostgresEngineServiceProvider::builderMacros() as $name => $class) {
34+
$this->assertTrue(Builder::hasMacro($name));
4035

41-
$callback = $builder->{$macro}()->callback;
36+
$callback = $builder->{$name}()->callback;
4237
$tsFunction = $callback($builder, []);
4338
$this->assertInstanceOf($class, $tsFunction);
4439
}

0 commit comments

Comments
 (0)