Skip to content

Commit 73c138d

Browse files
committed
Move scout engine declaration to the main MongoDBServiceProvider
1 parent 86ecad5 commit 73c138d

File tree

4 files changed

+23
-43
lines changed

4 files changed

+23
-43
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@
7171
"laravel": {
7272
"providers": [
7373
"MongoDB\\Laravel\\MongoDBServiceProvider",
74-
"MongoDB\\Laravel\\MongoDBBusServiceProvider",
75-
"MongoDB\\Laravel\\Scout\\MongoDBScoutServiceProvider"
74+
"MongoDB\\Laravel\\MongoDBBusServiceProvider"
7675
]
7776
}
7877
},

src/MongoDBServiceProvider.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77
use Closure;
88
use Illuminate\Cache\CacheManager;
99
use Illuminate\Cache\Repository;
10+
use Illuminate\Container\Container;
1011
use Illuminate\Filesystem\FilesystemAdapter;
1112
use Illuminate\Filesystem\FilesystemManager;
1213
use Illuminate\Foundation\Application;
1314
use Illuminate\Session\SessionManager;
1415
use Illuminate\Support\ServiceProvider;
1516
use InvalidArgumentException;
17+
use Laravel\Scout\EngineManager;
1618
use League\Flysystem\Filesystem;
1719
use League\Flysystem\GridFS\GridFSAdapter;
1820
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
1921
use MongoDB\GridFS\Bucket;
2022
use MongoDB\Laravel\Cache\MongoStore;
2123
use MongoDB\Laravel\Eloquent\Model;
2224
use MongoDB\Laravel\Queue\MongoConnector;
25+
use MongoDB\Laravel\Scout\ScoutEngine;
2326
use RuntimeException;
2427
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
2528

@@ -102,6 +105,7 @@ public function register()
102105
});
103106

104107
$this->registerFlysystemAdapter();
108+
$this->registerScoutEngine();
105109
}
106110

107111
private function registerFlysystemAdapter(): void
@@ -155,4 +159,21 @@ private function registerFlysystemAdapter(): void
155159
});
156160
});
157161
}
162+
163+
private function registerScoutEngine(): void
164+
{
165+
$this->app->resolving(EngineManager::class, function (EngineManager $engineManager) {
166+
$engineManager->extend('mongodb', function (Container $app) {
167+
$connectionName = $app->get('config')->get('scout.mongodb.connection', 'mongodb');
168+
$connection = $app->get('db')->connection($connectionName);
169+
$softDelete = (bool) $app->get('config')->get('scout.soft_delete', false);
170+
171+
assert($connection instanceof Connection, new InvalidArgumentException(sprintf('The connection "%s" is not a MongoDB connection.', $connectionName)));
172+
173+
return new ScoutEngine($connection->getMongoDB(), $softDelete);
174+
});
175+
176+
return $engineManager;
177+
});
178+
}
158179
}

src/Scout/MongoDBScoutServiceProvider.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/Scout/ScoutIntegrationTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Support\LazyCollection;
88
use Laravel\Scout\ScoutServiceProvider;
99
use LogicException;
10-
use MongoDB\Laravel\Scout\MongoDBScoutServiceProvider;
1110
use MongoDB\Laravel\Tests\Scout\Models\ScoutUser;
1211
use MongoDB\Laravel\Tests\Scout\Models\SearchableInSameNamespace;
1312
use MongoDB\Laravel\Tests\TestCase;
@@ -27,10 +26,7 @@ class ScoutIntegrationTest extends TestCase
2726
#[Override]
2827
protected function getPackageProviders($app): array
2928
{
30-
return array_merge(parent::getPackageProviders($app), [
31-
ScoutServiceProvider::class,
32-
MongoDBScoutServiceProvider::class,
33-
]);
29+
return array_merge(parent::getPackageProviders($app), [ScoutServiceProvider::class]);
3430
}
3531

3632
#[Override]

0 commit comments

Comments
 (0)