Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8cf9f66

Browse files
authored
PHPORM-259 Register MongoDB Session Handler with SESSION_DRIVER=mongodb (#3192)
* PHPORM-259 Register MongoDB Session Handler with SESSION_DRIVER=mongodb * Explicit dependency to symfony/http-foundation
1 parent 4fcf67b commit 8cf9f66

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"illuminate/database": "^10.30|^11",
3131
"illuminate/events": "^10.0|^11",
3232
"illuminate/support": "^10.0|^11",
33-
"mongodb/mongodb": "^1.18"
33+
"mongodb/mongodb": "^1.18",
34+
"symfony/http-foundation": "^6.4|^7"
3435
},
3536
"require-dev": {
3637
"mongodb/builder": "^0.2",

‎phpstan-baseline.neon‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ parameters:
55
count: 3
66
path: src/MongoDBBusServiceProvider.php
77

8+
-
9+
message: "#^Access to an undefined property Illuminate\\\\Foundation\\\\Application\\:\\:\\$config\\.$#"
10+
count: 4
11+
path: src/MongoDBServiceProvider.php
12+
813
-
914
message: "#^Method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:push\\(\\) invoked with 3 parameters, 0 required\\.$#"
1015
count: 3

‎src/MongoDBServiceProvider.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Filesystem\FilesystemAdapter;
1111
use Illuminate\Filesystem\FilesystemManager;
1212
use Illuminate\Foundation\Application;
13+
use Illuminate\Session\SessionManager;
1314
use Illuminate\Support\ServiceProvider;
1415
use InvalidArgumentException;
1516
use League\Flysystem\Filesystem;
@@ -20,6 +21,7 @@
2021
use MongoDB\Laravel\Eloquent\Model;
2122
use MongoDB\Laravel\Queue\MongoConnector;
2223
use RuntimeException;
24+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
2325

2426
use function assert;
2527
use function class_exists;
@@ -53,6 +55,25 @@ public function register()
5355
});
5456
});
5557

58+
// Session handler for MongoDB
59+
$this->app->resolving(SessionManager::class, function (SessionManager $sessionManager) {
60+
$sessionManager->extend('mongodb', function (Application $app) {
61+
$connectionName = $app->config->get('session.connection') ?: 'mongodb';
62+
$connection = $app->make('db')->connection($connectionName);
63+
64+
assert($connection instanceof Connection, new InvalidArgumentException(sprintf('The database connection "%s" used for the session does not use the "mongodb" driver.', $connectionName)));
65+
66+
return new MongoDbSessionHandler(
67+
$connection->getMongoClient(),
68+
$app->config->get('session.options', []) + [
69+
'database' => $connection->getDatabaseName(),
70+
'collection' => $app->config->get('session.table') ?: 'sessions',
71+
'ttl' => $app->config->get('session.lifetime'),
72+
],
73+
);
74+
});
75+
});
76+
5677
// Add cache and lock drivers.
5778
$this->app->resolving('cache', function (CacheManager $cache) {
5879
$cache->extend('mongodb', function (Application $app, array $config): Repository {

‎tests/SessionTest.php‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace MongoDB\Laravel\Tests;
44

55
use Illuminate\Session\DatabaseSessionHandler;
6+
use Illuminate\Session\SessionManager;
67
use Illuminate\Support\Facades\DB;
8+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
79

810
class SessionTest extends TestCase
911
{
@@ -14,7 +16,7 @@ protected function tearDown(): void
1416
parent::tearDown();
1517
}
1618

17-
public function testDatabaseSessionHandler()
19+
public function testDatabaseSessionHandlerCompatibility()
1820
{
1921
$sessionId = '123';
2022

@@ -30,4 +32,43 @@ public function testDatabaseSessionHandler()
3032
$handler->write($sessionId, 'bar');
3133
$this->assertEquals('bar', $handler->read($sessionId));
3234
}
35+
36+
public function testDatabaseSessionHandlerRegistration()
37+
{
38+
$this->app['config']->set('session.driver', 'database');
39+
$this->app['config']->set('session.connection', 'mongodb');
40+
41+
$session = $this->app['session'];
42+
$this->assertInstanceOf(SessionManager::class, $session);
43+
$this->assertInstanceOf(DatabaseSessionHandler::class, $session->getHandler());
44+
45+
$this->assertSessionCanStoreInMongoDB($session);
46+
}
47+
48+
public function testMongoDBSessionHandlerRegistration()
49+
{
50+
$this->app['config']->set('session.driver', 'mongodb');
51+
$this->app['config']->set('session.connection', 'mongodb');
52+
53+
$session = $this->app['session'];
54+
$this->assertInstanceOf(SessionManager::class, $session);
55+
$this->assertInstanceOf(MongoDbSessionHandler::class, $session->getHandler());
56+
57+
$this->assertSessionCanStoreInMongoDB($session);
58+
}
59+
60+
private function assertSessionCanStoreInMongoDB(SessionManager $session): void
61+
{
62+
$session->put('foo', 'bar');
63+
$session->save();
64+
65+
$this->assertNotNull($session->getId());
66+
67+
$data = DB::connection('mongodb')
68+
->getCollection('sessions')
69+
->findOne(['_id' => $session->getId()]);
70+
71+
self::assertIsObject($data);
72+
self::assertSame($session->getId(), $data->_id);
73+
}
3374
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /