Debian Codegraph @

vendor/clue/connection-manager-extra/src/Multiple/ConnectionManagerSelective.php

raw
1 <?php
2
3 namespace ConnectionManager\Extra\Multiple;
4
5 use React\Socket\ConnectorInterface;
6 use React\Promise;
7 use UnderflowException;
8 use InvalidArgumentException;
9
10 class ConnectionManagerSelective implements ConnectorInterface
11 {
12 private $managers;
13
14 /**
15 *
16 * @param ConnectorInterface[] $managers
17 */
18 public function __construct(array $managers)
19 {
20 foreach ($managers as $filter => $manager) {
21 $host = $filter;
22 $portMin = 0;
23 $portMax = 65535;
24
25 // search colon (either single one OR preceded by "]" due to IPv6)
26 $colon = strrpos($host, ':');
27 if ($colon !== false && (strpos($host, ':') === $colon || substr($host, $colon - 1, 1) === ']' )) {
28 if (!isset($host[$colon + 1])) {
29 throw new InvalidArgumentException('Entry "' . $filter . '" has no port after colon');
30 }
31
32 $minus = strpos($host, '-', $colon);
33 if ($minus === false) {
34 $portMin = $portMax = (int)substr($host, $colon + 1);
35
36 if (substr($host, $colon + 1) !== (string)$portMin) {
37 throw new InvalidArgumentException('Entry "' . $filter . '" has no valid port after colon');
38 }
39 } else {
40 $portMin = (int)substr($host, $colon + 1, ($minus - $colon));
41 $portMax = (int)substr($host, $minus + 1);
42
43 if (substr($host, $colon + 1) !== ($portMin . '-' . $portMax)) {
44 throw new InvalidArgumentException('Entry "' . $filter . '" has no valid port range after colon');
45 }
46
47 if ($portMin > $portMax) {
48 throw new InvalidArgumentException('Entry "' . $filter . '" has port range mixed up');
49 }
50 }
51 $host = substr($host, 0, $colon);
52 }
53
54 if ($host === '') {
55 throw new InvalidArgumentException('Entry "' . $filter . '" has an empty host');
56 }
57
58 if (!$manager instanceof ConnectorInterface) {
59 throw new InvalidArgumentException('Entry "' . $filter . '" is not a valid connector');
60 }
61 }
62
63 $this->managers = $managers;
64 }
65
66 public function connect($uri)
67 {
68 $parts = parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri);
69 if (!isset($parts) || !isset($parts['scheme'], $parts['host'], $parts['port'])) {
70 return Promise\reject(new InvalidArgumentException('Invalid URI'));
71 }
72
73 $connector = $this->getConnectorForTarget(
74 trim($parts['host'], '[]'),
75 $parts['port']
76 );
77
78 if ($connector === null) {
79 return Promise\reject(new UnderflowException('No connector for given target found'));
80 }
81
82 return $connector->connect($uri);
83 }
84
85 private function getConnectorForTarget($targetHost, $targetPort)
86 {
87 foreach ($this->managers as $host => $connector) {
88 $portMin = 0;
89 $portMax = 65535;
90
91 // search colon (either single one OR preceded by "]" due to IPv6)
92 $colon = strrpos($host, ':');
93 if ($colon !== false && (strpos($host, ':') === $colon || substr($host, $colon - 1, 1) === ']' )) {
94 $minus = strpos($host, '-', $colon);
95 if ($minus === false) {
96 $portMin = $portMax = (int)substr($host, $colon + 1);
97 } else {
98 $portMin = (int)substr($host, $colon + 1, ($minus - $colon));
99 $portMax = (int)substr($host, $minus + 1);
100 }
101 $host = trim(substr($host, 0, $colon), '[]');
102 }
103
104 if ($targetPort >= $portMin && $targetPort <= $portMax && fnmatch($host, $targetHost)) {
105 return $connector;
106 }
107 }
108
109 return null;
110 }
111 }