Skip to content
This repository was archived by the owner on Apr 27, 2021. It is now read-only.

Commit fbe9f72

Browse files
committed
Fixed few things about the SDK + Symfony Bundle
1 parent fe40641 commit fbe9f72

File tree

4 files changed

+49
-97
lines changed

4 files changed

+49
-97
lines changed

README.md

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
# redirection.io Proxy PHP SDK
22

3-
[![Latest Version](https://img.shields.io/github/release/redirectionio/proxy-sdk-php.svg)](https://github.com/redirectionio/proxy-sdk-php)
4-
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)
5-
[![Build Status](https://img.shields.io/travis/redirectionio/proxy-sdk-php/master.svg)](https://travis-ci.org/redirectionio/proxy-sdk-php)
6-
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/redirectionio/proxy-sdk-php.svg)](https://scrutinizer-ci.com/g/redirectionio/proxy-sdk-php)
7-
[![Quality Score](https://img.shields.io/scrutinizer/g/redirectionio/proxy-sdk-php.svg)](https://scrutinizer-ci.com/g/redirectionio/proxy-sdk-php)
8-
9-
[![Email](https://img.shields.io/badge/[email protected])](mailto:[email protected])
10-
11-
[redirection.io](https://redirection.io) is a tool to track HTTP errors and setup useful HTTP
12-
redirections. It listens your website's HTTP traffic and logs every HTTP errors,
13-
so you can check that the project's redirection rules apply efficiently.
3+
[redirection.io](https://redirection.io) is a tool to track HTTP errors and
4+
setup useful HTTP redirections. It listens your website's HTTP traffic and logs
5+
every HTTP errors, so you can check that the project's redirection rules apply
6+
efficiently.
147

158
Quick demo (see below for detailed info):
169

1710
```php
18-
$client = new RedirectionIO\Client\Sdk\Client($connectionsOptions);
11+
$client = new RedirectionIO\Client\Sdk\Client($connections);
1912
$request = new RedirectionIO\Client\Sdk\HttpMessage\Request(
2013
$_SERVER['HTTP_HOST'],
2114
$_SERVER['REQUEST_URI'],
@@ -31,7 +24,7 @@ if (null === $response) {
3124

3225
$client->log($request, $response);
3326

34-
// Finally, Returns your content or redirect
27+
// Finally, Returns your response
3528
```
3629

3730
## Requirements
@@ -54,20 +47,18 @@ Before starting, you need to instantiate a new Client.
5447
```php
5548
use RedirectionIO\Client\Sdk\Client;
5649

57-
$client = new Client(array $connectionsOptions, $timeout = 1000000, $debug = false, LoggerInterface $logger = null);
50+
$client = new Client(array $connections, int $timeout = 1000000, bool $debug = false, LoggerInterface $logger = null);
5851
```
5952

6053
Parameters:
6154

62-
- `array $connectionsOptions` array of connection(s) parameters to the Agent(s)
55+
- `array $connections` array of connection(s) parameters to the Agent(s)
6356
```php
64-
$connectionsOptions = [
65-
'connection1' => ['host' => 'host1', 'port' => 8001],
66-
'connection2' => ['host' => 'host2', 'port' => 8002],
67-
...
57+
$connections = [
58+
'connection tcp' => 'tcp://127.0.0.1:20301',
59+
'connection unix' => 'unix:///var/run/redirectionio_agent.sock',
6860
];
6961

70-
// Note: 'host' and 'port' options are both required
7162
```
7263
- `$timeout` timeout in microsecond for connection/request;
7364
- `$debug` enable or disable debug mode. In debug mode an exception is thrown is something goes wrong, if not every errors is silenced;

Tests/ClientTest.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
*/
1717
class ClientTest extends TestCase
1818
{
19-
private $host = 'localhost';
20-
private $port = 3100;
19+
private $connection = 'tcp://localhost:3100';
2120
private $client;
2221

2322
public static function setUpBeforeClass()
@@ -28,7 +27,7 @@ public static function setUpBeforeClass()
2827
public function setUp()
2928
{
3029
$this->client = new Client([
31-
'host1' => ['host' => $this->host, 'port' => $this->port],
30+
'host1' => $this->connection,
3231
]);
3332
}
3433

@@ -47,7 +46,7 @@ public function testFindRedirectWhenExistUsingUnixSocket()
4746
{
4847
$agent = static::startAgent(['socket_type' => 'AF_UNIX']);
4948

50-
$client = new Client(['host1' => ['remote_socket' => sys_get_temp_dir().'/fake_agent.sock']]);
49+
$client = new Client(['host1' => 'unix://'.sys_get_temp_dir().'/fake_agent.sock']);
5150

5251
$request = $this->createRequest(['path' => '/foo']);
5352

@@ -87,7 +86,7 @@ public function testFindRedirectWhenNotExist()
8786
public function testFindRedirectWhenAgentDown()
8887
{
8988
$client = new Client([
90-
'host1' => ['host' => 'unknown-host', 'port' => 80],
89+
'host1' => 'tcp://unknown-host:80',
9190
]);
9291

9392
$request = $this->createRequest();
@@ -104,7 +103,7 @@ public function testFindRedirectWhenAgentDown()
104103
public function testFindRedirectWhenAgentDownAndDebug()
105104
{
106105
$client = new Client([
107-
'host1' => ['host' => 'unknown-host', 'port' => 80],
106+
'host1' => 'tcp://unknown-host:80',
108107
], 1000000, true);
109108

110109
$request = $this->createRequest();
@@ -123,7 +122,7 @@ public function testLogRedirection()
123122
public function testLogRedirectionWhenAgentDown()
124123
{
125124
$client = new Client([
126-
'host1' => ['host' => 'unknown-host', 'port' => 80],
125+
'host1' => 'tcp://unknown-host:80',
127126
]);
128127

129128
$request = $this->createRequest();
@@ -139,7 +138,7 @@ public function testLogRedirectionWhenAgentDown()
139138
public function testLogRedirectionWhenAgentDownAndDebug()
140139
{
141140
$client = new Client([
142-
'host1' => ['host' => 'unknown-host', 'port' => 80],
141+
'host1' => 'tcp://unknown-host:80',
143142
], 1000000, true);
144143

145144
$request = $this->createRequest();
@@ -151,9 +150,9 @@ public function testLogRedirectionWhenAgentDownAndDebug()
151150
public function testCanFindWorkingHostInMultipleHostsArray()
152151
{
153152
$client = new Client([
154-
'host1' => ['host' => 'unknown-host', 'port' => 80],
155-
'host2' => ['host' => 'unknown-host', 'port' => 81],
156-
'host3' => ['host' => $this->host, 'port' => $this->port],
153+
'host1' => 'tcp://unknown-host:80',
154+
'host2' => 'tcp://unknown-host:81',
155+
'host3' => $this->connection,
157156
]);
158157
$request = $this->createRequest(['path' => '/foo']);
159158

@@ -169,9 +168,9 @@ public function testWhenAgentGoesDown()
169168
$agent = static::startAgent(['port' => 3101]);
170169

171170
$client = new Client([
172-
'host1' => ['host' => 'unknown-host', 'port' => 80],
173-
'host2' => ['host' => 'unknown-host', 'port' => 81],
174-
'host3' => ['host' => $this->host, 'port' => 3101],
171+
'host1' => 'tcp://unknown-host:80',
172+
'host2' => 'tcp://unknown-host:81',
173+
'host3' => 'tcp://localhost:3101',
175174
]);
176175
$request = $this->createRequest(['path' => '/foo']);
177176

@@ -197,15 +196,6 @@ public function testCannotAllowInstantiationWithEmptyConnectionsOptions()
197196
$client = new Client([]);
198197
}
199198

200-
/**
201-
* @expectedException \RedirectionIO\Client\Sdk\Exception\BadConfigurationException
202-
* @expectedExceptionMessage The required options "host", "port" are missing.
203-
*/
204-
public function testCannotAllowInstantiationWithEmptyConnectionOptions()
205-
{
206-
$client = new Client([[]]);
207-
}
208-
209199
private static function startAgent($options = [])
210200
{
211201
$socket_type = isset($options['socket_type']) ? $options['socket_type'] : 'AF_INET';

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
"extra": {
4242
"branch-alias": {
43-
"dev-master": "1.0.x-dev"
43+
"dev-master": "1.x-dev"
4444
}
4545
}
4646
}

src/Client.php

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@
1313

1414
class Client
1515
{
16-
private $connectionsOptions;
17-
private $currentConnection;
18-
private $currentConnectionName;
16+
private $connections;
1917
private $timeout;
2018
private $debug;
2119
private $logger;
20+
private $currentConnection;
21+
private $currentConnectionName;
2222

2323
/**
24-
* @param array $connectionsOptions
25-
* @param int $timeout
26-
* @param bool $debug
27-
* @param LoggerInterface $logger
24+
* @param int $timeout
25+
* @param bool $debug
2826
*/
29-
public function __construct(array $connectionsOptions, $timeout = 10000, $debug = false, LoggerInterface $logger = null)
27+
public function __construct(array $connections, $timeout = 10000, $debug = false, LoggerInterface $logger = null)
3028
{
31-
if (empty($connectionsOptions)) {
29+
if (!$connections) {
3230
throw new BadConfigurationException('At least one connection is required.');
3331
}
3432

35-
foreach ($connectionsOptions as $connectionName => $connectionOptions) {
36-
$this->connectionsOptions[$connectionName] = $this->resolveConnectionOptions($connectionOptions);
33+
foreach ($connections as $name => $connection) {
34+
$this->connections[$name] = [
35+
'remote_socket' => $connection,
36+
'retries' => 2,
37+
];
3738
}
3839

3940
$this->timeout = $timeout;
@@ -94,36 +95,6 @@ public function log(Request $request, Response $response)
9495
}
9596
}
9697

97-
private function resolveConnectionOptions(array $options = [])
98-
{
99-
if (isset($options['remote_socket'])) {
100-
$remoteSocket = explode(':', $options['remote_socket']);
101-
102-
if (!isset($remoteSocket[0]) || isset($remoteSocket[2])) {
103-
throw new BadConfigurationException('The option "remote_socket" should have "/link/to/agent/socket" or "ip_agent:port" format.');
104-
}
105-
106-
if (!isset($remoteSocket[1])) {
107-
$options['remote_socket'] = 'unix://'.$remoteSocket[0];
108-
} else {
109-
$options['remote_socket'] = sprintf('tcp://%s:%s', $remoteSocket[0], $remoteSocket[1]);
110-
}
111-
112-
$options['retries'] = 2;
113-
114-
return $options;
115-
}
116-
117-
if (!isset($options['host']) || !isset($options['port'])) {
118-
throw new BadConfigurationException('The required options "host", "port" are missing.');
119-
}
120-
121-
$options['remote_socket'] = sprintf('tcp://%s:%s', $options['host'], $options['port']);
122-
$options['retries'] = 2;
123-
124-
return $options;
125-
}
126-
12798
private function request($command, $context)
12899
{
129100
$connection = $this->getConnection();
@@ -134,10 +105,10 @@ private function request($command, $context)
134105
// if the pipe is broken, `fwrite` will throw a Notice
135106
if (false === $sent) {
136107
$this->logger->debug('Impossible to send content to the connection.', [
137-
'options' => $this->connectionsOptions[$this->currentConnectionName],
108+
'options' => $this->connections[$this->currentConnectionName],
138109
]);
139110

140-
--$this->connectionsOptions[$this->currentConnectionName]['retries'];
111+
--$this->connections[$this->currentConnectionName]['retries'];
141112
$this->currentConnection = null;
142113

143114
return $this->request($command, $context);
@@ -148,10 +119,10 @@ private function request($command, $context)
148119
// false: the persistent connection is stale
149120
if (false === $received) {
150121
$this->logger->debug('Impossible to get content from the connection.', [
151-
'options' => $this->connectionsOptions[$this->currentConnectionName],
122+
'options' => $this->connections[$this->currentConnectionName],
152123
]);
153124

154-
--$this->connectionsOptions[$this->currentConnectionName]['retries'];
125+
--$this->connections[$this->currentConnectionName]['retries'];
155126
$this->currentConnection = null;
156127

157128
return $this->request($command, $context);
@@ -166,31 +137,31 @@ private function getConnection()
166137
return $this->currentConnection;
167138
}
168139

169-
foreach ($this->connectionsOptions as $name => $connectionOptions) {
170-
if ($connectionOptions['retries'] <= 0) {
140+
foreach ($this->connections as $name => $connection) {
141+
if ($connection['retries'] <= 0) {
171142
continue;
172143
}
173144

174145
$this->logger->debug('New connection chosen. Trying to connect.', [
175-
'connectionOptions' => $connectionOptions,
146+
'connection' => $connection,
176147
'name' => $name,
177148
]);
178149

179-
$connection = $this->box('doConnect', false, [$connectionOptions]);
150+
$connection = $this->box('doConnect', false, [$connection]);
180151

181152
if (false === $connection) {
182153
$this->logger->debug('Impossible to connect to the connection.', [
183-
'connectionOptions' => $connectionOptions,
154+
'connection' => $connection,
184155
'name' => $name,
185156
]);
186157

187-
$this->connectionsOptions[$name]['retries'] = 0;
158+
$this->connections[$name]['retries'] = 0;
188159

189160
continue;
190161
}
191162

192163
$this->logger->debug('New connection approved.', [
193-
'connectionOptions' => $connectionOptions,
164+
'connection' => $connection,
194165
'name' => $name,
195166
]);
196167

@@ -203,7 +174,7 @@ private function getConnection()
203174
}
204175

205176
$this->logger->error('Can not find an agent.', [
206-
'connections_options' => $this->connectionsOptions,
177+
'connections_options' => $this->connections,
207178
]);
208179

209180
throw new AgentNotFoundException();

0 commit comments

Comments
 (0)