-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwsdata.module
402 lines (357 loc) · 11.1 KB
/
wsdata.module
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
/**
* @file
* Main module for wsconfig
*/
/**
* Implememnts hook_wsconfig_processor_info().
*/
function wsdata_wsconfig_processor_info() {
return array(
'wsdata_simple_xml_processor' => array(
'fields' => array(
'list_boolean' => t('WSData Simple XML Processor'),
'number_decimal' => t('WSData Simple XML Processor'),
'number_float' => t('WSData Simple XML Processor'),
'number_integer' => t('WSData Simple XML Processor'),
'list_float' => t('WSData Simple XML Processor'),
'list_integer' => t('WSData Simple XML Processor'),
'list_text' => t('WSData Simple XML Processor'),
'text_long' => t('WSData Simple XML Processor'),
'text_with_summary' => t('WSData Simple XML Processor'),
'text' => t('WSData Simple XML Processor'),
),
'data' => t('WSData Simple XML Processor'),
),
'wsdata_simple_json_processor' => array(
'fields' => array(
'list_boolean' => t('WSData Simple JSON Processor'),
'number_decimal' => t('WSData Simple JSON Processor'),
'number_float' => t('WSData Simple JSON Processor'),
'number_integer' => t('WSData Simple JSON Processor'),
'list_float' => t('WSData Simple JSON Processor'),
'list_integer' => t('WSData Simple JSON Processor'),
'list_text' => t('WSData Simple JSON Processor'),
'text_long' => t('WSData Simple JSON Processor'),
'text_with_summary' => t('WSData Simple JSON Processor'),
'text' => t('WSData Simple JSON Processor'),
),
'data' => t('WSData Simple JSON Processor'),
),
);
}
/**
* Implements hook_wsdata_language_plugin().
*/
function wsdata_wsdata_language_plugin() {
return array(
'header' => array(
'settings' => array(
'header' => 'Accept-Language',
),
'form' => 'wsdata_language_plugin_header_form',
'file' => 'wsdata.admin',
'file type' => 'inc',
'module' => 'wsdata', // @todo automatically fill in the module based on the invoke
),
'argument' => array(
'settings' => array(
'argument' => 'lang',
),
'form' => 'wsdata_language_plugin_argument_form',
'file' => 'wsdata.admin',
'file type' => 'inc',
'module' => 'wsdata',
),
'path' => array(
'settings' => array(
'position' => 0,
),
'form' => 'wsdata_language_plugin_path_form',
'file' => 'wsdata.admin',
'file type' => 'inc',
'module' => 'wsdata',
),
'uri' => array(
'settings' => array(
// Empty for now
),
'form' => 'wsdata_language_plugin_path_form',
'file' => 'wsdata.admin',
'file type' => 'inc',
'module' => 'wsdata',
),
'replace' => array(
'settings' => array(
'default' => '',
),
'form' => 'wsdata_language_plugin_replace_form',
'file' => 'wsdata.admin',
'file type' => 'inc',
'module' => 'wsdata',
),
'default' => array(
'settings' => array(),
'form' => 'wsdata_language_plugin_default_form',
'file' => 'wsdata.admin',
'file type' => 'inc',
'module' => 'wsdata',
),
);
}
/**
* Get the list defined language plugins
*
* @param string $plugin [optional]
* Specify a particular plugin to load
* @return array|boolean
* Returns an array of plugins or the specificly requested plugin, FALSE othwerwise
*/
function wsdata_get_language_plugins($plugin = NULL) {
$plugins = module_invoke_all('wsdata_language_plugin');
if (!empty($plugin)) {
if (array_key_exists($plugin, $plugins)) {
return $plugins[$plugin];
}
else {
return FALSE;
}
}
return $plugins;
}
/**
* Class definition for Web Service data parser
*/
abstract class WsData {
// Storage for parsed data
public $data;
// Storage for error information
protected $error;
// Languages which we have data for
protected $languages = FALSE;
public function __construct($data = NULL, &$entity = NULL, $lang = NULL) {
$this->entity = $entity;
if (isset($data) and $data) {
$this->addData($data, $lang);
}
}
// Return any error messages or error data
public function getError() {
return $this->error;
}
/**
* Retrieve the value for the given data key.
*
* This function retrieves data from the structured array in $this->data
* using $key as a key. $key should be a string, with the character ':'
* delimiting the parts of the key.
* I.E. The key something:someplace with retrive $this->data['something']['someplace']
* N.B. This function can be overridden to work with whatever the ->parse function
* is implemented to return.
*
* @param string $key [optional]
* Data key to load
* @param string $lang [optional]
* Language key
* @return mixed|boolean
* Returns the requested data, FALSE otherwise.
*/
public function getData($key = NULL, $lang = NULL) {
$return_data = FALSE;
if (is_array($this->data)) {
// Paths to load data from
$paths = array();
// Split the logic based on whether we have translated data
// - Return all the data for a given language
// - Return a key of data for a given language
// - Return a key of data for all languages
// First, see if we want a specific language
if ($this->languages) {
if (!is_null($lang) and array_key_exists($lang, $this->data)) {
$paths[$lang] = !empty($key) ? $lang . ':' . $key : $lang;
}
else {
foreach ($this->languages as $lang) {
$paths[$lang] = !empty($key) ? $lang . ':' . $key : $lang;
}
}
}
else {
if (!empty($key)) {
$paths[$key] = $key;
}
}
// Get the raw data
$return_data = $this->data;
// Simplest case, return all data.
if (empty($paths)) {
return $return_data;
}
// Second simplest case, one specific value
if (!empty($paths[$key])) {
$location = explode(':', $paths[$key]);
foreach ($location as $l) {
if (isset($return_data[$l])) {
$return_data = $return_data[$l];
}
else {
$return_data = FALSE;
}
}
return $return_data;
}
// Third case, one specific value in a given language
if (!empty($paths[$lang]) and count($paths) == 1) {
$location = explode(':', $path[$lang]);
foreach ($location as $l) {
if (isset($return_data[$l])) {
$return_data = $return_data[$l];
}
else {
$return_data = FALSE;
}
}
// Language specific data is always keyed by the language
$return_data[$lang] = $return_data;
return $return_data;
}
// Lastly, the complicated case. Keyed value for all languages
if ($this->languages and count($paths) > 1) {
$keyed_data = array();
foreach ($paths as $p => $path) {
// Reset return data
$return_data = $this->data;
$location = explode(':', $path);
foreach ($location as $l) {
if (isset($return_data[$l])) {
$return_data = $return_data[$l];
}
else {
$return_data = FALSE;
}
}
$keyed_data[$p] = $return_data;
}
// Finally, put the keyed data back into the return data.
return $keyed_data;
}
}
return $return_data;
}
/**
* Add data to an empty object or replace all existing data
*
* @param mixed $data
* A set of data to parse.
* @param string $language [optional]
* Language key for the data being added
*
* In some cases, it may require multiple web service requests
* to load language specific content. You can add each
* request data result to the same processor object. getData()
* should then return the merged data keyed by language.
*
* If your webservice returns all data for all languages in
* a single request, leave $lang to NULL (not LANGUAGE_NONE).
* LANGUAGE_NONE is considered a valid language and triggers
* the language keying.
*/
public function addData($data, $lang = NULL) {
if (!is_null($lang) and !empty($data)) {
$this->languages[$lang] = $lang;
$this->data[$lang] = $this->parse($data);
}
else {
// Default action, just parse the data
$this->data = $this->parse($data);
}
}
// Returns an array of the content type of the data this processor accepts
abstract public function accepts();
// Parse the web service response string into a structured array and return the array
abstract protected function parse($data);
}
/**
* Class definition for Web Service Connector
*/
abstract class WsConnector {
protected $expires;
protected $cacheDefaultTime;
protected $cacheDefaultOverride;
protected $staleCache;
protected $endpoint;
protected $error;
// All connectors support the default plugin by default.
// The plugin essentially means all the language data is
// in the single request.
protected $languagePlugins = array('default');
public function getEndpoint() {
return $this->endpoint;
}
public function __construct($endpoint) {
$this->endpoint = trim($endpoint);
$this->expires = 0;
$this->cacheDefaultTime = 0;
$this->cacheDefaultOverride = FALSE;
$this->staleCache = FALSE;
}
public function supportsCaching() {
return FALSE;
}
public function getError() {
return $this->error;
}
/**
* Return the list of supported language handling plugins
*/
public function getSupportedLanguagePlugins() {
return $this->languagePlugins;
}
public function defaultCache($mintime = 0, $override = FALSE, $stale = FALSE) {
$this->cacheDefaultTime = $mintime;
$this->cacheDefaultOverride = $override;
$this->staleCache = $stale;
}
abstract function getMethods();
abstract public function wscall($type, $method, $argument, $options);
public function create($method, $object, $options = array()) {
$this->expires = 0;
return $this->wscall('create', $method, $object, $options);
}
public function read($method, $id, $options = array()) {
$this->expires = 0;
return $this->wscall('read', $method, $id, $options);
}
public function update($id, $method, $object, $options = array()) {
$this->expires = 0;
return $this->wscall('update', $method, array( $id, $object), $options);
}
public function delete($id, $method, $options = array()) {
$this->expires = 0;
return $this->wscall('delete', $method, $id, $options);
}
public function index($method, $options = array()) {
$this->expires = 0;
return $this->wscall('index', $method, array(), $options);
}
public function expires() {
if ($this->expires > 0) {
return $this->expires;
}
else {
return FALSE;
}
}
public function isDegraded() {
return FALSE;
}
protected function setError($code, $message) {
$this->error = array(
'code' => $code,
'message' => $message,
);
}
protected function clearError() {
$this->error = NULL;
}
}