forked from clue/reactphp-ami
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory.php
More file actions
159 lines (147 loc) · 5.49 KB
/
Factory.php
File metadata and controls
159 lines (147 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Clue\React\Ami;
use React\EventLoop\LoopInterface;
use React\Socket\ConnectionInterface;
use React\Socket\Connector;
use React\Socket\ConnectorInterface;
/**
* The `Factory` is responsible for creating your [`Client`](#client) instance.
*
* ```php
* $factory = new Clue\React\Ami\Factory();
* ```
*
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
* pass the event loop instance to use for this object. You can use a `null` value
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
* given event loop instance.
*
* If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
* proxy servers etc.), you can explicitly pass a custom instance of the
* [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
*
* ```php
* $connector = new React\Socket\Connector(array(
* 'dns' => '127.0.0.1',
* 'tcp' => array(
* 'bindto' => '192.168.10.1:0'
* ),
* 'tls' => array(
* 'verify_peer' => false,
* 'verify_peer_name' => false
* )
* ));
*
* $factory = new Clue\React\Ami\Factory(null, $connector);
* ```
*/
class Factory
{
private $connector;
/**
* @param ?LoopInterface $loop
* @param ?ConnectorInterface $connector
*/
public function __construct($loop = null, $connector = null)
{
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
}
if ($connector !== null && !$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
}
if ($connector === null) {
$connector = new Connector(array(), $loop);
}
$this->connector = $connector;
}
/**
* Create a new [`Client`](#client).
*
* It helps with establishing a plain TCP/IP or secure TLS connection to the AMI
* and optionally issuing an initial `login` action.
*
* ```php
* $factory->createClient($url)->then(
* function (Clue\React\Ami\Client $client) {
* // client connected (and authenticated)
* },
* function (Exception $e) {
* // an error occurred while trying to connect or authorize client
* }
* );
* ```
*
* The method returns a [Promise](https://github.com/reactphp/promise) that will
* resolve with the [`Client`](#client) instance on success or will reject with an
* `Exception` if the URL is invalid or the connection or authentication fails.
*
* The `$url` parameter contains the host and optional port (which defaults to
* `5038` for plain TCP/IP connections) to connect to:
*
* ```php
* $factory->createClient('localhost:5038');
* ```
*
* The above example does not pass any authentication details, so you may have to
* call `ActionSender::login()` after connecting or use the recommended shortcut
* to pass a username and secret for your AMI login details like this:
*
* ```php
* $factory->createClient('user:secret@localhost');
* ```
*
* Note that both the username and password must be URL-encoded (percent-encoded)
* if they contain special characters:
*
* ```php
* $user = 'he:llo';
* $pass = 'p@ss';
*
* $promise = $factory->createClient(
* rawurlencode($user) . ':' . rawurlencode($pass) . '@localhost'
* );
* ```
*
* The `Factory` defaults to establishing a plaintext TCP connection.
* If you want to create a secure TLS connection, you can use the `tls` scheme
* (which defaults to port `5039`):
*
* ```php
* $factory->createClient('tls://user:secret@localhost:5039');
* ```
*
* @param string $url
* @return \React\Promise\PromiseInterface<Client,\Exception>
*/
public function createClient($url)
{
$parts = parse_url((strpos($url, '://') === false ? 'tcp://' : '') . $url);
if (!$parts || !isset($parts['scheme'], $parts['host'])) {
return \React\Promise\reject(new \InvalidArgumentException('Given URL "' . $url . '" can not be parsed'));
}
// use default port 5039 for `tls://` or 5038 otherwise
if (!isset($parts['port'])) {
$parts['port'] = $parts['scheme'] === 'tls' ? 5039 : 5038;
}
$promise = $this->connector->connect($parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port'])->then(function (ConnectionInterface $stream) {
return new Client($stream);
});
if (isset($parts['user'])) {
$promise = $promise->then(function (Client $client) use ($parts) {
$sender = new ActionSender($client);
return $sender->login(rawurldecode($parts['user']), rawurldecode($parts['pass']))->then(
function () use ($client) {
return $client;
},
function ($error) use ($client) {
$client->close();
throw $error;
}
);
});
}
return $promise;
}
}