Context.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Pool;
  12. use Hyperf\Context\Context as CoroutineContext;
  13. use Hyperf\Contract\ConnectionInterface;
  14. use Hyperf\Contract\StdoutLoggerInterface;
  15. use Psr\Container\ContainerInterface;
  16. use Psr\Log\LoggerInterface;
  17. class Context
  18. {
  19. protected LoggerInterface $logger;
  20. public function __construct(protected ContainerInterface $container, protected string $name)
  21. {
  22. $this->logger = $container->get(StdoutLoggerInterface::class);
  23. }
  24. /**
  25. * Get a connection from request context.
  26. */
  27. public function connection(): ?ConnectionInterface
  28. {
  29. if (CoroutineContext::has($this->name)) {
  30. return CoroutineContext::get($this->name);
  31. }
  32. return null;
  33. }
  34. public function set(ConnectionInterface $connection): void
  35. {
  36. CoroutineContext::set($this->name, $connection);
  37. }
  38. }