EntityManagerHandler.php 980 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Swoole;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use K911\Swoole\Server\RequestHandler\RequestHandlerInterface;
  5. use Swoole\Http\Request;
  6. use Swoole\Http\Response;
  7. final class EntityManagerHandler implements RequestHandlerInterface
  8. {
  9. private $decorated;
  10. private $entityManager;
  11. public function __construct(RequestHandlerInterface $decorated, EntityManagerInterface $entityManager)
  12. {
  13. $this->decorated = $decorated;
  14. $this->entityManager = $entityManager;
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function handle(Request $request, Response $response): void
  20. {
  21. try {
  22. $this->decorated->handle($request, $response);
  23. } finally {
  24. // Swoole handle several request in a raw. We clear the entityManager between 2 call, to avoid Doctrine
  25. // to re-use the same objects without fetching it from the database.
  26. $this->entityManager->clear();
  27. }
  28. }
  29. }