friendship ended with social-app. php is my new best friend
1<?php
2
3namespace React\Socket;
4
5use React\EventLoop\Loop;
6use React\EventLoop\LoopInterface;
7use React\Promise;
8use InvalidArgumentException;
9use RuntimeException;
10
11/**
12 * Unix domain socket connector
13 *
14 * Unix domain sockets use atomic operations, so we can as well emulate
15 * async behavior.
16 */
17final class UnixConnector implements ConnectorInterface
18{
19 private $loop;
20
21 /**
22 * @param ?LoopInterface $loop
23 */
24 public function __construct($loop = null)
25 {
26 if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
27 throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
28 }
29
30 $this->loop = $loop ?: Loop::get();
31 }
32
33 public function connect($path)
34 {
35 if (\strpos($path, '://') === false) {
36 $path = 'unix://' . $path;
37 } elseif (\substr($path, 0, 7) !== 'unix://') {
38 return Promise\reject(new \InvalidArgumentException(
39 'Given URI "' . $path . '" is invalid (EINVAL)',
40 \defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : (\defined('PCNTL_EINVAL') ? \PCNTL_EINVAL : 22)
41 ));
42 }
43
44 $resource = @\stream_socket_client($path, $errno, $errstr, 1.0);
45
46 if (!$resource) {
47 return Promise\reject(new \RuntimeException(
48 'Unable to connect to unix domain socket "' . $path . '": ' . $errstr . SocketServer::errconst($errno),
49 $errno
50 ));
51 }
52
53 $connection = new Connection($resource, $this->loop);
54 $connection->unix = true;
55
56 return Promise\resolve($connection);
57 }
58}