AbstractConnectionFactory.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php declare(strict_types=1);
  2. namespace mikemadisonweb\rabbitmq\components;
  3. use PhpAmqpLib\Connection\AbstractConnection;
  4. class AbstractConnectionFactory
  5. {
  6. /** @var \ReflectionClass */
  7. private $_class;
  8. /** @var array */
  9. private $_parameters;
  10. /**
  11. * Constructor
  12. *
  13. * @param string $class FQCN of AMQPConnection class to instantiate.
  14. * @param array $parameters Map containing parameters resolved by Extension.
  15. */
  16. public function __construct($class, array $parameters)
  17. {
  18. $this->_class = $class;
  19. $this->_parameters = $this->parseUrl($parameters);
  20. }
  21. /**
  22. * @return mixed
  23. */
  24. public function createConnection() : AbstractConnection
  25. {
  26. if ($this->_parameters['ssl_context'] !== null) {
  27. return new $this->_class(
  28. $this->_parameters['host'],
  29. $this->_parameters['port'],
  30. $this->_parameters['user'],
  31. $this->_parameters['password'],
  32. $this->_parameters['vhost'],
  33. $this->_parameters['ssl_context'],
  34. [
  35. 'connection_timeout' => $this->_parameters['connection_timeout'],
  36. 'read_write_timeout' => $this->_parameters['read_write_timeout'],
  37. 'keepalive' => $this->_parameters['keepalive'],
  38. 'heartbeat' => $this->_parameters['heartbeat'],
  39. 'channel_rpc_timeout' => $this->_parameters['channel_rpc_timeout'],
  40. ]
  41. );
  42. }
  43. return new $this->_class(
  44. $this->_parameters['host'],
  45. $this->_parameters['port'],
  46. $this->_parameters['user'],
  47. $this->_parameters['password'],
  48. $this->_parameters['vhost'],
  49. false, // insist
  50. 'AMQPLAIN', // login_method
  51. null, // login_response
  52. 'en_EN', // locale
  53. $this->_parameters['connection_timeout'],
  54. $this->_parameters['read_write_timeout'],
  55. $this->_parameters['ssl_context'],
  56. $this->_parameters['keepalive'],
  57. $this->_parameters['heartbeat'],
  58. $this->_parameters['channel_rpc_timeout']
  59. );
  60. }
  61. /**
  62. * Parse connection defined by url, e.g. 'amqp://guest:password@localhost:5672/vhost?lazy=1&connection_timeout=6'
  63. * @param $parameters
  64. * @return array
  65. */
  66. private function parseUrl($parameters)
  67. {
  68. if (!$parameters['url']) {
  69. return $parameters;
  70. }
  71. $url = parse_url($parameters['url']);
  72. if ($url === false || !isset($url['scheme']) || $url['scheme'] !== 'amqp') {
  73. throw new \InvalidArgumentException('Malformed parameter "url".');
  74. }
  75. if (isset($url['host'])) {
  76. $parameters['host'] = urldecode($url['host']);
  77. }
  78. if (isset($url['port'])) {
  79. $parameters['port'] = (int)$url['port'];
  80. }
  81. if (isset($url['user'])) {
  82. $parameters['user'] = urldecode($url['user']);
  83. }
  84. if (isset($url['pass'])) {
  85. $parameters['password'] = urldecode($url['pass']);
  86. }
  87. if (isset($url['path'])) {
  88. $parameters['vhost'] = urldecode(ltrim($url['path'], '/'));
  89. }
  90. if (isset($url['query'])) {
  91. $query = [];
  92. parse_str($url['query'], $query);
  93. $parameters = array_merge($parameters, $query);
  94. }
  95. unset($parameters['url']);
  96. return $parameters;
  97. }
  98. }