-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HttpFoundation] Create migration for session table when pdo handler …
…is used
- Loading branch information
1 parent
929d5f4
commit ac62915
Showing
13 changed files
with
226 additions
and
67 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/Symfony/Bridge/Doctrine/SchemaListener/AbstractSchemaSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\SchemaListener; | ||
|
||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception\TableNotFoundException; | ||
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | ||
use Doctrine\ORM\Tools\ToolEvents; | ||
|
||
abstract class AbstractSchemaSubscriber implements EventSubscriber | ||
{ | ||
abstract public function postGenerateSchema(GenerateSchemaEventArgs $event): void; | ||
|
||
public function getSubscribedEvents(): array | ||
{ | ||
if (!class_exists(ToolEvents::class)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
ToolEvents::postGenerateSchema, | ||
]; | ||
} | ||
|
||
protected function getIsSameDatabaseChecker(Connection $connection): \Closure | ||
{ | ||
return static function (\Closure $exec) use ($connection): bool { | ||
$checkTable = 'schema_subscriber_check_'.bin2hex(random_bytes(7)); | ||
$connection->executeStatement(sprintf('CREATE TABLE %s (id INTEGER NOT NULL)', $checkTable)); | ||
|
||
try { | ||
$exec(sprintf('DROP TABLE %s', $checkTable)); | ||
} catch (\Exception) { | ||
// ignore | ||
} | ||
|
||
try { | ||
$connection->executeStatement(sprintf('DROP TABLE %s', $checkTable)); | ||
|
||
return false; | ||
} catch (TableNotFoundException) { | ||
return true; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Symfony/Bridge/Doctrine/SchemaListener/PdoSessionHandlerSchemaSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\SchemaListener; | ||
|
||
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; | ||
|
||
final class PdoSessionHandlerSchemaSubscriber extends AbstractSchemaSubscriber | ||
{ | ||
private array $pdoSessionHandlers; | ||
|
||
/** | ||
* @param iterable<mixed, PdoSessionHandler> $pdoSessionHandlers | ||
*/ | ||
public function __construct(iterable $pdoSessionHandlers) | ||
{ | ||
$this->pdoSessionHandlers = $pdoSessionHandlers; | ||
} | ||
|
||
public function postGenerateSchema(GenerateSchemaEventArgs $event): void | ||
{ | ||
$connection = $event->getEntityManager()->getConnection(); | ||
|
||
foreach ($this->pdoSessionHandlers as $pdoSessionHandler) { | ||
$pdoSessionHandler->configureSchema($event->getSchema(), $connection, $this->getIsSameDatabaseChecker($connection)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/Symfony/Bridge/Doctrine/Tests/SchemaListener/PdoSessionHandlerSchemaSubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\SchemaListener; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Doctrine\SchemaListener\PdoSessionHandlerSchemaSubscriber; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; | ||
|
||
class PdoSessionHandlerSchemaSubscriberTest extends TestCase | ||
{ | ||
public function testPostGenerateSchemaPdo() | ||
{ | ||
$schema = new Schema(); | ||
$dbalConnection = $this->createMock(Connection::class); | ||
$entityManager = $this->createMock(EntityManagerInterface::class); | ||
$entityManager->expects($this->once()) | ||
->method('getConnection') | ||
->willReturn($dbalConnection); | ||
$event = new GenerateSchemaEventArgs($entityManager, $schema); | ||
|
||
$pdoSessionHandler = $this->createMock(PdoSessionHandler::class); | ||
$pdoSessionHandler->expects($this->once()) | ||
->method('configureSchema') | ||
->with($schema, $dbalConnection); | ||
|
||
$subscriber = new PdoSessionHandlerSchemaSubscriber($pdoSessionHandler); | ||
$subscriber->postGenerateSchema($event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.