From 17f0a377ae9591b7d6ac49e28ed1ac4fd96f2395 Mon Sep 17 00:00:00 2001 From: Diogo Bemfica Date: Tue, 24 Nov 2020 14:25:37 -0300 Subject: [PATCH] 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()) {