|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Manticoresearch; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Psr\Log\NullLogger; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Transport |
|
11
|
|
|
* @package Manticoresearch |
|
12
|
|
|
*/ |
|
13
|
|
|
class Transport |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Connection |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $connection; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var LoggerInterface|NullLogger |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $logger; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Transport constructor. |
|
27
|
|
|
* @param Connection|null $connection |
|
28
|
|
|
* @param LoggerInterface|null $logger |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Connection $connection = null, LoggerInterface $logger = null) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($connection) { |
|
33
|
|
|
$this->connection = $connection; |
|
34
|
|
|
} |
|
35
|
|
|
$this->logger = $logger ?? new NullLogger(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return Connection|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getConnection() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->connection; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Connection $connection |
|
48
|
|
|
* @return Transport |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setConnection(Connection $connection): Transport |
|
51
|
|
|
{ |
|
52
|
|
|
$this->connection = $connection; |
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $transport |
|
58
|
|
|
* @param Connection $connection |
|
59
|
|
|
* @param LoggerInterface $logger |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
* @throws \Exception |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function create($transport, Connection $connection, LoggerInterface $logger) |
|
64
|
|
|
{ |
|
65
|
|
|
if (is_string($transport)) { |
|
|
|
|
|
|
66
|
|
|
$className = "Manticoresearch\\Transport\\$transport"; |
|
67
|
|
|
if (class_exists($className)) { |
|
68
|
|
|
$transport = new $className($connection, $logger); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
if ($transport instanceof self) { |
|
72
|
|
|
$transport->setConnection($connection); |
|
73
|
|
|
} else { |
|
74
|
|
|
throw new \Exception('Bad transport'); |
|
75
|
|
|
} |
|
76
|
|
|
return $transport; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $uri |
|
81
|
|
|
* @param array $query |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function setupURI(string $uri, $query = []): string |
|
85
|
|
|
{ |
|
86
|
|
|
if (!empty($query)) { |
|
87
|
|
|
foreach ($query as $k => $v) { |
|
88
|
|
|
if (is_bool($v)) { |
|
89
|
|
|
$query[$k] = $v ? 'true' : 'false'; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
$uri .= '?' . http_build_query($query); |
|
93
|
|
|
} |
|
94
|
|
|
return $uri; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|