Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a08a41c

Browse files
bug #49097 [HttpFoundation] inject SessionHandler in PdoSessionHandlerSchemaSubscriber (alli83)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpFoundation] inject SessionHandler in PdoSessionHandlerSchemaSubscriber | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT | Doc PR | I'm working on it following the PR #48059. In #48059, it works only if you put the handler explicitly to PdoSessionHandler in the configuration (handler_id) but it stops working if the session is defined by the factory and we can't use it as follow: ```` handler_id: '%env(resolve:DATABASE_URL)%' ````` Linked to DoctrineBundle PR doctrine/DoctrineBundle#1623 Also imho, I think the second argument of configureSchema inPdoSessionHandler should be optional and set to return true if not defined since the function is public eg. one wants to add the table to the Schema. Commits ------- 9aded06 [HttpFoundation] inject SessionHandler in PdoSessionHandlerSchemaSubscriber
2 parents b497938 + 9aded06 commit a08a41c

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/Symfony/Bridge/Doctrine/SchemaListener/PdoSessionHandlerSchemaSubscriber.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@
1616

1717
final class PdoSessionHandlerSchemaSubscriber extends AbstractSchemaSubscriber
1818
{
19-
private iterable $pdoSessionHandlers;
19+
private PdoSessionHandler $sessionHandler;
2020

21-
/**
22-
* @param iterable<mixed, PdoSessionHandler> $pdoSessionHandlers
23-
*/
24-
public function __construct(iterable $pdoSessionHandlers)
21+
public function __construct(\SessionHandlerInterface $sessionHandler)
2522
{
26-
$this->pdoSessionHandlers = $pdoSessionHandlers;
23+
if ($sessionHandler instanceof PdoSessionHandler) {
24+
$this->sessionHandler = $sessionHandler;
25+
}
26+
}
27+
28+
public function getSubscribedEvents(): array
29+
{
30+
return isset($this->sessionHandler) ? parent::getSubscribedEvents() : [];
2731
}
2832

2933
public function postGenerateSchema(GenerateSchemaEventArgs $event): void
3034
{
3135
$connection = $event->getEntityManager()->getConnection();
3236

33-
foreach ($this->pdoSessionHandlers as $pdoSessionHandler) {
34-
$pdoSessionHandler->configureSchema($event->getSchema(), $this->getIsSameDatabaseChecker($connection));
35-
}
37+
$this->sessionHandler->configureSchema($event->getSchema(), $this->getIsSameDatabaseChecker($connection));
3638
}
3739
}

src/Symfony/Bridge/Doctrine/Tests/SchemaListener/PdoSessionHandlerSchemaSubscriberTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testPostGenerateSchemaPdo()
3636
->method('configureSchema')
3737
->with($schema, fn () => true);
3838

39-
$subscriber = new PdoSessionHandlerSchemaSubscriber([$pdoSessionHandler]);
39+
$subscriber = new PdoSessionHandlerSchemaSubscriber($pdoSessionHandler);
4040
$subscriber->postGenerateSchema($event);
4141
}
4242
}

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,12 @@ public function __construct(#[\SensitiveParameter] \PDO|string $pdoOrDsn = null,
178178
$this->ttl = $options['ttl'] ?? null;
179179
}
180180

181-
public function configureSchema(Schema $schema, \Closure $isSameDatabase): void
181+
/**
182+
* Adds the Table to the Schema if it doesn't exist.
183+
*/
184+
public function configureSchema(Schema $schema, \Closure $isSameDatabase = null): void
182185
{
183-
if ($schema->hasTable($this->table) || !$isSameDatabase($this->getConnection()->exec(...))) {
186+
if ($schema->hasTable($this->table) || ($isSameDatabase && !$isSameDatabase($this->getConnection()->exec(...)))) {
184187
return;
185188
}
186189

0 commit comments

Comments
 (0)