Skip to content

Commit 6b4cf67

Browse files
committed
Added ability to log elastic requests to application logger.
1 parent a51d1b4 commit 6b4cf67

File tree

2 files changed

+159
-7
lines changed

2 files changed

+159
-7
lines changed

src/ElasticsearchServiceProvider.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,40 @@
22

33
namespace BoxedCode\Laravel\Scout;
44

5-
use Laravel\Scout\EngineManager;
6-
use Illuminate\Support\ServiceProvider;
75
use Elasticsearch\ClientBuilder as ElasticBuilder;
6+
use Illuminate\Support\ServiceProvider;
7+
use Laravel\Scout\EngineManager;
8+
use Psr\Log\LoggerInterface;
89

910
class ElasticsearchServiceProvider extends ServiceProvider
1011
{
12+
public function register()
13+
{
14+
// Bind the elastic connection builder to the
15+
// container if it's not already present.
16+
if (!$this->app->bound(ElasticBuilder::class)) {
17+
$this->app->bind(ElasticBuilder::class, function() {
18+
return new ElasticBuilder();
19+
});
20+
}
21+
}
22+
1123
/**
1224
* Bootstrap the application services.
1325
*/
1426
public function boot()
1527
{
16-
app(EngineManager::class)->extend('elasticsearch', function($app) {
17-
$client = ElasticBuilder::create()
18-
->setHosts(config('scout.elasticsearch.hosts'))
19-
->build();
28+
$this->app->make(EngineManager::class)->extend('elasticsearch', function() {
29+
$config = $this->app->make('config');
30+
31+
$builder = $this->app->make(ElasticBuilder::class)
32+
->setHosts($config->get('scout.elasticsearch.hosts'));
33+
34+
if ($config->get('scout.elasticsearch.debug', false)) {
35+
$builder->setLogger($this->app->make(LoggerInterface::class));
36+
}
2037

21-
return new ElasticsearchEngine($client);
38+
return new ElasticsearchEngine($builder->build());
2239
});
2340
}
2441
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use BoxedCode\Laravel\Scout\ElasticsearchEngine;
6+
use BoxedCode\Laravel\Scout\ElasticsearchServiceProvider;
7+
use Elasticsearch\ClientBuilder as ElasticBuilder;
8+
use Illuminate\Contracts\Config\Repository as Config;
9+
use Illuminate\Contracts\Container\Container;
10+
use Laravel\Scout\EngineManager;
11+
use Mockery;
12+
use Psr\Log\LoggerInterface;
13+
use \Elasticsearch\Client as ElasticClient;
14+
15+
class ElasticsearchServiceProviderTestCase extends AbstractTestCase
16+
{
17+
public function test_binds_elastic_builder()
18+
{
19+
$app = Mockery::mock(Container::class);
20+
21+
$provider = new ElasticsearchServiceProvider($app);
22+
23+
$app->shouldReceive('bound')
24+
->once()
25+
->with(ElasticBuilder::class)
26+
->andReturnFalse();
27+
28+
$app->shouldReceive('bind')
29+
->once()
30+
->with(ElasticBuilder::class, Mockery::any())
31+
->andReturnNull();
32+
33+
$this->assertNull($provider->register());
34+
}
35+
36+
public function test_honors_currently_bound_elastic_builder()
37+
{
38+
$app = Mockery::mock(Container::class);
39+
40+
$provider = new ElasticsearchServiceProvider($app);
41+
42+
$app->shouldReceive('bound')
43+
->once()
44+
->with(ElasticBuilder::class)
45+
->andReturnTrue();
46+
47+
$this->assertNull($provider->register());
48+
}
49+
50+
protected function setupEngineRegistration()
51+
{
52+
$app = Mockery::mock(Container::class);
53+
54+
$provider = new ElasticsearchServiceProvider($app);
55+
56+
$config = Mockery::mock(Config::class);
57+
58+
$app->shouldReceive('make')
59+
->twice()
60+
->with('config')
61+
->andReturn($config);
62+
63+
$app->shouldReceive('make')
64+
->once()
65+
->with(EngineManager::class)
66+
->andReturn(
67+
$manager = new EngineManager($app)
68+
);
69+
70+
$app->shouldReceive('make')
71+
->once()
72+
->with(ElasticBuilder::class)
73+
->andReturn(
74+
$builder = Mockery::mock(ElasticBuilder::class)
75+
);
76+
77+
$config->shouldReceive('get')
78+
->once()
79+
->with('scout.elasticsearch.hosts')
80+
->andReturn(['127.0.0.1']);
81+
82+
$builder->shouldReceive('setHosts')
83+
->once()
84+
->with(['127.0.0.1'])
85+
->andReturnSelf();
86+
87+
$builder->shouldReceive('build')
88+
->once()
89+
->andReturn(
90+
$elastic = Mockery::mock(ElasticClient::class)
91+
);
92+
93+
return [$app, $config, $provider, $manager, $builder];
94+
}
95+
96+
public function test_engine_registration()
97+
{
98+
[$app, $config, $provider, $manager, $builder] = $this->setupEngineRegistration();
99+
100+
$config->shouldReceive('get')
101+
->once()
102+
->with('scout.elasticsearch.debug', false)
103+
->andReturnFalse();
104+
105+
$provider->boot();
106+
107+
$this->assertInstanceOf(ElasticsearchEngine::class, $manager->engine('elasticsearch'));
108+
}
109+
110+
public function test_engine_registration_with_debug_logger()
111+
{
112+
[$app, $config, $provider, $manager, $builder] = $this->setupEngineRegistration();
113+
114+
$config->shouldReceive('get')
115+
->once()
116+
->with('scout.elasticsearch.debug', false)
117+
->andReturnTrue();
118+
119+
$app->shouldReceive('make')
120+
->once()
121+
->with(LoggerInterface::class)
122+
->andReturn(
123+
$logger = Mockery::mock(LoggerInterface::class)
124+
);
125+
126+
$builder->shouldReceive('setLogger')
127+
->once()
128+
->with($logger)
129+
->andReturnSelf();
130+
131+
$provider->boot();
132+
133+
$this->assertInstanceOf(ElasticsearchEngine::class, $manager->engine('elasticsearch'));
134+
}
135+
}

0 commit comments

Comments
 (0)