forked from randallknutson/services
-
Notifications
You must be signed in to change notification settings - Fork 1
/
services.resource_build.inc
251 lines (223 loc) · 7.84 KB
/
services.resource_build.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
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
<?php
// $Id$
/**
* @file
* Contains functions necessary for building the resource definitions. This
* is only needed the first time a the resources for a endpoint are fetched,
* or when the cache has been cleared.
*/
/**
* Builds the resource definition array for a endpoint.
*
* @param string $endpoint_name
* Optional. The endpoint name.
* @return array
* The resource definitions.
*/
function _services_build_resources($endpoint_name = '') {
module_load_include('resource_runtime.inc', 'services');
// Get all installed resources
$resources = module_invoke_all('services_resources');
// Load the endpoint
$endpoint = NULL;
if (!empty($endpoint_name)) {
$endpoint = services_endpoint_load($endpoint_name);
// Apply the endpoint on the services
_services_apply_endpoint($resources, $endpoint, TRUE);
}
drupal_alter('services_resources', $resources, $endpoint);
// Process the resources, and collect all controllers in the process
$controllers = array();
foreach ($resources as $name => &$resource) {
_services_process_resource($name, $resource, $controllers);
}
// Make sure that we got a access callback for all resources
foreach ($controllers as &$controller) {
if (!empty($controller['file'])) {
// Convert old-style file references to something that fits module_load_include() better.
if (!empty($controller['file']['file']) && empty($controller['file']['type'])) {
$controller['file']['type'] = $controller['file']['file'];
}
}
if (!isset($controller['access callback'])) {
$controller['access callback'] = 'user_access';
}
}
drupal_alter('services_resources_controller_post_processing', $controllers, $endpoint);
drupal_alter('services_resources_post_processing', $resources, $endpoint);
// Do some endpoint-dependent processing
if ($endpoint) {
// Let the authentication modules alter our controllers
foreach ($endpoint->authentication as $auth_module => $auth_settings) {
services_auth_invoke($auth_module, 'alter_controllers', $auth_settings, $controllers, $endpoint);
}
// Apply any aliases from endpoint
if (!empty($endpoint)) {
$aliased = array();
}
foreach ($resources as $key => $def) {
if (!empty($def['endpoint']['alias'])) {
$aliased[$def['endpoint']['alias']] = $def;
}
else {
$aliased[$key] = $def;
}
}
$resources = $aliased;
}
return $resources;
}
/**
* Applies the endpoint to a set of resources. Resources and controllers that
* aren't supported will be removed (if $strict is set to TRUE) and both
* resources and controllers will get the 'endpoint' attribute set.
*
* @param array $resources
* An array of resources that the endpoint should be applied on.
* @param array $endpoint
* A endpoint information array.
* @param bool $strict
* Optional.
* @return void
*/
function _services_apply_endpoint(&$resources, $endpoint, $strict = TRUE) {
foreach ($resources as $name => &$resource) {
$cres = ($endpoint && isset($endpoint->resources[$name])) ? $endpoint->resources[$name] : array();
$resource['endpoint'] = $cres;
if ($strict && empty($cres)) {
unset($resources[$name]);
}
else {
$crud = array('create', 'retrieve', 'update', 'delete', 'index');
foreach ($crud as $op) {
if (isset($resource[$op])) {
$cop = isset($cres['operations'][$op]) ? $cres['operations'][$op] : array();
$resource[$op]['endpoint'] = $cop;
if ($strict && (empty($cop) || !$cop['enabled'])) {
unset($resource[$op]);
}
}
}
$classes = array('targeted actions', 'actions', 'relationships');
foreach ($classes as $class) {
if (!empty($resource[$class])) {
foreach ($resource[$class] as $op => $def) {
$cop = isset($cres[$class][$op]) ? $cres[$class][$op] : array();
if (empty($cop) || !$cop['enabled']) {
if ($strict) {
unset($resource[$class][$op]);
}
}
else {
$resource[$class][$op]['endpoint'] = $cop;
}
}
}
}
}
}
}
/**
* Process resource runs through all the controllers of a resource and applies
* some inheritance logic and adds the to the $controllers array.
*
* @param string $name
* The name of the resource
* @param array &$resource
* The resource definition
* @param array &$controllers
* An that will be fillew with all the controllers for the resource.
* @return void
*/
function _services_process_resource($name, &$resource, &$controllers) {
$resource['name'] = $name;
$keys = array('retrieve', 'create', 'update', 'delete');
foreach ($keys as $key) {
if (isset($resource[$key])) {
$controllers[$name . '/' . $key] = &$resource[$key];
}
}
if (isset($resource['index'])) {
$controllers[$name . '/index'] = &$resource['index'];
}
if (isset($resource['relationships'])) {
foreach ($resource['relationships'] as $relname => $rel) {
// Run some inheritance logic
if (isset($resource['retrieve'])) {
if (empty($rel['args']) || $rel['args'][0]['name'] !== $resource['retrieve']['args'][0]['name']) {
array_unshift($rel['args'], $resource['retrieve']['args'][0]);
}
$resource['relationships'][$relname] = array_merge($resource['retrieve'], $rel);
}
$controllers[$name . '/relationship/' . $relname] = &$resource['relationships'][$relname];
}
}
if (isset($resource['actions'])) {
foreach ($resource['actions'] as $actname => $act) {
// Run some inheritance logic
if (isset($resource['update'])) {
$up = $resource['update'];
unset($up['args']);
$resource['actions'][$actname] = array_merge($up, $act);
}
$controllers[$name . '/action/' . $actname] = &$resource['actions'][$actname];
}
}
if (isset($resource['targeted actions'])) {
foreach ($resource['targeted actions'] as $actname => $act) {
// Run some inheritance logic
if (isset($resource['update'])) {
if (empty($act['args']) || $act['args'][0]['name'] !== $resource['update']['args'][0]['name']) {
array_unshift($act['args'], $resource['update']['args'][0]);
}
$resource['targeted actions'][$actname] = array_merge($resource['update'], $act);
}
$controllers[$name . '/targeted_action/' . $actname] = &$resource['actions'][$actname];
}
}
}
/**
* Supplies the resource definitions for Drupal core data
*
* @return array
*/
function _services_core_resources() {
require_once("resources/comment_resource.inc");
require_once("resources/file_resource.inc");
require_once("resources/node_resource.inc");
require_once("resources/system_resource.inc");
require_once("resources/taxonomy_resource.inc");
require_once("resources/user_resource.inc");
$resources = array();
$resources += _comment_resource_definition();
$resources += _file_resource_definition();
$resources += _node_resource_definition();
$resources += _system_resource_definition();
$resources += _taxonomy_resource_definition();
$resources += _user_resource_definition();
return $resources;
}
/**
* Invokes all hook_service and translates them to actions on resources.
*
* @return array
*/
function _services_legacy_services_as_resources() {
$services = $methods = module_invoke_all('service');
$resources = array();
foreach ($services as $service) {
$signature = preg_split('/\./', $service['method']);
$controller = $service;
$controller['args'] = array();
if (!empty($service['args'])) {
foreach ($service['args'] as $arg) {
$arg['source'] = array(
'data' => $arg['name'],
);
$controller['args'][] = $arg;
}
}
$resources['service_' . $signature[0]]['actions'][$signature[1]] = $controller;
}
return $resources;
}