From e0405659880b44ea51e9259406d94842e44a3d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 22 Mar 2016 22:54:26 +0100 Subject: [PATCH 1/4] Update react/socket-client dependency to all supported versions --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 432fdcd..c3ccdec 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": ">=5.3", "react/promise": "~1.0|~2.0", - "react/socket-client": "0.3.*|0.4.*", + "react/socket-client": "^0.5 || ^0.4 || ^0.3", "react/event-loop": "0.3.*|0.4.*" }, "require-dev": { From 59dd121cacfd56333ebde8efcc6033cc5b23958f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 4 Sep 2017 09:51:29 +0200 Subject: [PATCH 2/4] Revert "Temporarily remove explicit dependency on Evenement to fix install" This reverts commit bcb7dbd2ed879609bb38f7e614d1d13b7dddbb00. --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index c3ccdec..b28f62a 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ ], "require": { "php": ">=5.3", + "evenement/evenement": "~1.0|~2.0", "react/promise": "~1.0|~2.0", "react/socket-client": "^0.5 || ^0.4 || ^0.3", "react/event-loop": "0.3.*|0.4.*" From 29164c336b02f50b2822a0d3624d154dfc61e29b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 23 Mar 2016 23:37:42 +0100 Subject: [PATCH 3/4] Inject SocketClient via ConnectorInterface --- src/Factory.php | 3 ++- tests/FactoryTest.php | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/FactoryTest.php diff --git a/src/Factory.php b/src/Factory.php index 5e18aab..a5d9056 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -3,6 +3,7 @@ namespace Clue\React\Ami; use React\EventLoop\LoopInterface; +use React\SocketClient\ConnectorInterface; use React\SocketClient\Connector; use React\SocketClient\SecureConnector; use React\Dns\Resolver\Factory as ResolverFactory; @@ -15,7 +16,7 @@ class Factory private $connector; private $secureConnector; - public function __construct(LoopInterface $loop, Connector $connector = null, SecureConnector $secureConnector = null) + public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, ConnectorInterface $secureConnector = null) { if ($connector === null) { $resolverFactory = new ResolverFactory(); diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php new file mode 100644 index 0000000..cd211a1 --- /dev/null +++ b/tests/FactoryTest.php @@ -0,0 +1,41 @@ +loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $this->tcp = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); + $this->tls = $this->getMockBuilder('React\SocketClient\ConnectorInterface')->getMock(); + + $this->factory = new Factory($this->loop, $this->tcp, $this->tls); + } + + public function testDefaultCtor() + { + $this->factory = new Factory($this->loop); + } + + public function testCreateClientUsesTcpConnectorWithDefaultLocation() + { + $promise = new Promise(function () { }); + $this->tcp->expects($this->once())->method('create')->with('127.0.0.1', 5038)->willReturn($promise); + + $this->factory->createClient(); + } + + public function testCreateClientUsesTcpConnectorWithLocalhostLocation() + { + $promise = new Promise(function () { }); + $this->tcp->expects($this->once())->method('create')->with('127.0.0.1', 5038)->willReturn($promise); + + $this->factory->createClient('localhost'); + } +} From a499a4582ebc24bf6bb2bf3311bc84a38a061a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 23 Mar 2016 23:38:52 +0100 Subject: [PATCH 4/4] Fix using secure connector for TLS/SSL urls --- README.md | 29 +++++++++++++++++++++++++---- src/Factory.php | 2 +- tests/FactoryTest.php | 8 ++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bcd2b26..e64b07b 100644 --- a/README.md +++ b/README.md @@ -104,9 +104,9 @@ It helps with establishing a plain TCP/IP or secure SSL/TLS connection to the AM and issuing an initial `login` action. ```php -$factory->createClient('user:secret@localhost')->then( +$factory->createClient($amiUrl)->then( function (Client $client) { - // client connected and authenticated + // client connected (and authenticated) }, function (Exception $e) { // an error occured while trying to connect or authorize client @@ -114,8 +114,29 @@ $factory->createClient('user:secret@localhost')->then( ); ``` -> Note: The given $amiUrl *must* include a host, it *should* include a username and secret -> and it *can* include a scheme (tcp/ssl) and port definition. +The `$amiUrl` contains the host and optional port to connect to: + +```php +$factory->createClient('127.0.0.1:5038'); +``` + +> If the `$amiUrl` is `null` (or omitted) this method defaults to connecting + to your local host (`127.0.0.1:5038`). + +The above examples to 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'); +``` + +The `Factory` defaults to establishing a plaintext TCP connection. +If you want to connect through a secure TLS proxy, you can use the `tls` scheme: + +```php +$factory->createClient('tls://user:secret@localhost:12345'); +``` ### Client diff --git a/src/Factory.php b/src/Factory.php index a5d9056..36884f3 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -35,7 +35,7 @@ public function createClient($address = null) { $parts = $this->parseUrl($address); - $secure = (isset($parts['schema']) && $parts['schema'] !== 'tcp'); + $secure = (isset($parts['scheme']) && $parts['scheme'] !== 'tcp'); $connector = $secure ? $this->secureConnector : $this->connector; $promise = $connector->create($parts['host'], $parts['port'])->then(function (Stream $stream) { diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index cd211a1..33d303f 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -38,4 +38,12 @@ public function testCreateClientUsesTcpConnectorWithLocalhostLocation() $this->factory->createClient('localhost'); } + + public function testCreateClientUsesTlsConnectorWithTlsLocation() + { + $promise = new Promise(function () { }); + $this->tls->expects($this->once())->method('create')->with('ami.local', 1234)->willReturn($promise); + + $this->factory->createClient('tls://ami.local:1234'); + } }