Skip to content

Commit

Permalink
bug #49097 [HttpFoundation] inject SessionHandler in PdoSessionHandle…
Browse files Browse the repository at this point in the history
…rSchemaSubscriber (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
  • Loading branch information
nicolas-grekas committed Jan 29, 2023
2 parents b497938 + 9aded06 commit a08a41c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@

final class PdoSessionHandlerSchemaSubscriber extends AbstractSchemaSubscriber
{
private iterable $pdoSessionHandlers;
private PdoSessionHandler $sessionHandler;

/**
* @param iterable<mixed, PdoSessionHandler> $pdoSessionHandlers
*/
public function __construct(iterable $pdoSessionHandlers)
public function __construct(\SessionHandlerInterface $sessionHandler)
{
$this->pdoSessionHandlers = $pdoSessionHandlers;
if ($sessionHandler instanceof PdoSessionHandler) {
$this->sessionHandler = $sessionHandler;
}
}

public function getSubscribedEvents(): array
{
return isset($this->sessionHandler) ? parent::getSubscribedEvents() : [];
}

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

foreach ($this->pdoSessionHandlers as $pdoSessionHandler) {
$pdoSessionHandler->configureSchema($event->getSchema(), $this->getIsSameDatabaseChecker($connection));
}
$this->sessionHandler->configureSchema($event->getSchema(), $this->getIsSameDatabaseChecker($connection));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testPostGenerateSchemaPdo()
->method('configureSchema')
->with($schema, fn () => true);

$subscriber = new PdoSessionHandlerSchemaSubscriber([$pdoSessionHandler]);
$subscriber = new PdoSessionHandlerSchemaSubscriber($pdoSessionHandler);
$subscriber->postGenerateSchema($event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,12 @@ public function __construct(#[\SensitiveParameter] \PDO|string $pdoOrDsn = null,
$this->ttl = $options['ttl'] ?? null;
}

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

Expand Down

0 comments on commit a08a41c

Please sign in to comment.