-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gabriel Nogueira Berthier
committed
Jun 6, 2023
1 parent
bc13827
commit 3e1d4a7
Showing
9 changed files
with
111 additions
and
39 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Core\Data\Doctrine; | ||
|
||
use Doctrine\DBAL\DriverManager; | ||
use Doctrine\DBAL\Types\Type; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\ORMSetup; | ||
|
||
class EntityManagerBuilder | ||
{ | ||
public static function produce(array $doctrine): EntityManager | ||
{ | ||
$devMode = $doctrine['dev_mode']; | ||
|
||
$config = ORMSetup::createAttributeMetadataConfiguration( | ||
$doctrine['metadata_dirs'], | ||
$devMode | ||
); | ||
|
||
if (!Type::hasType('uuid')) { | ||
Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType'); | ||
} | ||
|
||
|
||
$connection = DriverManager::getConnection( | ||
$doctrine['connection'], | ||
$config | ||
); | ||
|
||
$entityManager = new EntityManager($connection, $config); | ||
|
||
if (!Type::hasType('uuid_binary')) { | ||
Type::addType('uuid_binary', 'Ramsey\Uuid\Doctrine\UuidBinaryType'); | ||
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('uuid_binary', 'binary'); | ||
} | ||
|
||
return $entityManager; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Core\Decorators; | ||
|
||
use Core\Data\Doctrine\EntityManagerBuilder; | ||
use Doctrine\ORM\Decorator\EntityManagerDecorator; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class ReopeningEntityManagerDecorator extends EntityManagerDecorator | ||
{ | ||
public function __construct(private ContainerInterface $container) | ||
{ | ||
parent::__construct(EntityManagerBuilder::produce($container->get('settings')['doctrine'])); | ||
} | ||
|
||
public function open(): void | ||
{ | ||
if (!$this->wrapped->isOpen()) { | ||
$settings = $this->container->get('settings'); | ||
$doctrine = $settings['doctrine']; | ||
|
||
$this->wrapped = EntityManagerBuilder::produce($doctrine); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,43 +1,17 @@ | ||
<?php | ||
|
||
use Doctrine\DBAL\Types\Type; | ||
declare(strict_types=1); | ||
|
||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Tools\Setup; | ||
use Psr\Container\ContainerInterface; | ||
use Doctrine\Common\Cache\Psr6\DoctrineProvider; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||
use Core\Data\Doctrine\EntityManagerBuilder; | ||
|
||
return [ | ||
EntityManager::class => static function (ContainerInterface $container): EntityManager { | ||
$settings = $container->get('settings'); | ||
$doctrine = $settings['doctrine']; | ||
|
||
$cache = $doctrine['dev_mode'] ? | ||
DoctrineProvider::wrap(new ArrayAdapter()) : | ||
DoctrineProvider::wrap(new FilesystemAdapter(directory: $doctrine['cache_dir'])); | ||
|
||
$config = Setup::createAttributeMetadataConfiguration( | ||
$doctrine['metadata_dirs'], | ||
$doctrine['dev_mode'], | ||
null, | ||
$cache | ||
); | ||
|
||
if (!Type::hasType('uuid')) { | ||
Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType'); | ||
} | ||
|
||
$entityManager = EntityManager::create( | ||
$doctrine['connection'], | ||
$config | ||
); | ||
|
||
if (!Type::hasType('uuid_binary')) { | ||
Type::addType('uuid_binary', 'Ramsey\Uuid\Doctrine\UuidBinaryType'); | ||
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('uuid_binary', 'binary'); | ||
} | ||
|
||
return $entityManager; | ||
return EntityManagerBuilder::produce($doctrine); | ||
}, | ||
]; | ||
]; |
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
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,32 @@ | ||
<?php | ||
|
||
namespace Core\Http\Middlewares; | ||
|
||
|
||
use Core\Decorators\ReopeningEntityManagerDecorator; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class DatabaseKeepAliveMiddleware implements MiddlewareInterface | ||
{ | ||
public function __construct( | ||
private ReopeningEntityManagerDecorator $reopeningEntityManagerDecorator, | ||
private ContainerInterface $containerInterface | ||
) { | ||
} | ||
|
||
function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$this->reopeningEntityManagerDecorator->open(); | ||
|
||
try { | ||
return $handler->handle($request); | ||
} finally { | ||
$this->reopeningEntityManagerDecorator->getConnection()->close(); | ||
$this->reopeningEntityManagerDecorator->clear(); | ||
} | ||
} | ||
} |