Happyr /
elastica-dsn
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Happyr\ElasticaDsn; |
||
| 6 | |||
| 7 | use Happyr\ElasticaDsn\Exception\InvalidArgumentException; |
||
| 8 | use Elastica\Client; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Creates a Elastic search client. |
||
| 12 | * |
||
| 13 | * @author Tobias Nyholm <[email protected]> |
||
| 14 | * @author Rob Frawley 2nd <[email protected]> |
||
| 15 | * @author Nicolas Grekas <[email protected]> |
||
| 16 | */ |
||
| 17 | final class ClientFactory |
||
| 18 | { |
||
| 19 | public static function create($servers, array $options = []): Client |
||
| 20 | { |
||
| 21 | return new Client(self::getConfig($servers, $options)); |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string|string[] $servers An array of servers, a DSN, or an array of DSNs |
||
| 26 | * @param array $options Valid keys are "username" and "password" |
||
| 27 | */ |
||
| 28 | public static function getConfig($servers, array $options = []): array |
||
| 29 | { |
||
| 30 | if (\is_string($servers)) { |
||
| 31 | $servers = [$servers]; |
||
| 32 | } elseif (!\is_array($servers)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 33 | throw new InvalidArgumentException(\sprintf('ClientFactory::create() expects array or string as first argument, %s given.', \gettype($servers))); |
||
| 34 | } |
||
| 35 | |||
| 36 | \set_error_handler(function ($type, $msg, $file, $line) { |
||
| 37 | throw new \ErrorException($msg, 0, $type, $file, $line); |
||
| 38 | }); |
||
| 39 | try { |
||
| 40 | return self::doGetConfig($servers, $options); |
||
| 41 | } finally { |
||
| 42 | \restore_error_handler(); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | 8 | private static function doGetConfig(array $input, array $options): array |
|
| 47 | { |
||
| 48 | 8 | $servers = []; |
|
| 49 | 8 | $username = $options['username'] ?? null; |
|
| 50 | 8 | $password = $options['password'] ?? null; |
|
| 51 | |||
| 52 | // parse any DSN in $servers |
||
| 53 | 8 | foreach ($input as $i => $dsn) { |
|
| 54 | 8 | if (\is_array($dsn)) { |
|
| 55 | continue; |
||
| 56 | } |
||
| 57 | 8 | if (0 !== \mb_strpos($dsn, 'elasticsearch:')) { |
|
| 58 | throw new InvalidArgumentException( |
||
| 59 | \sprintf('Invalid Elasticsearch DSN: %s does not start with "elasticsearch:"', $dsn) |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | 8 | $params = \preg_replace_callback( |
|
| 63 | 8 | '#^elasticsearch:(//)?(?:([^@]*+)@)?#', |
|
| 64 | function ($m) use (&$username, &$password) { |
||
| 65 | 8 | if (!empty($m[2])) { |
|
| 66 | 2 | list($username, $password) = \explode(':', $m[2], 2) + [1 => null]; |
|
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 67 | } |
||
| 68 | |||
| 69 | 8 | return 'file:'.($m[1] ?? ''); |
|
| 70 | 8 | }, |
|
| 71 | 8 | $dsn |
|
| 72 | ); |
||
| 73 | |||
| 74 | 8 | if (false === $params = \parse_url($params)) { |
|
| 75 | throw new InvalidArgumentException(\sprintf('Invalid Elasticsearch DSN: %s', $dsn)); |
||
| 76 | } |
||
| 77 | |||
| 78 | 8 | $query = $hosts = []; |
|
| 79 | 8 | if (isset($params['query'])) { |
|
| 80 | 3 | \parse_str($params['query'], $query); |
|
| 81 | |||
| 82 | 3 | if (isset($query['host'])) { |
|
| 83 | 3 | if (!\is_array($hosts = $query['host'])) { |
|
| 84 | throw new InvalidArgumentException(\sprintf('Invalid Elasticsearch DSN: %s', $dsn)); |
||
| 85 | } |
||
| 86 | 3 | foreach ($hosts as $host => $value) { |
|
| 87 | 3 | if (false === $port = \mb_strrpos($host, ':')) { |
|
| 88 | 2 | $hosts[$host] = ['host' => $host, 'port' => 9200]; |
|
| 89 | } else { |
||
| 90 | 3 | $hosts[$host] = ['host' => \mb_substr($host, 0, $port), 'port' => (int) \mb_substr($host, 1 + $port)]; |
|
| 91 | } |
||
| 92 | } |
||
| 93 | 3 | $hosts = \array_values($hosts); |
|
| 94 | 3 | unset($query['host']); |
|
| 95 | } |
||
| 96 | 3 | if ($hosts && !isset($params['host']) && !isset($params['path'])) { |
|
| 97 | 3 | $servers = \array_merge($servers, $hosts); |
|
| 98 | 3 | continue; |
|
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | 6 | if (!isset($params['host']) && !isset($params['path'])) { |
|
| 103 | throw new InvalidArgumentException(\sprintf('Invalid Elasticsearch DSN: %s', $dsn)); |
||
| 104 | } |
||
| 105 | |||
| 106 | 6 | if (isset($params['path']) && \preg_match('#/(\d+)$#', $params['path'], $m)) { |
|
| 107 | $params['path'] = \mb_substr($params['path'], 0, -\mb_strlen($m[0])); |
||
| 108 | } |
||
| 109 | |||
| 110 | 6 | if (isset($params['path']) && \preg_match('#:(\d+)$#', $params['path'], $m)) { |
|
| 111 | 1 | $params['host'] = \mb_substr($params['path'], 0, -\mb_strlen($m[0])); |
|
| 112 | 1 | $params['port'] = $m[1]; |
|
| 113 | 1 | unset($params['path']); |
|
| 114 | } |
||
| 115 | |||
| 116 | $params += [ |
||
| 117 | 6 | 'host' => $params['host'] ?? $params['path'], |
|
| 118 | 6 | 'port' => !isset($params['port']) ? 9200 : null, |
|
| 119 | ]; |
||
| 120 | 6 | if ($query) { |
|
| 121 | $params += $query; |
||
| 122 | $options = $query + $options; |
||
| 123 | } |
||
| 124 | |||
| 125 | 6 | $servers[] = ['host' => $params['host'], 'port' => $params['port']]; |
|
| 126 | |||
| 127 | 6 | if ($hosts) { |
|
| 128 | $servers = \array_merge($servers, $hosts); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | 8 | $config = ['servers' => $servers]; |
|
| 133 | 8 | if (null !== $username) { |
|
| 134 | 2 | $config['username'] = $username; |
|
| 135 | } |
||
| 136 | 8 | if (null !== $password) { |
|
| 137 | 2 | $config['password'] = $password; |
|
| 138 | } |
||
| 139 | |||
| 140 | 8 | return $config; |
|
| 141 | } |
||
| 142 | } |
||
| 143 |