From 17f0a377ae9591b7d6ac49e28ed1ac4fd96f2395 Mon Sep 17 00:00:00 2001 From: Diogo Bemfica Date: Tue, 24 Nov 2020 14:25:37 -0300 Subject: [PATCH 1/6] add options in read method to commands that have multiple responses --- README.md | 2 +- composer.json | 2 +- src/Client.php | 18 +++++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f20a256..d362e0a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ # RouterOS API Client - composer require evilfreelancer/routeros-api-php + composer require proner/routeros-api-php This library is partly based on [this old project](https://github.com/BenMenking/routeros-api), but unlike it has many innovations to ease development. In addition, the project is designed diff --git a/composer.json b/composer.json index a908f03..60127f4 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "evilfreelancer/routeros-api-php", + "name": "proner/routeros-api-php", "type": "library", "description": "Modern Mikrotik RouterOS API PHP client for your applications (with Laravel support)", "keywords": [ diff --git a/src/Client.php b/src/Client.php index ce89b02..27f1846 100644 --- a/src/Client.php +++ b/src/Client.php @@ -229,12 +229,14 @@ class Client implements Interfaces\ClientInterface * @return array|string * @since 1.0.0 */ - public function readRAW() + public function readRAW(array $options = []) { // By default response is empty $response = []; // We have to wait a !done or !fatal $lastReply = false; + // Count !re in response + $countResponse = 0; // Convert strings to array and return results if ($this->isCustomOutput()) { @@ -246,6 +248,11 @@ class Client implements Interfaces\ClientInterface while (true) { $word = $this->connector->readWord(); + //Limit response number to finish the read + if (isset($options['count']) && $countResponse >= (int)$options['count']) { + $lastReply = true; + } + if ('' === $word) { if ($lastReply) { // We received a !done or !fatal message in a precedent loop @@ -266,6 +273,11 @@ class Client implements Interfaces\ClientInterface if ('!done' === $word || '!fatal' === $word) { $lastReply = true; } + + // If we get a !re line in response, we increment the variable + if ('!re' === $word) { + $countResponse++; + } } // Parse results and return @@ -285,10 +297,10 @@ class Client implements Interfaces\ClientInterface * * @return mixed */ - public function read(bool $parse = true) + public function read(bool $parse = true, array $options = []) { // Read RAW response - $response = $this->readRAW(); + $response = $this->readRAW($options); // Return RAW configuration if custom output is set if ($this->isCustomOutput()) { From 1b1e6c01101616b647ecbc628f3aa54dc6df5e4d Mon Sep 17 00:00:00 2001 From: Diogo Bemfica Date: Tue, 24 Nov 2020 14:35:29 -0300 Subject: [PATCH 2/6] fix read method in client interface --- src/Interfaces/ClientInterface.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Interfaces/ClientInterface.php b/src/Interfaces/ClientInterface.php index 2b81e1e..aecc698 100644 --- a/src/Interfaces/ClientInterface.php +++ b/src/Interfaces/ClientInterface.php @@ -27,10 +27,11 @@ interface ClientInterface * A !fatal block precedes TCP connexion close * * @param bool $parse If need parse output to array + * @param array $options If need pass options * * @return mixed */ - public function read(bool $parse = true); + public function read(bool $parse = true, array $options = []); /** * Send write query to RouterOS (modern version of write) From 74155bbe40f07653fab9ca436518d7123de9a7a4 Mon Sep 17 00:00:00 2001 From: Diogo Bemfica Date: Fri, 27 Nov 2020 15:57:38 -0300 Subject: [PATCH 3/6] add test read method passing options --- tests/ClientTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 52adb62..77682c3 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -249,6 +249,12 @@ class ClientTest extends TestCase // $this->assertCount(13, $read); $this->assertEquals('zzzz', $read[0]['tag']); + + /* + * Build query with option count + */ + $read = $this->client->query('/interface/monitor-traffic')->read(true, ['count' => 3]); + $this->assertCount(3, $read); } public function testReadAsIterator(): void From 561cd6fcc7bbecdaf071fe37eb5380b49f94eda7 Mon Sep 17 00:00:00 2001 From: Diogo Bemfica Date: Fri, 27 Nov 2020 16:00:39 -0300 Subject: [PATCH 4/6] restored vendor name --- README.md | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d362e0a..f20a256 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ # RouterOS API Client - composer require proner/routeros-api-php + composer require evilfreelancer/routeros-api-php This library is partly based on [this old project](https://github.com/BenMenking/routeros-api), but unlike it has many innovations to ease development. In addition, the project is designed diff --git a/composer.json b/composer.json index 60127f4..a908f03 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "proner/routeros-api-php", + "name": "evilfreelancer/routeros-api-php", "type": "library", "description": "Modern Mikrotik RouterOS API PHP client for your applications (with Laravel support)", "keywords": [ From 480590bab428f4c5a939f8257151cab9ab24fd8e Mon Sep 17 00:00:00 2001 From: Diogo Bemfica Date: Fri, 27 Nov 2020 16:07:21 -0300 Subject: [PATCH 5/6] fix php doc of read method --- src/Client.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Client.php b/src/Client.php index 27f1846..796fb32 100644 --- a/src/Client.php +++ b/src/Client.php @@ -226,6 +226,8 @@ class Client implements Interfaces\ClientInterface /** * Read RAW response from RouterOS, it can be /export command results also, not only array from API * + * @param array $options + * * @return array|string * @since 1.0.0 */ @@ -294,6 +296,7 @@ class Client implements Interfaces\ClientInterface * A !fatal block precedes TCP connexion close * * @param bool $parse If need parse output to array + * @param array $options * * @return mixed */ From 5ce76283ce178ba87428eaad66d84858ff8354e9 Mon Sep 17 00:00:00 2001 From: Diogo Bemfica Date: Mon, 30 Nov 2020 13:41:46 -0300 Subject: [PATCH 6/6] add options in read method on ShortsTrait --- src/ShortsTrait.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ShortsTrait.php b/src/ShortsTrait.php index 2287a41..9495cf1 100644 --- a/src/ShortsTrait.php +++ b/src/ShortsTrait.php @@ -33,13 +33,14 @@ trait ShortsTrait * Alias for ->read() method * * @param bool $parse If need parse output to array + * @param array $options * * @return mixed * @since 0.7 */ - public function r(bool $parse = true) + public function r(bool $parse = true, array $options = []) { - return $this->read($parse); + return $this->read($parse, $options); } /**