-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathAbstractDriver.php
More file actions
222 lines (190 loc) · 5.72 KB
/
AbstractDriver.php
File metadata and controls
222 lines (190 loc) · 5.72 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace Muffin\Webservice;
use Cake\Core\App;
use Cake\Core\InstanceConfigTrait;
use Cake\Utility\Inflector;
use Muffin\Webservice\Exception\MissingWebserviceClassException;
use Muffin\Webservice\Exception\UnimplementedWebserviceMethodException;
use Muffin\Webservice\Webservice\Webservice;
use Muffin\Webservice\Webservice\WebserviceInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use RuntimeException;
abstract class AbstractDriver implements LoggerAwareInterface
{
use InstanceConfigTrait;
use LoggerAwareTrait;
protected $_client;
protected $_defaultConfig = [];
/**
* Whatever queries should be logged
*
* @var bool
*/
protected $_logQueries = false;
/**
* The list of webservices to be used
*
* @var array
*/
protected $_webservices = [];
/**
* Constructor.
*
* @param array $config Custom configuration.
*/
public function __construct($config)
{
$this->config($config);
$this->initialize();
}
/**
* Initialize is used to easily extend the constructor.
*
* @return void
*/
abstract public function initialize();
/**
* Set or return an instance of the client used for communication
*
* @param object $client The client to use
*
* @return $this
*/
public function client($client = null)
{
if ($client === null) {
return $this->_client;
}
$this->_client = $client;
return $this;
}
/**
* Set or get a instance of a webservice
*
* @param string $name The name of the webservice
* @param \Muffin\Webservice\Webservice\WebserviceInterface|null $webservice The instance of the webservice you'd like to set
*
* @return $this
*/
public function webservice($name, WebserviceInterface $webservice = null)
{
if ($webservice !== null) {
$this->_webservices[$name] = $webservice;
return $this;
}
if (!isset($this->_webservices[$name])) {
list($pluginName) = pluginSplit(App::shortName(get_class($this), 'Webservice/Driver'));
$webserviceClass = implode('.', array_filter([$pluginName, Inflector::camelize($name)]));
$webservice = $this->_createWebservice($webserviceClass, [
'endpoint' => $name,
'driver' => $this,
]);
$this->_webservices[$name] = $webservice;
}
return $this->_webservices[$name];
}
/**
* Returns a logger instance
*
* @return \Psr\Log\LoggerInterface
*/
public function logger()
{
return $this->logger;
}
/**
* Returns the connection name used in the configuration
*
* @return string
*/
public function configName()
{
return (string)$this->_config['name'];
}
/**
* Enables or disables query logging for this driver
*
* @param bool|null $enable whether to turn logging on or disable it.
* Use null to read current value.
*
* @return bool
*/
public function logQueries($enable = null)
{
if ($enable === null) {
return $this->_logQueries;
}
$this->_logQueries = $enable;
}
/**
* Proxies the client's methods.
*
* @param string $method Method name.
* @param array $args Arguments to pass-through.
*
* @return mixed
*
* @throws \RuntimeException If the client object has not been initialized.
* @throws \Muffin\Webservice\Exception\UnimplementedWebserviceMethodException If the method does not exist in the client.
*/
public function __call($method, $args)
{
stackTrace();
debug($method);exit();
if (!is_object($this->client())) {
throw new RuntimeException(sprintf(
'The `%s` client has not been initialized',
$this->config('name')
));
}
if (!method_exists($this->client(), $method)) {
throw new UnimplementedWebserviceMethodException([
'name' => $this->config('name'),
'method' => $method
]);
}
return call_user_func_array([$this->_client, $method], $args);
}
/**
* Returns a handy representation of this driver
*
* @return array
*/
public function __debugInfo()
{
return [
'client' => $this->client(),
'logger' => $this->logger(),
'webservices' => array_keys($this->_webservices)
];
}
/**
* Creates a Webservice instance.
*
* It provides a fallback to PluginNameWebservice.
*
* @param string $className Class name of the webservice to initialize
* @param array $options Set of options to pass to the constructor
*
* @return WebserviceInterface
*/
protected function _createWebservice($className, array $options = [])
{
$namespaceParts = explode('\\', get_class($this));
$pluginName = implode('/', array_reverse(array_slice(array_reverse($namespaceParts), -2)));
$webservice = App::className($className, 'Webservice', 'Webservice');
if ($webservice) {
return new $webservice($options);
}
$fallbackWebserviceClass = $pluginName . '.' . end($namespaceParts);
$fallbackWebservice = App::className($fallbackWebserviceClass, 'Webservice', 'Webservice');
if ($fallbackWebservice) {
return new $fallbackWebservice($options);
}
throw new MissingWebserviceClassException([
'class' => $className,
'fallbackClass' => $fallbackWebserviceClass
]);
}
}