Config.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Amqp;
  7. use Magento\Framework\Amqp\Connection\FactoryOptions;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\ObjectManager;
  10. use PhpAmqpLib\Connection\AbstractConnection;
  11. use PhpAmqpLib\Channel\AMQPChannel;
  12. use Magento\Framework\Amqp\Connection\Factory as ConnectionFactory;
  13. /**
  14. * Reads the Amqp config in the deployed environment configuration
  15. *
  16. * @api
  17. * @since 102.0.1
  18. */
  19. class Config
  20. {
  21. /**
  22. * Queue config key
  23. */
  24. const QUEUE_CONFIG = 'queue';
  25. /**
  26. * Amqp config key
  27. */
  28. const AMQP_CONFIG = 'amqp';
  29. const HOST = 'host';
  30. const PORT = 'port';
  31. const USERNAME = 'user';
  32. const PASSWORD = 'password';
  33. const VIRTUALHOST = 'virtualhost';
  34. const SSL = 'ssl';
  35. const SSL_OPTIONS = 'ssl_options';
  36. /**
  37. * Deployment configuration
  38. *
  39. * @var DeploymentConfig
  40. */
  41. private $deploymentConfig;
  42. /**
  43. * @var AbstractConnection
  44. */
  45. private $connection;
  46. /**
  47. * @var AMQPChannel
  48. */
  49. private $channel;
  50. /**
  51. * Associative array of Amqp configuration
  52. *
  53. * @var array
  54. */
  55. private $data;
  56. /**
  57. * AMQP connection name.
  58. *
  59. * @var string
  60. */
  61. private $connectionName;
  62. /**
  63. * @var ConnectionFactory
  64. */
  65. private $connectionFactory;
  66. /**
  67. * Initialize dependencies.
  68. *
  69. * Example environment config:
  70. * <code>
  71. * 'queue' =>
  72. * [
  73. * 'amqp' => [
  74. * 'host' => 'localhost',
  75. * 'port' => 5672,
  76. * 'username' => 'guest',
  77. * 'password' => 'guest',
  78. * 'virtual_host' => '/',
  79. * 'ssl' => false,
  80. * 'ssl_options' => [],
  81. * ],
  82. * ],
  83. * </code>
  84. *
  85. * @param DeploymentConfig $config
  86. * @param string $connectionName
  87. * @param ConnectionFactory|null $connectionFactory
  88. */
  89. public function __construct(
  90. DeploymentConfig $config,
  91. $connectionName = 'amqp',
  92. ConnectionFactory $connectionFactory = null
  93. ) {
  94. $this->deploymentConfig = $config;
  95. $this->connectionName = $connectionName;
  96. $this->connectionFactory = $connectionFactory
  97. ?: ObjectManager::getInstance()->get(ConnectionFactory::class);
  98. }
  99. /**
  100. * Destructor
  101. *
  102. * @return void
  103. * @since 102.0.1
  104. */
  105. public function __destruct()
  106. {
  107. $this->closeConnection();
  108. }
  109. /**
  110. * Returns the configuration set for the key.
  111. *
  112. * @param string $key
  113. * @return string
  114. * @throws \LogicException
  115. * @since 102.0.1
  116. */
  117. public function getValue($key)
  118. {
  119. $this->load();
  120. return $this->data[$key] ?? null;
  121. }
  122. /**
  123. * Create amqp connection
  124. *
  125. * @return AbstractConnection
  126. */
  127. private function createConnection(): AbstractConnection
  128. {
  129. $sslEnabled = trim($this->getValue(self::SSL)) === 'true';
  130. $options = new FactoryOptions();
  131. $options->setHost($this->getValue(self::HOST));
  132. $options->setPort($this->getValue(self::PORT));
  133. $options->setUsername($this->getValue(self::USERNAME));
  134. $options->setPassword($this->getValue(self::PASSWORD));
  135. $options->setVirtualHost($this->getValue(self::VIRTUALHOST));
  136. $options->setSslEnabled($sslEnabled);
  137. /** @var array $sslOptions */
  138. if ($sslOptions = $this->getValue(self::SSL_OPTIONS)) {
  139. $options->setSslOptions($sslOptions);
  140. }
  141. return $this->connectionFactory->create($options);
  142. }
  143. /**
  144. * Return Amqp channel
  145. *
  146. * @return AMQPChannel
  147. * @throws \LogicException
  148. * @since 102.0.1
  149. */
  150. public function getChannel()
  151. {
  152. if (!isset($this->connection) || !isset($this->channel)) {
  153. $this->connection = $this->createConnection();
  154. $this->channel = $this->connection->channel();
  155. }
  156. return $this->channel;
  157. }
  158. /**
  159. * Load the configuration for Amqp
  160. *
  161. * @return void
  162. * @throws \LogicException
  163. */
  164. private function load()
  165. {
  166. if (null === $this->data) {
  167. $queueConfig = $this->deploymentConfig->getConfigData(self::QUEUE_CONFIG);
  168. if ($this->connectionName == self::AMQP_CONFIG) {
  169. $this->data = isset($queueConfig[self::AMQP_CONFIG]) ? $queueConfig[self::AMQP_CONFIG] : [];
  170. } else {
  171. $this->data = isset($queueConfig['connections'][$this->connectionName])
  172. ? $queueConfig['connections'][$this->connectionName]
  173. : [];
  174. }
  175. if (empty($this->data)) {
  176. throw new \LogicException('Unknown connection name ' . $this->connectionName);
  177. }
  178. }
  179. }
  180. /**
  181. * Close Amqp connection and Channel
  182. *
  183. * @return void
  184. */
  185. private function closeConnection()
  186. {
  187. if (isset($this->channel)) {
  188. $this->channel->close();
  189. unset($this->channel);
  190. }
  191. if (isset($this->connection)) {
  192. $this->connection->close();
  193. unset($this->connection);
  194. }
  195. }
  196. }