|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\AmqpExtInterop; |
| 4 | + |
| 5 | +use Interop\Amqp\AmqpConnectionFactory as InteropAmqpConnectionFactory; |
| 6 | + |
| 7 | +class AmqpConnectionFactory implements InteropAmqpConnectionFactory |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @var array |
| 11 | + */ |
| 12 | + private $config; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var \AMQPConnection |
| 16 | + */ |
| 17 | + private $connection; |
| 18 | + |
| 19 | + /** |
| 20 | + * The config could be an array, string DSN or null. In case of null it will attempt to connect to localhost with default credentials. |
| 21 | + * |
| 22 | + * [ |
| 23 | + * 'host' => 'amqp.host The host to connect too. Note: Max 1024 characters.', |
| 24 | + * 'port' => 'amqp.port Port on the host.', |
| 25 | + * 'vhost' => 'amqp.vhost The virtual host on the host. Note: Max 128 characters.', |
| 26 | + * 'user' => 'amqp.user The user name to use. Note: Max 128 characters.', |
| 27 | + * 'pass' => 'amqp.password Password. Note: Max 128 characters.', |
| 28 | + * 'read_timeout' => 'Timeout in for income activity. Note: 0 or greater seconds. May be fractional.', |
| 29 | + * 'write_timeout' => 'Timeout in for outcome activity. Note: 0 or greater seconds. May be fractional.', |
| 30 | + * 'connect_timeout' => 'Connection timeout. Note: 0 or greater seconds. May be fractional.', |
| 31 | + * 'persisted' => 'bool, Whether it use single persisted connection or open a new one for every context', |
| 32 | + * 'lazy' => 'the connection will be performed as later as possible, if the option set to true', |
| 33 | + * 'pre_fetch_count' => 'Controls how many messages could be prefetched', |
| 34 | + * 'pre_fetch_size' => 'Controls how many messages could be prefetched', |
| 35 | + * 'receive_method' => 'Could be either basic_get or basic_consume', |
| 36 | + * ] |
| 37 | + * |
| 38 | + * or |
| 39 | + * |
| 40 | + * amqp://user:pass@host:10000/vhost?lazy=true&persisted=false&read_timeout=2 |
| 41 | + * |
| 42 | + * @param array|string $config |
| 43 | + */ |
| 44 | + public function __construct($config = 'amqp://') |
| 45 | + { |
| 46 | + if (empty($config) || 'amqp://' === $config) { |
| 47 | + $config = []; |
| 48 | + } elseif (is_string($config)) { |
| 49 | + $config = $this->parseDsn($config); |
| 50 | + } elseif (is_array($config)) { |
| 51 | + } else { |
| 52 | + throw new \LogicException('The config must be either an array of options, a DSN string or null'); |
| 53 | + } |
| 54 | + |
| 55 | + $this->config = array_replace($this->defaultConfig(), $config); |
| 56 | + |
| 57 | + $supportedMethods = ['basic_get', 'basic_consume']; |
| 58 | + if (false == in_array($this->config['receive_method'], $supportedMethods, true)) { |
| 59 | + throw new \LogicException(sprintf( |
| 60 | + 'Invalid "receive_method" option value "%s". It could be only "%s"', |
| 61 | + $this->config['receive_method'], |
| 62 | + implode('", "', $supportedMethods) |
| 63 | + )); |
| 64 | + } |
| 65 | + |
| 66 | + if ('basic_consume' == $this->config['receive_method']) { |
| 67 | + if (false == (version_compare(phpversion('amqp'), '1.9.1', '>=') || phpversion('amqp') == '1.9.1-dev')) { |
| 68 | + // @see https://github.com/php-enqueue/enqueue-dev/issues/110 and https://github.com/pdezwart/php-amqp/issues/281 |
| 69 | + throw new \LogicException('The "basic_consume" method does not work on amqp extension prior 1.9.1 version.'); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * {@inheritdoc} |
| 76 | + * |
| 77 | + * @return AmqpContext |
| 78 | + */ |
| 79 | + public function createContext() |
| 80 | + { |
| 81 | + if ($this->config['lazy']) { |
| 82 | + return new AmqpContext(function () { |
| 83 | + return $this->createExtContext($this->establishConnection()); |
| 84 | + }, $this->config['receive_method']); |
| 85 | + } |
| 86 | + |
| 87 | + return new AmqpContext($this->createExtContext($this->establishConnection()), $this->config['receive_method']); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param \AMQPConnection $extConnection |
| 92 | + * |
| 93 | + * @return \AMQPChannel |
| 94 | + */ |
| 95 | + private function createExtContext(\AMQPConnection $extConnection) |
| 96 | + { |
| 97 | + $channel = new \AMQPChannel($extConnection); |
| 98 | + if (false == empty($this->config['pre_fetch_count'])) { |
| 99 | + $channel->setPrefetchCount((int) $this->config['pre_fetch_count']); |
| 100 | + } |
| 101 | + |
| 102 | + if (false == empty($this->config['pre_fetch_size'])) { |
| 103 | + $channel->setPrefetchSize((int) $this->config['pre_fetch_size']); |
| 104 | + } |
| 105 | + |
| 106 | + return $channel; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return \AMQPConnection |
| 111 | + */ |
| 112 | + private function establishConnection() |
| 113 | + { |
| 114 | + if (false == $this->connection) { |
| 115 | + $config = $this->config; |
| 116 | + $config['login'] = $this->config['user']; |
| 117 | + $config['password'] = $this->config['pass']; |
| 118 | + |
| 119 | + $this->connection = new \AMQPConnection($config); |
| 120 | + |
| 121 | + $this->config['persisted'] ? $this->connection->pconnect() : $this->connection->connect(); |
| 122 | + } |
| 123 | + if (false == $this->connection->isConnected()) { |
| 124 | + $this->config['persisted'] ? $this->connection->preconnect() : $this->connection->reconnect(); |
| 125 | + } |
| 126 | + |
| 127 | + return $this->connection; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param string $dsn |
| 132 | + * |
| 133 | + * @return array |
| 134 | + */ |
| 135 | + private function parseDsn($dsn) |
| 136 | + { |
| 137 | + $dsnConfig = parse_url($dsn); |
| 138 | + if (false === $dsnConfig) { |
| 139 | + throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn)); |
| 140 | + } |
| 141 | + |
| 142 | + $dsnConfig = array_replace([ |
| 143 | + 'scheme' => null, |
| 144 | + 'host' => null, |
| 145 | + 'port' => null, |
| 146 | + 'user' => null, |
| 147 | + 'pass' => null, |
| 148 | + 'path' => null, |
| 149 | + 'query' => null, |
| 150 | + ], $dsnConfig); |
| 151 | + |
| 152 | + if ('amqp' !== $dsnConfig['scheme']) { |
| 153 | + throw new \LogicException(sprintf('The given DSN scheme "%s" is not supported. Could be "amqp" only.', $dsnConfig['scheme'])); |
| 154 | + } |
| 155 | + |
| 156 | + if ($dsnConfig['query']) { |
| 157 | + $query = []; |
| 158 | + parse_str($dsnConfig['query'], $query); |
| 159 | + |
| 160 | + $dsnConfig = array_replace($query, $dsnConfig); |
| 161 | + } |
| 162 | + |
| 163 | + $dsnConfig['vhost'] = ltrim($dsnConfig['path'], '/'); |
| 164 | + |
| 165 | + unset($dsnConfig['scheme'], $dsnConfig['query'], $dsnConfig['fragment'], $dsnConfig['path']); |
| 166 | + |
| 167 | + $config = array_replace($this->defaultConfig(), $dsnConfig); |
| 168 | + $config = array_map(function ($value) { |
| 169 | + return urldecode($value); |
| 170 | + }, $config); |
| 171 | + |
| 172 | + return $config; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @return array |
| 177 | + */ |
| 178 | + private function defaultConfig() |
| 179 | + { |
| 180 | + return [ |
| 181 | + 'host' => 'localhost', |
| 182 | + 'port' => 5672, |
| 183 | + 'vhost' => '/', |
| 184 | + 'user' => 'guest', |
| 185 | + 'pass' => 'guest', |
| 186 | + 'read_timeout' => null, |
| 187 | + 'write_timeout' => null, |
| 188 | + 'connect_timeout' => null, |
| 189 | + 'persisted' => false, |
| 190 | + 'lazy' => true, |
| 191 | + 'pre_fetch_count' => null, |
| 192 | + 'pre_fetch_size' => null, |
| 193 | + 'receive_method' => 'basic_get', |
| 194 | + ]; |
| 195 | + } |
| 196 | +} |
0 commit comments