1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * This file is part of Scout Extended. |
||
7 | * |
||
8 | * (c) Algolia Team <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Algolia\ScoutExtended; |
||
15 | |||
16 | use Algolia\AlgoliaSearch\AnalyticsClient; |
||
17 | use Algolia\AlgoliaSearch\SearchClient; |
||
18 | use Algolia\ScoutExtended\Console\Commands\FlushCommand; |
||
19 | use Algolia\ScoutExtended\Console\Commands\ImportCommand; |
||
20 | use Algolia\ScoutExtended\Console\Commands\MakeAggregatorCommand; |
||
21 | use Algolia\ScoutExtended\Console\Commands\OptimizeCommand; |
||
22 | use Algolia\ScoutExtended\Console\Commands\ReImportCommand; |
||
23 | use Algolia\ScoutExtended\Console\Commands\StatusCommand; |
||
24 | use Algolia\ScoutExtended\Console\Commands\SyncCommand; |
||
25 | use Algolia\ScoutExtended\Engines\AlgoliaEngine; |
||
26 | use Algolia\ScoutExtended\Helpers\SearchableFinder; |
||
27 | use Algolia\ScoutExtended\Jobs\UpdateJob; |
||
28 | use Algolia\ScoutExtended\Managers\EngineManager; |
||
29 | use Algolia\ScoutExtended\Searchable\AggregatorObserver; |
||
30 | use Illuminate\Support\ServiceProvider; |
||
31 | use Laravel\Scout\ScoutServiceProvider; |
||
32 | |||
33 | final class ScoutExtendedServiceProvider extends ServiceProvider |
||
34 | { |
||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | 61 | public function boot(): void |
|
39 | { |
||
40 | 61 | $this->loadViewsFrom(__DIR__.'/../resources/views', 'algolia'); |
|
41 | 61 | } |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 61 | public function register(): void |
|
47 | { |
||
48 | 61 | $this->app->register(ScoutServiceProvider::class); |
|
49 | |||
50 | 61 | $this->registerBinds(); |
|
51 | 61 | $this->registerCommands(); |
|
52 | 61 | $this->registerMacros(); |
|
53 | 61 | } |
|
54 | |||
55 | /** |
||
56 | * Binds Algolia services into the container. |
||
57 | * |
||
58 | * @return void |
||
59 | */ |
||
60 | 61 | private function registerBinds(): void |
|
61 | { |
||
62 | $this->app->bind(Algolia::class, function () { |
||
63 | 23 | return new Algolia($this->app); |
|
64 | 61 | }); |
|
65 | |||
66 | 61 | $this->app->alias(Algolia::class, 'algolia'); |
|
67 | |||
68 | $this->app->singleton(EngineManager::class, function ($app) { |
||
69 | 56 | return new EngineManager($app); |
|
70 | 61 | }); |
|
71 | |||
72 | 61 | $this->app->alias(EngineManager::class, \Laravel\Scout\EngineManager::class); |
|
73 | |||
74 | $this->app->bind(AlgoliaEngine::class, function (): AlgoliaEngine { |
||
75 | 54 | return $this->app->make(\Laravel\Scout\EngineManager::class)->createAlgoliaDriver(); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
76 | 61 | }); |
|
77 | |||
78 | 61 | $this->app->alias(AlgoliaEngine::class, 'algolia.engine'); |
|
79 | $this->app->bind(SearchClient::class, function (): SearchClient { |
||
80 | 53 | return $this->app->make('algolia.engine')->getClient(); |
|
81 | 61 | }); |
|
82 | |||
83 | 61 | $this->app->alias(SearchClient::class, 'algolia.client'); |
|
84 | |||
85 | $this->app->bind(AnalyticsClient::class, function (): AnalyticsClient { |
||
86 | 1 | return AnalyticsClient::create(config('scout.algolia.id'), config('scout.algolia.secret')); |
|
87 | 61 | }); |
|
88 | |||
89 | 61 | $this->app->alias(AnalyticsClient::class, 'algolia.analytics'); |
|
90 | |||
91 | 61 | $this->app->singleton(AggregatorObserver::class, AggregatorObserver::class); |
|
92 | 61 | $this->app->bind(\Laravel\Scout\Builder::class, Builder::class); |
|
93 | |||
94 | $this->app->bind(SearchableFinder::class, function () { |
||
95 | 17 | return new SearchableFinder($this->app); |
|
96 | 61 | }); |
|
97 | 61 | } |
|
98 | |||
99 | /** |
||
100 | * Register artisan commands. |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | 61 | private function registerCommands(): void |
|
105 | { |
||
106 | 61 | if ($this->app->runningInConsole()) { |
|
107 | 61 | $this->commands([ |
|
108 | 61 | MakeAggregatorCommand::class, |
|
109 | ImportCommand::class, |
||
110 | FlushCommand::class, |
||
111 | OptimizeCommand::class, |
||
112 | ReImportCommand::class, |
||
113 | StatusCommand::class, |
||
114 | SyncCommand::class, |
||
115 | ]); |
||
116 | } |
||
117 | 61 | } |
|
118 | |||
119 | /** |
||
120 | * Register macros. |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | 61 | private function registerMacros(): void |
|
125 | { |
||
126 | \Illuminate\Database\Eloquent\Builder::macro('transform', function (array $array, array $transformers = null) { |
||
127 | 15 | foreach ($transformers ?? UpdateJob::getTransformers() as $transformer) { |
|
128 | 15 | $array = app($transformer)->transform($this->getModel(), $array); |
|
129 | } |
||
130 | |||
131 | 15 | return $array; |
|
132 | 61 | }); |
|
133 | 61 | } |
|
134 | } |
||
135 |