Skip to content

Commit c36f52d

Browse files
committed
Created RouterErrorException and made it be thrown by various Util methods (more to come);
Tweaked root menu storage (setMenu() and getMenu() more specifically), so that it allows Util::newRequest() from it properly; Fixed Script::parseValueToArray() exception code; Many other changes (see RELEASE-1.0.0b6 diff).
1 parent 5ed1fa6 commit c36f52d

File tree

10 files changed

+380
-276
lines changed

10 files changed

+380
-276
lines changed

RELEASE-1.0.0b6

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Util stuff, mostly.
22

33
* __BREAKING CHANGES:__
4+
- Removed all $mode arguments from all Countable implementations (turns out no stable 5.6+ releases uses it...), and the COUNT_RECURSIVE implementations (which were mostly useless anyway). Util::count() has the $query argument as its first and only one.
45
- Util::edit() is no longer an alias of Util::set(). It's now its own method that can modify or unset a single specified property.
56
- Util's escapeValue(), escapeString(), parseValue(), prepareScript() and appendScript() methods are now in their own new class called "Script", with the last two now being called just prepare() and append().
67
- Script::escapeValue() now converts DateTime objects into a string having the "M/d/Y H:i:s" format (used across RouterOS), or just "M/d/Y" if the time is exactly midnight, and the timezone is UTC. The old behaviour can be achieved by "manually" adding the DateTime to "new DateTime('@0')".
@@ -10,8 +11,12 @@ Util stuff, mostly.
1011
- newRequest()
1112
* Script::parseValue() now supports letter notation for time (1h2m3s), not just double colon notation (01:02:03), modeled after RouterOS. Related to that is also that leading zeroes, and zero minutes and seconds are now optional (e.g. "1:" is a valid way of saying 1 hour). Sub-second information is rounded up to the nearest second on current PHP versions (future versions are expected to support sub-second information in DateInterval by allowing seconds to be a double; The code currently attempts to give DateInterval a double, falling back to rounding to a second).
1213
* Script::parseValue() now recognizes dates in the "M/d/Y H:i:s" format (used across RouterOS), and turns that into a DateTime object for that time (or midnight in UTC if the time part is omitted).
14+
* Util::exec() now returns the script source after execution. Previously, this was a private option, but is now the only way, and it's public. Intended to be used for easier output extraction.
1315
* Util::getAll() now throws a NotSupportedException if the arguments "follow", "follow-only" or "count-only" are used. The first two, because PHP would hang (since Client::sendSync() is used under the hood), and the last one because it's unredable in the returned output (use Util::count() instead).
1416
* Util::setMenu() can now go back to the root menu.
17+
* Util::get() can now accept a query as an argument for $number
18+
* Util::get() can have $valueName set to NULL (as is now by default), in which case all properties are retrieved in an array, parsed with Script::parseValueToArray().
19+
* Util::get() no longer fallbacks automatically with a "print" request for buggy versions. You may "manually" call Util::getAll() if you suspect that's an issue.
1520
* Util::find() now works even when the underlying Client streams responses.
1621
* Util::move()'s second argument $destination is now optional, and without it (or if set to NULL), the item is moved at the bottom of the menu.
1722
* Client::login() consumes the !done or !fatal response even when called on an already logged in connection.

src/PEAR2/Net/RouterOS/Message.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,24 +181,13 @@ public function getIterator()
181181
}
182182

183183
/**
184-
* Counts the number of arguments.
184+
* Counts the number of attributes.
185185
*
186-
* @param int $mode The counter mode.
187-
* Either COUNT_NORMAL or COUNT_RECURSIVE.
188-
* When in normal mode, counts the number of arguments.
189-
* When in recursive mode, counts the number of API words
190-
* (including the empty word at the end).
191-
*
192-
* @return int The number of arguments/words.
186+
* @return int The number of attributes.
193187
*/
194-
public function count($mode = COUNT_NORMAL)
188+
public function count()
195189
{
196-
$result = count($this->attributes);
197-
if ($mode !== COUNT_NORMAL) {
198-
$result += 2/*first+last word*/
199-
+ (int)(null !== $this->getTag());
200-
}
201-
return $result;
190+
return count($this->attributes);
202191
}
203192

204193
/**

src/PEAR2/Net/RouterOS/Response.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -316,23 +316,4 @@ public function getUnrecognizedWords()
316316
{
317317
return $this->unrecognizedWords;
318318
}
319-
320-
/**
321-
* Counts the number of arguments or words.
322-
*
323-
* @param int $mode The counter mode.
324-
* Either COUNT_NORMAL or COUNT_RECURSIVE.
325-
* When in normal mode, counts the number of arguments.
326-
* When in recursive mode, counts the number of API words.
327-
*
328-
* @return int The number of arguments/words.
329-
*/
330-
public function count($mode = COUNT_NORMAL)
331-
{
332-
$result = parent::count($mode);
333-
if ($mode !== COUNT_NORMAL) {
334-
$result += count($this->unrecognizedWords);
335-
}
336-
return $result;
337-
}
338319
}

src/PEAR2/Net/RouterOS/ResponseCollection.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,26 +217,13 @@ public function toArray($useIndex = false)
217217
}
218218

219219
/**
220-
* Counts the responses/words in the collection.
221-
*
222-
* @param int $mode The counter mode.
223-
* Either COUNT_NORMAL or COUNT_RECURSIVE.
224-
* When in normal mode, counts the number of responses.
225-
* When in recursive mode, counts the total number of API words.
220+
* Counts the responses in the collection.
226221
*
227222
* @return int The number of responses in the collection.
228223
*/
229-
public function count($mode = COUNT_NORMAL)
224+
public function count()
230225
{
231-
if ($mode !== COUNT_NORMAL) {
232-
$result = 0;
233-
foreach ($this->responses as $response) {
234-
$result += $response->count($mode);
235-
}
236-
return $result;
237-
} else {
238-
return count($this->responses);
239-
}
226+
return count($this->responses);
240227
}
241228

242229
/**
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
* ~~summary~~
5+
*
6+
* ~~description~~
7+
*
8+
* PHP version 5
9+
*
10+
* @category Net
11+
* @package PEAR2_Net_RouterOS
12+
* @author Vasil Rangelov <[email protected]>
13+
* @copyright 2011 Vasil Rangelov
14+
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
15+
* @version GIT: $Id$
16+
* @link http://pear2.php.net/PEAR2_Net_RouterOS
17+
*/
18+
/**
19+
* The namespace declaration.
20+
*/
21+
namespace PEAR2\Net\RouterOS;
22+
23+
/**
24+
* Base of this class.
25+
*/
26+
use RuntimeException;
27+
28+
/**
29+
* Refered to in the constructor.
30+
*/
31+
use Exception as E;
32+
33+
/**
34+
* Exception thrown by higher level classes (Util, etc.) when the router
35+
* returns an error.
36+
*
37+
* @category Net
38+
* @package PEAR2_Net_RouterOS
39+
* @author Vasil Rangelov <[email protected]>
40+
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
41+
* @link http://pear2.php.net/PEAR2_Net_RouterOS
42+
*/
43+
class RouterErrorException extends RuntimeException implements Exception
44+
{
45+
const CODE_ITEM_ERROR = 0x100000;
46+
const CODE_SCRIPT_ERROR = 0x200000;
47+
const CODE_READ_ERROR = 0x010000;
48+
const CODE_WRITE_ERROR = 0x020000;
49+
const CODE_EXEC_ERROR = 0x040000;
50+
51+
const CODE_CACHE_ERROR = 0x100001;
52+
const CODE_GET_ERROR = 0x110001;
53+
const CODE_GETALL_ERROR = 0x110002;
54+
const CODE_ADD_ERROR = 0x120001;
55+
const CODE_SET_ERROR = 0x120002;
56+
const CODE_REMOVE_ERROR = 0x120004;
57+
const CODE_SCRIPT_GET_ERROR = 0x210001;
58+
const CODE_SCRIPT_ADD_ERROR = 0x220001;
59+
const CODE_SCRIPT_REMOVE_ERROR = 0x220004;
60+
const CODE_SCRIPT_RUN_ERROR = 0x240001;
61+
62+
/**
63+
* @var ResponseCollection|null The complete response returned by the router.
64+
*/
65+
private $_responses = null;
66+
67+
/**
68+
* Creates a new RouterErrorException.
69+
*
70+
* @param string $message The Exception message to throw.
71+
* @param int $code The Exception code.
72+
* @param E|null $previous The previous exception used for
73+
* the exception chaining.
74+
* @param ResponseCollection|null $responses The complete set responses
75+
* returned by the router.
76+
*/
77+
public function __construct(
78+
$message,
79+
$code = 0,
80+
E $previous = null,
81+
ResponseCollection $responses = null
82+
) {
83+
parent::__construct($message, $code, $previous);
84+
$this->_responses = $responses;
85+
}
86+
87+
/**
88+
* Gets the complete set responses returned by the router.
89+
*
90+
* @return ResponseCollection|null The complete set responses
91+
* returned by the router.
92+
*/
93+
public function getResponses()
94+
{
95+
return $this->_responses;
96+
}
97+
98+
// @codeCoverageIgnoreStart
99+
// String representation is not reliable in testing
100+
101+
/**
102+
* Returns a string representation of the exception.
103+
*
104+
* @return string The exception as a string.
105+
*/
106+
public function __toString()
107+
{
108+
$result = parent::__toString();
109+
if ($this->_responses instanceof ResponseCollection) {
110+
$result .= "\nResponse collection:\n" .
111+
print_r($this->_responses->toArray(), true);
112+
}
113+
return $result;
114+
}
115+
116+
// @codeCoverageIgnoreEnd
117+
}

src/PEAR2/Net/RouterOS/Script.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public static function parseValueToArray(
395395
}
396396
throw new ParserException(
397397
'The supplied value can not be converted to an array',
398-
ParserException::CODE_DATETIME
398+
ParserException::CODE_ARRAY
399399
);
400400
}
401401

0 commit comments

Comments
 (0)