ConnectionPoolTrait.php 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Smf\ConnectionPool;
  3. trait ConnectionPoolTrait
  4. {
  5. /**
  6. * @var ConnectionPool[] $pools
  7. */
  8. protected $pools = [];
  9. /**
  10. * Add a connection pool
  11. * @param string $key
  12. * @param ConnectionPool $pool
  13. */
  14. public function addConnectionPool(string $key, ConnectionPool $pool)
  15. {
  16. $this->pools[$key] = $pool;
  17. }
  18. /**
  19. * Get a connection pool by key
  20. * @param string $key
  21. * @return ConnectionPool
  22. */
  23. public function getConnectionPool(string $key): ConnectionPool
  24. {
  25. return $this->pools[$key];
  26. }
  27. /**
  28. * Close the connection by key
  29. * @param string $key
  30. * @return bool
  31. */
  32. public function closeConnectionPool(string $key)
  33. {
  34. return $this->pools[$key]->close();
  35. }
  36. /**
  37. * Close all connection pools
  38. */
  39. public function closeConnectionPools()
  40. {
  41. foreach ($this->pools as $pool) {
  42. $pool->close();
  43. }
  44. }
  45. }