1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of graze/dynamark3-client. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @license https://github.com/graze/dynamark3-client/blob/master/LICENSE.md |
12
|
|
|
* @link https://github.com/graze/dynamark3-client |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Graze\Dynamark3Client; |
16
|
|
|
|
17
|
|
|
use \Graze\TelnetClient\TelnetClientInterface; |
18
|
|
|
use \Graze\Dynamark3Client\CommandResolver; |
19
|
|
|
use \Graze\Dynamark3Client\Command\CommandInterface; |
20
|
|
|
use \Graze\TelnetClient\TelnetClient; |
21
|
|
|
use \Graze\Dynamark3Client\Dynamark3Constants; |
22
|
|
|
|
23
|
|
|
class Dynamark3Client implements Dynamark3ClientInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TelnetClientInterface |
27
|
|
|
*/ |
28
|
|
|
protected $telnet; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var CommandResolver |
32
|
|
|
*/ |
33
|
|
|
protected $commandResolver; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param TelnetClientInterface $telnet |
37
|
|
|
* @param CommandResolver $commandResolver |
38
|
|
|
*/ |
39
|
3 |
|
public function __construct(TelnetClientInterface $telnet, CommandResolver $commandResolver) |
40
|
|
|
{ |
41
|
3 |
|
$this->telnet = $telnet; |
42
|
3 |
|
$this->commandResolver = $commandResolver; |
43
|
3 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $dsn |
47
|
|
|
*/ |
48
|
1 |
|
public function connect($dsn) |
49
|
|
|
{ |
50
|
1 |
|
$this->telnet->connect( |
51
|
1 |
|
$dsn, |
52
|
1 |
|
Dynamark3Constants::PROMPT, |
53
|
1 |
|
Dynamark3Constants::PROMPT_ERROR, |
54
|
1 |
|
Dynamark3Constants::LINE_ENDING |
55
|
|
|
); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* @param array $arguments |
61
|
|
|
* |
62
|
|
|
* @return Dynamark3ResponseInterface |
63
|
|
|
*/ |
64
|
1 |
|
public function __call($name, array $arguments) |
65
|
|
|
{ |
66
|
1 |
|
$command = $this->commandResolver->resolve($name); |
67
|
1 |
|
$telnetResponse = $this->telnet->execute( |
68
|
1 |
|
$command->getCommandText() . $command->getArgumentText($arguments), |
69
|
1 |
|
$command->getPrompt() |
70
|
|
|
); |
71
|
|
|
|
72
|
1 |
|
return $command->parseResponse($telnetResponse); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return Dynamark3Client |
77
|
|
|
*/ |
78
|
1 |
|
public static function factory() |
79
|
|
|
{ |
80
|
1 |
|
return new static( |
81
|
1 |
|
TelnetClient::factory(), |
82
|
1 |
|
new CommandResolver() |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|