forked from algolia/scout-extended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoutExtendedServiceProvider.php
138 lines (116 loc) · 4.26 KB
/
ScoutExtendedServiceProvider.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
<?php
declare(strict_types=1);
/**
* This file is part of Scout Extended.
*
* (c) Algolia Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Algolia\ScoutExtended;
use Algolia\AlgoliaSearch\AnalyticsClient;
use Algolia\AlgoliaSearch\SearchClient;
use Algolia\ScoutExtended\Console\Commands\FlushCommand;
use Algolia\ScoutExtended\Console\Commands\ImportCommand;
use Algolia\ScoutExtended\Console\Commands\MakeAggregatorCommand;
use Algolia\ScoutExtended\Console\Commands\OptimizeCommand;
use Algolia\ScoutExtended\Console\Commands\ReImportCommand;
use Algolia\ScoutExtended\Console\Commands\StatusCommand;
use Algolia\ScoutExtended\Console\Commands\SyncCommand;
use Algolia\ScoutExtended\Contracts\LocalSettingsRepositoryContract;
use Algolia\ScoutExtended\Engines\AlgoliaEngine;
use Algolia\ScoutExtended\Helpers\SearchableFinder;
use Algolia\ScoutExtended\Jobs\UpdateJob;
use Algolia\ScoutExtended\Managers\EngineManager;
use Algolia\ScoutExtended\Repositories\LocalSettingsRepository;
use Algolia\ScoutExtended\Searchable\AggregatorObserver;
use Illuminate\Support\ServiceProvider;
use Laravel\Scout\ScoutServiceProvider;
final class ScoutExtendedServiceProvider extends ServiceProvider
{
/**
* {@inheritdoc}
*/
public function boot(): void
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'algolia');
$this->registerCommands();
$this->registerMacros();
}
/**
* {@inheritdoc}
*/
public function register(): void
{
$this->app->register(ScoutServiceProvider::class);
$this->registerBinds();
}
/**
* Binds Algolia services into the container.
*
* @return void
*/
private function registerBinds(): void
{
$this->app->bind(Algolia::class, function ($app) {
return new Algolia($app);
});
$this->app->alias(Algolia::class, 'algolia');
$this->app->singleton(EngineManager::class, function ($app) {
return new EngineManager($app);
});
$this->app->alias(EngineManager::class, \Laravel\Scout\EngineManager::class);
$this->app->bind(AlgoliaEngine::class, function ($app): AlgoliaEngine {
return $app->make(\Laravel\Scout\EngineManager::class)->createAlgoliaDriver();
});
$this->app->alias(AlgoliaEngine::class, 'algolia.engine');
$this->app->bind(SearchClient::class, function ($app): SearchClient {
return $app->make('algolia.engine')->getClient();
});
$this->app->alias(SearchClient::class, 'algolia.client');
$this->app->bind(AnalyticsClient::class, function (): AnalyticsClient {
return AnalyticsClient::create(config('scout.algolia.id'), config('scout.algolia.secret'));
});
$this->app->alias(AnalyticsClient::class, 'algolia.analytics');
$this->app->singleton(AggregatorObserver::class, AggregatorObserver::class);
$this->app->bind(\Laravel\Scout\Builder::class, Builder::class);
$this->app->bind(SearchableFinder::class, function ($app) {
return new SearchableFinder($app);
});
$this->app->singleton(LocalSettingsRepositoryContract::class, LocalSettingsRepository::class);
}
/**
* Register artisan commands.
*
* @return void
*/
private function registerCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
MakeAggregatorCommand::class,
ImportCommand::class,
FlushCommand::class,
OptimizeCommand::class,
ReImportCommand::class,
StatusCommand::class,
SyncCommand::class,
]);
}
}
/**
* Register macros.
*
* @return void
*/
private function registerMacros(): void
{
\Illuminate\Database\Eloquent\Builder::macro('transform', function (array $array, array $transformers = null) {
foreach ($transformers ?? UpdateJob::getTransformers() as $transformer) {
$array = app($transformer)->transform($this->getModel(), $array);
}
return $array;
});
}
}