|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bunny\Test\Library; |
| 6 | + |
| 7 | +function parseAmqpUri($uri): array |
| 8 | +{ |
| 9 | + $uriComponents = parse_url($uri); |
| 10 | + |
| 11 | + if ( |
| 12 | + !isset($uriComponents['scheme']) |
| 13 | + || !in_array($uriComponents['scheme'], ['amqp', 'amqps']) |
| 14 | + ) { |
| 15 | + throw new \RuntimeException( |
| 16 | + sprintf( |
| 17 | + 'URI scheme must be "amqp" or "amqps". URI given: "%s"', |
| 18 | + $uri |
| 19 | + ) |
| 20 | + ); |
| 21 | + } |
| 22 | + |
| 23 | + $options = []; |
| 24 | + |
| 25 | + if (isset($uriComponents['host'])) { |
| 26 | + $options['host'] = $uriComponents['host']; |
| 27 | + } |
| 28 | + |
| 29 | + if (isset($uriComponents['port'])) { |
| 30 | + $options['port'] = $uriComponents['port']; |
| 31 | + } |
| 32 | + |
| 33 | + if (isset($uriComponents['user'])) { |
| 34 | + $options['user'] = $uriComponents['user']; |
| 35 | + } |
| 36 | + |
| 37 | + if (isset($uriComponents['pass'])) { |
| 38 | + $options['password'] = $uriComponents['pass']; |
| 39 | + } |
| 40 | + |
| 41 | + if (isset($uriComponents['path'])) { |
| 42 | + $vhostCandidate = $uriComponents['path']; |
| 43 | + |
| 44 | + if (strpos($vhostCandidate, '/') === 0) { |
| 45 | + $vhostCandidate = substr($vhostCandidate, 1); |
| 46 | + } |
| 47 | + |
| 48 | + if (strpos($vhostCandidate, '/') !== false) { |
| 49 | + throw new \RuntimeException( |
| 50 | + sprintf( |
| 51 | + 'An URI path component that is a valid vhost may not contain unescaped "/" characters. URI given: "%s"', |
| 52 | + $uri |
| 53 | + ) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + $vhostCandidate = rawurldecode($vhostCandidate); |
| 58 | + |
| 59 | + $options['vhost'] = $vhostCandidate; |
| 60 | + } |
| 61 | + |
| 62 | + if ($options['vhost'] === '') { |
| 63 | + $options['vhost'] = '/'; |
| 64 | + } |
| 65 | + |
| 66 | + return $options; |
| 67 | +} |
0 commit comments