Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPORM-28 Add Scout engine to index into MongoDB Search #3205

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ jobs:
until docker exec --tty mongodb mongosh 127.0.0.1:27017 --eval "db.runCommand({ ping: 1 })"; do
sleep 1
done
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure mongot is ready.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In docker-compose.yml you removed the host/port from the mongosh invocation. Should you do so here as well for consistency? I'm OK with either but wanted to ask.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update it as part of PHPORM-287

sleep 1
done

- name: "Show MongoDB server status"
run: |
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"require-dev": {
"mongodb/builder": "^0.2",
"laravel/scout": "^11",
"league/flysystem-gridfs": "^3.28",
"league/flysystem-read-only": "^3.0",
"phpunit/phpunit": "^10.3",
Expand Down
6 changes: 2 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.5'

services:
app:
tty: true
Expand All @@ -16,11 +14,11 @@ services:

mongodb:
container_name: mongodb
image: mongo:latest
image: mongodb/mongodb-atlas-local:latest
ports:
jmikola marked this conversation as resolved.
Show resolved Hide resolved
- "27017:27017"
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh mongodb:27017 --quiet
test: mongosh --quiet --eval 'db.runCommand("ping").ok'
interval: 10s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this relies on mongosh defaulting `localhost:27017"

timeout: 10s
retries: 5
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ parameters:
message: "#^Method Illuminate\\\\Database\\\\Schema\\\\Blueprint\\:\\:create\\(\\) invoked with 1 parameter, 0 required\\.$#"
count: 1
path: src/Schema/Builder.php

-
message: "#^Call to an undefined method Illuminate\\\\Support\\\\HigherOrderCollectionProxy\\<\\(int\\|string\\), Illuminate\\\\Database\\\\Eloquent\\\\Model\\>\\:\\:pushSoftDeleteMetadata\\(\\)\\.$#"
count: 1
path: src/Scout/ScoutEngine.php
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</testsuite>
</testsuites>
<php>
<env name="MONGODB_URI" value="mongodb://mongodb/"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the mongodb hostname a sensible hostname here? In #3205 (comment) you mentioned removing it in favor of the default of localhost:27017. I understand that you'd need to use an explicit value in this context, but I'd expect to see localhost:27017 instead of mongodb in that case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the default localhost from the mongodb container, but from the other container, you need to specify the mongodb container name.

<env name="MONGODB_URI" value="mongodb://mongodb/?directConnection=true"/>
<env name="MONGODB_DATABASE" value="unittest"/>
<env name="SQLITE_DATABASE" value=":memory:"/>
<env name="QUEUE_CONNECTION" value="database"/>
Expand Down
21 changes: 21 additions & 0 deletions src/MongoDBServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
use Closure;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\Repository;
use Illuminate\Container\Container;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Foundation\Application;
use Illuminate\Session\SessionManager;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Laravel\Scout\EngineManager;
use League\Flysystem\Filesystem;
use League\Flysystem\GridFS\GridFSAdapter;
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
use MongoDB\GridFS\Bucket;
use MongoDB\Laravel\Cache\MongoStore;
use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Queue\MongoConnector;
use MongoDB\Laravel\Scout\ScoutEngine;
use RuntimeException;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;

Expand Down Expand Up @@ -102,6 +105,7 @@ public function register()
});

$this->registerFlysystemAdapter();
$this->registerScoutEngine();
}

private function registerFlysystemAdapter(): void
Expand Down Expand Up @@ -155,4 +159,21 @@ private function registerFlysystemAdapter(): void
});
});
}

private function registerScoutEngine(): void
{
$this->app->resolving(EngineManager::class, function (EngineManager $engineManager) {
$engineManager->extend('mongodb', function (Container $app) {
$connectionName = $app->get('config')->get('scout.mongodb.connection', 'mongodb');
$connection = $app->get('db')->connection($connectionName);
$softDelete = (bool) $app->get('config')->get('scout.soft_delete', false);

assert($connection instanceof Connection, new InvalidArgumentException(sprintf('The connection "%s" is not a MongoDB connection.', $connectionName)));

return new ScoutEngine($connection->getMongoDB(), $softDelete);
});

return $engineManager;
});
}
}
Loading
Loading