-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathClient.php
170 lines (140 loc) · 4.49 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Created by PhpStorm.
* User: execut
* Date: 11/8/17
* Time: 5:10 PM
*/
namespace execut\oData;
use GuzzleHttp\Exception\ConnectException;
use Kily\Tools1C\OData\RequestException;
use yii\base\Component;
use yii\caching\TagDependency;
class Client extends Component
{
public $host = null;
public $path = null;
public $options = [];
public $customColumnsTypes = [];
protected $_client = null;
protected $cache = [];
protected $currentHost = null;
protected $e = null;
protected $_logger = null;
public function resetStaticCache() {
$this->cache = [];
}
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
}
public function setLogger($logger) {
if (is_array($logger)) {
$logger = \yii::createObject($logger);
}
$this->_logger = $logger;
}
public function getLogger() {
return $this->_logger;
}
public function __call($name, $params)
{
if (!empty($params[1]) && $params[1] === ActiveQuery::EMPTY_CONDITION_STUB) {
$this->getClient()->reset();
return [];
}
if ($this->isConnectionError()) {
if ($this->e) {
$message = $this->e->getMessage();
} else {
$message = '';
}
$this->getClient()->reset();
throw new Exception($message);
}
try {
if ($name === 'get') {
$cacheKey = $name . $this->_gettedTable . serialize($params);
if ($result = $this->getCache($cacheKey)) {
$this->getClient()->reset();
return $result;
}
} else {
$this->cache = [];
}
$result = call_user_func_array([$this->getClient(), $name], $params);
if ($name === 'get') {
$this->setCache($cacheKey, $result);
}
} catch (\Exception $e) {
$this->getClient()->reset();
$this->setIsConnectionError(true);
$this->e = $e;
$message = $e->getMessage()
. '. OData query "' . $name . '" params "' . var_export($params, true)
. '". Trace: ' . $e->getTraceAsString();
if (strpos($e->getMessage(), 'Ошибка при разборе опции запроса')) {
$message .= '. ' . var_export($params[1], true);
}
throw new Exception($message, $e->getCode(), $e);
}
return $result;
}
public function getCache($key) {
if (!empty($this->cache[$key])) {
return $this->cache[$key];
}
}
public function setCache($key, $value) {
$this->cache[$key] = $value;
return $this;
}
protected $_gettedTable = null;
public function __get($name)
{
$this->_gettedTable = $name;
$this->getClient()->$name;
return $this;
}
protected $isConnectionError = [];
public function isConnectionError() {
if (array_key_exists($this->currentHost, $this->isConnectionError)) {
return $this->isConnectionError[$this->currentHost];
}
$cacheValue = \yii::$app->cache->get($this->getCacheKey());
if ($cacheValue) {
try {
$this->getClient()->reset();
$this->getClient()->Catalog_Контрагенты->get(null, null, [
'query' => [
'$top' => 1,
]
]);
} catch (\Exception $e) {
$this->getClient()->reset();
$this->e = $e;
return $this->isConnectionError[$this->currentHost] = true;
}
\yii::$app->cache->delete($this->getCacheKey());
}
return $this->isConnectionError[$this->currentHost] = false;
}
protected function getCacheKey() {
return __CLASS__ . '-' . $this->currentHost;
}
public function setIsConnectionError($value) {
if ($value) {
\yii::$app->cache->set($this->getCacheKey(), true);
}
}
/**
* @return \Kily\Tools1C\OData\Client
*/
protected function getClient()
{
if ($this->_client === null) {
$this->_client = new \Kily\Tools1C\OData\Client(trim($this->host, '/') . '/' . $this->path, $this->options, $this->getLogger());
}
return $this->_client;
}
}