Skip to content

Commit f5fc453

Browse files
committed
PHPORM-259 Register MongoDB Session Handler with SESSION_DRIVER=mongodb
1 parent 9973195 commit f5fc453

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

phpstan-baseline.neon

+25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\<static\\(MongoDB\\\\Laravel\\\\Auth\\\\User\\)\\>\\:\\:pull\\(\\)\\.$#"
5+
count: 1
6+
path: src/Auth/User.php
7+
8+
-
9+
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\<static\\(MongoDB\\\\Laravel\\\\Auth\\\\User\\)\\>\\:\\:push\\(\\)\\.$#"
10+
count: 1
11+
path: src/Auth/User.php
12+
13+
-
14+
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\<static\\(MongoDB\\\\Laravel\\\\Eloquent\\\\Model\\)\\>\\:\\:pull\\(\\)\\.$#"
15+
count: 1
16+
path: src/Eloquent/Model.php
17+
18+
-
19+
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\<static\\(MongoDB\\\\Laravel\\\\Eloquent\\\\Model\\)\\>\\:\\:push\\(\\)\\.$#"
20+
count: 1
21+
path: src/Eloquent/Model.php
22+
323
-
424
message: "#^Access to an undefined property Illuminate\\\\Container\\\\Container\\:\\:\\$config\\.$#"
525
count: 3
626
path: src/MongoDBBusServiceProvider.php
727

28+
-
29+
message: "#^Access to an undefined property Illuminate\\\\Foundation\\\\Application\\:\\:\\$config\\.$#"
30+
count: 4
31+
path: src/MongoDBServiceProvider.php
32+
833
-
934
message: "#^Method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:push\\(\\) invoked with 3 parameters, 0 required\\.$#"
1035
count: 3

src/MongoDBServiceProvider.php

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

+42-1
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)