This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
services_documentation.module
276 lines (252 loc) · 8.97 KB
/
services_documentation.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
<?php
/**
* @file
* Provides documentation pages for Services resources.
*/
/**
* Implements hook_menu().
*/
function services_documentation_menu() {
$base_path = variable_get('services_documentation_base_path', 'developers');
// API versions overview.
$endpoints = services_endpoint_load_all();
$items[$base_path] = array(
'title' => 'Services Documentation: API Versions',
'page callback' => 'services_documentation_versions_overview_page',
'page arguments' => array($endpoints),
'access arguments' => array('view services documentation'),
'file' => 'services_documentation.pages.inc',
);
// Build menu items for each API version.
foreach ($endpoints as $endpoint_id => $endpoint) {
$endpoint_path = $base_path . '/' . $endpoint_id;
$items[$endpoint_path] = array(
'title' => 'Version ' . $endpoint->name,
'page callback' => 'services_documentation_version_page',
'access arguments' => array('view services documentation'),
'page arguments' => array($endpoint),
'file' => 'services_documentation.pages.inc',
);
}
$items['admin/structure/services/list/%services_endpoint/resources/%/%/%'] = array(
'title' => 'Services Documentation',
'title callback' => 'services_documentation_op_docs_form_title',
'title arguments' => array($endpoint->name, 6, 7, 8),
'page callback' => 'drupal_get_form',
'page arguments' => array('services_documentation_op_docs_form', 4, 6, 7, 8),
'access arguments' => array('edit services documentation'),
'file' => 'services_documentation.admin.inc',
);
// Provide an administration page for configuring homepage content.
$items['admin/config/services/documentation'] = array(
'title' => 'Services Documentation',
'page callback' => 'drupal_get_form',
'page arguments' => array('services_documentation_configure'),
'access arguments' => array('administer site configuration'),
'file' => 'services_documentation.admin.inc',
'description' => "Configure API documentation.",
);
return $items;
}
/**
* Implements hook_permission().
*/
function services_documentation_permission() {
$permissions = array();
$permissions['view services documentation'] = array(
'title' => t('view services documentation'),
'description' => t('View services documentation.'),
);
module_load_include('inc', 'services_documentation');
// Generate a permission per enabled method, per resource, per endpoint.
$endpoints = services_endpoint_load_all();
foreach ($endpoints as $endpoint_id => $endpoint) {
$resources = services_documentation_build_documentation($endpoint);
// Cycle through resources. E.g., users, taxonomy_term, etc.
foreach ($resources as $resource_name => $resource) {
// Cycle through operation bundles. E.g., targeted_actions, actions, etc.
foreach ($resource as $method_bundle_type => $method_bundle) {
// Cycle through each operation in bundle. E.g., create, retrieve, etc.
foreach ($method_bundle as $method_name => $method) {
$permission_name = 'view ' . $endpoint_id . ' ' . $resource_name . ' ' . $method_name . ' documentation';
$method_title = $endpoint_id . ' > ' . $resource_name . ' > ' . $method_name;
$vars = array(
'@endpoint_id' => $endpoint_id,
'@resource_name' => $resource_name,
'@method_name' => $method_name,
'@method_title' => $method_title,
);
$permissions[$permission_name] = array(
'title' => t('View services documentation for @method_title', $vars),
'description' => t('View services documentation for the @method_name method of the @resource_name resource at the @endpoint_id endpoint.', $vars),
);
}
}
}
}
return $permissions;
}
/**
* Implements hook_theme().
*/
function services_documentation_theme($existing, $type, $theme, $path) {
$theme_path = drupal_get_path('module', 'services_documentation') . '/theme';
return array(
'services_documentation_versions_overview' => array(
'path' => $theme_path,
'template' => 'services-documentation-versions-overview',
'variables' => array('versions' => NULL),
),
'services_documentation_version' => array(
'path' => $theme_path,
'template' => 'services-documentation-version',
'variables' => array(
'table_of_contents' => NULL,
'description' => NULL,
'resources' => NULL,
),
),
'services_documentation_resource' => array(
'path' => $theme_path,
'template' => 'services-documentation-resource',
'variables' => array(
'name' => NULL,
'description' => NULL,
'limit' => NULL,
'method_bundles' => NULL,
),
),
'services_documentation_method_bundle' => array(
'path' => $theme_path,
'template' => 'services-documentation-method-bundle',
'variables' => array(
'name' => NULL,
'methods' => NULL,
),
),
'services_documentation_method' => array(
'path' => $theme_path,
'template' => 'services-documentation-method',
'variables' => array(
'anchor' => NULL,
'name' => NULL,
'path' => NULL,
'weight' => NULL,
'verb' => NULL,
'description' => NULL,
'parameters' => NULL,
'request_url' => NULL,
'request_data' => NULL,
'response' => NULL,
'errors' => NULL,
'example_implementations_bundles' => NULL,
'method' => NULL,
'auth' => FALSE,
),
),
'services_documentation_method_error' => array(
'path' => $theme_path,
'template' => 'services-documentation-method-error',
'variables' => array(
'question' => NULL,
'description' => NULL,
'response' => NULL,
'anchor' => NULL,
),
),
'services_documentation_implementation_bundle' => array(
'path' => $theme_path,
'template' => 'services-documentation-implementation-bundle',
'variables' => array(
'language' => NULL,
'examples' => NULL,
),
),
'services_documentation_implementation' => array(
'path' => $theme_path,
'template' => 'services-documentation-implementation',
'variables' => array(
'name' => NULL,
'description' => NULL,
'location' => NULL,
'files' => NULL,
'download_link' => NULL,
'uses_sdk' => NULL,
),
),
'services_documentation_implementation_file' => array(
'path' => $theme_path,
'template' => 'services-documentation-implementation-file',
'variables' => array(
'name' => NULL,
'path' => NULL,
'type' => NULL,
'contents' => NULL,
'children' => NULL,
),
),
);
}
/**
* Preprocesses variables for services_documentation_method_bundle template.
*/
function template_preprocess_services_documentation_method_bundle(&$vars) {
// Implement admin setting for hiding method bundle title.
if (!variable_get('services_documentation_display_bundle_title', FALSE)) {
unset($vars['name']);
}
}
/**
* Implements hook_element_info().
*/
function services_documentation_element_info() {
$types['services_documentation_method'] = array(
'#input' => FALSE,
'#pre_render' => array('services_documentation_format_method'),
'#theme' => array('services_documentation_method'),
);
return $types;
}
/**
* Formats documented Services methods.
*/
function services_documentation_format_method($element) {
$element['url'] = array();
// Create HTTP method key.
foreach ($element['#method']['args'] as $key => $argument) {
if (!empty($argument['source'])) {
if (is_array($argument['source'])) {
switch (key($argument['source'])) {
case 'param':
$element['#method']['args'][$key]['http_method'] = t('GET');
break;
case 'path':
$element['#method']['args'][$key]['http_method'] = t('URL');
$element['url'][$argument['source']['path']] = '[' . $argument['name'] . ']';
break;
}
}
else {
$element['#method']['args'][$key]['http_method'] = t('POST');
}
}
else {
unset($element['#method']['args'][$key]);
}
}
$element['url'] = $element['#name'] . '/' . implode('/', $element['url']);
if (!empty($element['#request_url']) && valid_url($element['#request_url'])) {
$url = drupal_parse_url($element['#request_url']);
$options = array('query' => $url['query'], 'html' => TRUE);
$element['#request_url'] = l($element['#request_url'], $url['path'], $options);
}
return $element;
}
/**
* Implements hook_controller_settings_alter().
*/
function services_documentation_controller_settings_alter(&$controller_info, $op_info) {
$controller_info['services_documentation'] = array(
'#markup' => l(t('edit documentation'), "admin/structure/services/list/{$op_info['endpoint']->name}/resources/{$op_info['resource']['key']}/{$op_info['class']}/{$op_info['op']['name']}"),
);
}