-
-
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.
[Http-Foundation][Session]feat: create automatically session table wi…
…th pdo connection
- Loading branch information
Showing
6 changed files
with
222 additions
and
4 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
<?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\ORM\Tools\Event\GenerateSchemaEventArgs; | ||
use Doctrine\ORM\Tools\ToolEvents; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\SessionHandlerDecoratorInterface; | ||
|
||
class PdoSessionHandlerSchemaSubscriber implements EventSubscriber | ||
{ | ||
private $sessionHandler; | ||
|
||
/** | ||
* @param $sessionHandler | ||
*/ | ||
public function __construct($sessionHandler = null) | ||
{ | ||
$this->sessionHandler = $sessionHandler; | ||
} | ||
|
||
public function postGenerateSchema(GenerateSchemaEventArgs $event): void | ||
{ | ||
$dbalConnection = $event->getEntityManager()->getConnection(); | ||
|
||
$sessionHandler = $this->sessionHandler; | ||
|
||
if (!$sessionHandler instanceof PdoSessionHandler && | ||
!$sessionHandler instanceof SessionHandlerDecoratorInterface) { | ||
return; | ||
} | ||
|
||
if ($sessionHandler instanceof SessionHandlerDecoratorInterface) { | ||
$handlers = $sessionHandler->getHandlers(); | ||
|
||
foreach ($handlers as $handler) { | ||
if ($handler instanceof PdoSessionHandler) { | ||
$handler->configureSchema($event->getSchema(), $dbalConnection); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
$this->sessionHandler->configureSchema($event->getSchema(), $dbalConnection); | ||
} | ||
|
||
public function getSubscribedEvents(): array | ||
{ | ||
$subscribedEvents = []; | ||
|
||
if (class_exists(ToolEvents::class)) { | ||
$subscribedEvents[] = ToolEvents::postGenerateSchema; | ||
} | ||
|
||
return $subscribedEvents; | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...ony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerDecoratorInterface.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,20 @@ | ||
<?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\Component\HttpFoundation\Session\Storage\Handler; | ||
|
||
interface SessionHandlerDecoratorInterface | ||
{ | ||
/** | ||
* @return \SessionHandlerInterface[] | ||
*/ | ||
public function getHandlers(): array; | ||
} |