/
rest_server.inc
126 lines (118 loc) · 3.5 KB
/
rest_server.inc
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
<?php
/**
* @file
* Autoload classes and server settings.
*/
/**
* REST server settings form. Generates the form fragment for configuring the REST server
* for an endpoint.
*
* @param array $form
* The form fragment from services that we should add our fields to.
* @param object $endpoint
* The endpoint that we're configuring the REST server for.
* @param array $settings
* The current settings.
* @return void
*/
function _rest_server_settings(&$form, $endpoint, $settings) {
$settings = rest_server_setup_settings($settings);
$form['formatters'] = array(
'#type' => 'checkboxes',
'#title' => t('Response formatters'),
'#required' => TRUE,
'#description' => t('Select the response formats you want to enable for the rest server.'),
) + _rest_server_settings_checkboxes_attributes($settings['formatters']);
$form['parsers'] = array(
'#type' => 'checkboxes',
'#title' => t('Request parsing'),
'#required' => TRUE,
'#description' => t('Select the request parser types you want to enable for the rest server.'),
) + _rest_server_settings_checkboxes_attributes($settings['parsers']);
}
/**
* Utility function that creates attributes for a checkboxes-type form
* element from a rest server settings array.
*
* @param array $settings
* @return array
*/
function _rest_server_settings_checkboxes_attributes($settings) {
$keys = array_keys($settings);
$options = array_combine($keys, $keys);
$default = array();
foreach ($settings as $key => $enabled) {
if ($enabled) {
$default[] = $key;
}
}
ksort($options);
return array(
'#options' => $options,
'#default_value' => $default,
);
}
/**
* Submit handler for the services REST server settings form.
*
* @param object $endpoint
* The endpoint that's being configured.
* @param array $values
* The partial form-state from services.
* @return array
* The settings for the REST server in this endpoint.
*/
function _rest_server_settings_submit($endpoint, $values) {
$values['formatters'] = array_map('_rest_server_settings_not_zero', $values['formatters']);
$values['parsers'] = array_map('_rest_server_settings_not_zero', $values['parsers']);
return $values;
}
/**
* Utility function intended for use with array_map to change everything that
* isn't === 0 into TRUE.
*
* @param string $value
* The value to map.
* @return bool
* FALSE if the $value is === 0 otherwise TRUE is returned.
*/
function _rest_server_settings_not_zero($value) {
return $value !== 0;
}
/**
* Actual implementation of hook_autoload_info().
*/
function _rest_server_autoload_info() {
return array(
'RESTServer' => array(
'file' => 'includes/RESTServer.inc',
),
'RESTServerViewBuiltIn' => array(
'file' => 'includes/rest_server.views.inc',
),
'RESTServerView' => array(
'file' => 'includes/rest_server.views.inc',
),
'RssFormatView' => array(
'file' => 'formats/RssFormatView.inc',
),
'ResourceFeedModel' => array(
'file' => 'includes/models.inc',
),
'ResourceFeedModelItem' => array(
'file' => 'includes/models.inc',
),
'ResourceTimeFeedModel' => array(
'file' => 'includes/models.inc',
),
'ResourceTimeFeedModelItem' => array(
'file' => 'includes/models.inc',
),
'NodeResourceFeedModel' => array(
'file' => 'includes/node_resource.models.inc',
),
'NodeResourceViewFeedModel' => array(
'file' => 'includes/node_resource.models.inc',
),
);
}