Skip to content

Commit 2eb8cf2

Browse files
committed
PHPORM-28 Add Scout engined to index into MongoDB Search
Move scout engine declaration to the main MongoDBServiceProvider
1 parent 697c36f commit 2eb8cf2

14 files changed

+1497
-8
lines changed

Diff for: .github/workflows/build-ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ jobs:
6565
until docker exec --tty mongodb mongosh 127.0.0.1:27017 --eval "db.runCommand({ ping: 1 })"; do
6666
sleep 1
6767
done
68+
until docker exec --tty mongodb mongosh 127.0.0.1:27017 --eval "db.createCollection('connection_test') && db.getCollection('connection_test').createSearchIndex({mappings:{dynamic: true}})"; do
69+
sleep 1
70+
done
6871
6972
- name: "Show MongoDB server status"
7073
run: |

Diff for: composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
"require-dev": {
3737
"mongodb/builder": "^0.2",
38+
"laravel/scout": "^11",
3839
"league/flysystem-gridfs": "^3.28",
3940
"league/flysystem-read-only": "^3.0",
4041
"phpunit/phpunit": "^10.3",

Diff for: docker-compose.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.5'
2-
31
services:
42
app:
53
tty: true
@@ -16,11 +14,11 @@ services:
1614

1715
mongodb:
1816
container_name: mongodb
19-
image: mongo:latest
17+
image: mongodb/mongodb-atlas-local:latest
2018
ports:
2119
- "27017:27017"
2220
healthcheck:
23-
test: echo 'db.runCommand("ping").ok' | mongosh mongodb:27017 --quiet
21+
test: mongosh mongodb:27017 --quiet --eval 'db.runCommand("ping").ok'
2422
interval: 10s
2523
timeout: 10s
2624
retries: 5

Diff for: phpstan-baseline.neon

+5
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ parameters:
2424
message: "#^Method Illuminate\\\\Database\\\\Schema\\\\Blueprint\\:\\:create\\(\\) invoked with 1 parameter, 0 required\\.$#"
2525
count: 1
2626
path: src/Schema/Builder.php
27+
28+
-
29+
message: "#^Call to an undefined method Illuminate\\\\Support\\\\HigherOrderCollectionProxy\\<\\(int\\|string\\), Illuminate\\\\Database\\\\Eloquent\\\\Model\\>\\:\\:pushSoftDeleteMetadata\\(\\)\\.$#"
30+
count: 1
31+
path: src/Scout/ScoutEngine.php

Diff for: src/MongoDBServiceProvider.php

+21
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
}

0 commit comments

Comments
 (0)