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 |
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
|
4 |
|
public function __construct(TelnetClientInterface $telnet, CommandResolver $commandResolver) |
40
|
|
|
{ |
41
|
4 |
|
$this->telnet = $telnet; |
42
|
4 |
|
$this->commandResolver = $commandResolver; |
43
|
4 |
|
} |
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
|
|
|
Dynamark3Constants::LINE_ENDING |
55
|
1 |
|
); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* @param array $arguments |
61
|
|
|
* |
62
|
|
|
* @return \Graze\Dynamark3Client\Dynamark3ResponseInterface |
63
|
|
|
*/ |
64
|
2 |
|
public function __call($name, array $arguments) |
65
|
|
|
{ |
66
|
2 |
|
$command = $this->commandResolver->resolve($name); |
67
|
2 |
|
array_unshift($arguments, $command); |
68
|
2 |
|
return call_user_func_array([$this, 'send'], $arguments); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param CommandInterface $command |
73
|
|
|
* |
74
|
|
|
* @return \Graze\Dynamark3Client\Dynamark3ResponseInterface |
75
|
|
|
*/ |
76
|
2 |
|
protected function send(CommandInterface $command) |
77
|
|
|
{ |
78
|
2 |
|
$arguments = func_get_args(); |
79
|
2 |
|
$command = array_shift($arguments); |
80
|
|
|
|
81
|
2 |
|
$argumentsString = ''; |
82
|
2 |
|
if (!empty($arguments)) { |
83
|
|
|
// add quotes to arguments |
84
|
1 |
|
array_walk($arguments, function (&$value) { |
85
|
1 |
|
$value = sprintf('"%s"', $value); |
86
|
1 |
|
}); |
87
|
|
|
|
88
|
1 |
|
$argumentsString = ' ' . implode(' ', $arguments); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
2 |
|
$telnetResponse = $this->telnet->execute( |
92
|
2 |
|
$command->getCommandText() . $argumentsString, |
93
|
2 |
|
$command->getPrompt() |
94
|
2 |
|
); |
95
|
|
|
|
96
|
2 |
|
return $command->parseResponse($telnetResponse); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Dynamark3Client |
101
|
|
|
*/ |
102
|
1 |
|
public static function factory() |
103
|
|
|
{ |
104
|
1 |
|
return new static( |
105
|
1 |
|
TelnetClient::factory(), |
106
|
1 |
|
new CommandResolver() |
107
|
1 |
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|