|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Pheanstalk; |
| 4 | + |
| 5 | +use Enqueue\Psr\PsrConnectionFactory; |
| 6 | +use Pheanstalk\Pheanstalk; |
| 7 | + |
| 8 | +class PheanstalkConnectionFactory implements PsrConnectionFactory |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var array |
| 12 | + */ |
| 13 | + private $config; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var Pheanstalk |
| 17 | + */ |
| 18 | + private $connection; |
| 19 | + |
| 20 | + /** |
| 21 | + * The config could be an array, string DSN or null. In case of null it will attempt to connect to localhost with default settings. |
| 22 | + * |
| 23 | + * [ |
| 24 | + * 'host' => 'localhost', |
| 25 | + * 'port' => 11300, |
| 26 | + * 'timeout' => null, |
| 27 | + * 'persisted' => true, |
| 28 | + * ] |
| 29 | + * |
| 30 | + * or |
| 31 | + * |
| 32 | + * beanstalk://host:port |
| 33 | + * |
| 34 | + * @param array|string $config |
| 35 | + */ |
| 36 | + public function __construct($config = 'beanstalk://') |
| 37 | + { |
| 38 | + if (empty($config) || 'beanstalk://' === $config) { |
| 39 | + $config = []; |
| 40 | + } elseif (is_string($config)) { |
| 41 | + $config = $this->parseDsn($config); |
| 42 | + } elseif (is_array($config)) { |
| 43 | + } else { |
| 44 | + throw new \LogicException('The config must be either an array of options, a DSN string or null'); |
| 45 | + } |
| 46 | + |
| 47 | + $this->config = array_replace($this->defaultConfig(), $config); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + * |
| 53 | + * @return PheanstalkContext |
| 54 | + */ |
| 55 | + public function createContext() |
| 56 | + { |
| 57 | + return new PheanstalkContext($this->establishConnection()); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return Pheanstalk |
| 62 | + */ |
| 63 | + private function establishConnection() |
| 64 | + { |
| 65 | + if (false == $this->connection) { |
| 66 | + $this->connection = new Pheanstalk( |
| 67 | + $this->config['host'], |
| 68 | + $this->config['port'], |
| 69 | + $this->config['timeout'], |
| 70 | + $this->config['persisted'] |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + return $this->connection; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param string $dsn |
| 79 | + * |
| 80 | + * @return array |
| 81 | + */ |
| 82 | + private function parseDsn($dsn) |
| 83 | + { |
| 84 | + $dsnConfig = parse_url($dsn); |
| 85 | + if (false === $dsnConfig) { |
| 86 | + throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn)); |
| 87 | + } |
| 88 | + |
| 89 | + $dsnConfig = array_replace([ |
| 90 | + 'scheme' => null, |
| 91 | + 'host' => null, |
| 92 | + 'port' => null, |
| 93 | + 'user' => null, |
| 94 | + 'pass' => null, |
| 95 | + 'path' => null, |
| 96 | + 'query' => null, |
| 97 | + ], $dsnConfig); |
| 98 | + |
| 99 | + if ('beanstalk' !== $dsnConfig['scheme']) { |
| 100 | + throw new \LogicException(sprintf('The given DSN scheme "%s" is not supported. Could be "beanstalk" only.', $dsnConfig['scheme'])); |
| 101 | + } |
| 102 | + |
| 103 | + $query = []; |
| 104 | + if ($dsnConfig['query']) { |
| 105 | + parse_str($dsnConfig['query'], $query); |
| 106 | + } |
| 107 | + |
| 108 | + return array_replace($query, [ |
| 109 | + 'port' => $dsnConfig['port'], |
| 110 | + 'host' => $dsnConfig['host'], |
| 111 | + ]); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return array |
| 116 | + */ |
| 117 | + private function defaultConfig() |
| 118 | + { |
| 119 | + return [ |
| 120 | + 'host' => 'localhost', |
| 121 | + 'port' => Pheanstalk::DEFAULT_PORT, |
| 122 | + 'timeout' => null, |
| 123 | + 'persisted' => true, |
| 124 | + ]; |
| 125 | + } |
| 126 | +} |
0 commit comments