1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Contains functions necessary for building the resource definitions. This |
---|
6 | * is only needed the first time a the resources for a endpoint are fetched, |
---|
7 | * or when the cache has been cleared. |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Builds the resource definition array for a endpoint. |
---|
12 | * |
---|
13 | * @param string $endpoint_name |
---|
14 | * Optional. The endpoint name. |
---|
15 | * @return array |
---|
16 | * The resource definitions. |
---|
17 | */ |
---|
18 | function _services_build_resources($endpoint_name = '') { |
---|
19 | module_load_include('resource_runtime.inc', 'services'); |
---|
20 | |
---|
21 | // Get all installed resources |
---|
22 | $resources = module_invoke_all('services_resources'); |
---|
23 | |
---|
24 | // Load the endpoint |
---|
25 | $endpoint = NULL; |
---|
26 | if (!empty($endpoint_name)) { |
---|
27 | $endpoint = services_endpoint_load($endpoint_name); |
---|
28 | // Apply the endpoint on the services |
---|
29 | _services_apply_endpoint($resources, $endpoint, TRUE); |
---|
30 | } |
---|
31 | |
---|
32 | drupal_alter('services_resources', $resources, $endpoint); |
---|
33 | |
---|
34 | // Process the resources, and collect all controllers in the process |
---|
35 | $controllers = array(); |
---|
36 | foreach ($resources as $name => &$resource) { |
---|
37 | _services_process_resource($name, $resource, $controllers); |
---|
38 | } |
---|
39 | |
---|
40 | // Make sure that we got a access callback for all resources |
---|
41 | foreach ($controllers as &$controller) { |
---|
42 | if (!empty($controller['file'])) { |
---|
43 | // Convert old-style file references to something that fits module_load_include() better. |
---|
44 | if (!empty($controller['file']['file']) && empty($controller['file']['type'])) { |
---|
45 | $controller['file']['type'] = $controller['file']['file']; |
---|
46 | } |
---|
47 | } |
---|
48 | if (!isset($controller['access callback'])) { |
---|
49 | $controller['access callback'] = 'user_access'; |
---|
50 | } |
---|
51 | } |
---|
52 | drupal_alter('services_resources_controller_post_processing', $controllers, $endpoint); |
---|
53 | |
---|
54 | // This hook is deprecated and will be removed in next versions of services. |
---|
55 | // Use hook_services_resources_alter instead. |
---|
56 | drupal_alter('services_resources_post_processing', $resources, $endpoint); |
---|
57 | |
---|
58 | // Do some endpoint-dependent processing |
---|
59 | if ($endpoint) { |
---|
60 | // Let the authentication modules alter our controllers |
---|
61 | foreach ($endpoint->authentication as $auth_module => $auth_settings) { |
---|
62 | services_auth_invoke($auth_module, 'alter_controllers', $auth_settings, $controllers, $endpoint); |
---|
63 | } |
---|
64 | |
---|
65 | // Apply any aliases from endpoint |
---|
66 | $aliased = array(); |
---|
67 | foreach ($resources as $key => $def) { |
---|
68 | if (!empty($def['endpoint']['alias'])) { |
---|
69 | $aliased[$def['endpoint']['alias']] = $def; |
---|
70 | } |
---|
71 | else { |
---|
72 | $aliased[$key] = $def; |
---|
73 | } |
---|
74 | } |
---|
75 | $resources = $aliased; |
---|
76 | } |
---|
77 | |
---|
78 | return $resources; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Applies the endpoint to a set of resources. Resources and controllers that |
---|
83 | * aren't supported will be removed (if $strict is set to TRUE) and both |
---|
84 | * resources and controllers will get the 'endpoint' attribute set. |
---|
85 | * |
---|
86 | * @param array $resources |
---|
87 | * An array of resources that the endpoint should be applied on. |
---|
88 | * @param array $endpoint |
---|
89 | * A endpoint information array. |
---|
90 | * @param bool $strict |
---|
91 | * Optional. |
---|
92 | * @return void |
---|
93 | */ |
---|
94 | function _services_apply_endpoint(&$resources, $endpoint, $strict = TRUE) { |
---|
95 | foreach ($resources as $name => &$resource) { |
---|
96 | $cres = ($endpoint && isset($endpoint->resources[$name])) ? $endpoint->resources[$name] : array(); |
---|
97 | $resource['endpoint'] = $cres; |
---|
98 | |
---|
99 | if ($strict && empty($cres)) { |
---|
100 | unset($resources[$name]); |
---|
101 | } |
---|
102 | else { |
---|
103 | $crud = array('create', 'retrieve', 'update', 'delete', 'index'); |
---|
104 | foreach ($crud as $op) { |
---|
105 | if (isset($resource[$op])) { |
---|
106 | $cop = isset($cres['operations'][$op]) ? $cres['operations'][$op] : array(); |
---|
107 | $resource[$op]['endpoint'] = $cop; |
---|
108 | if ($strict && (empty($cop) || !$cop['enabled'])) { |
---|
109 | unset($resource[$op]); |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | $classes = array('targeted_actions', 'actions', 'relationships'); |
---|
115 | foreach ($classes as $class) { |
---|
116 | if (!empty($resource[$class])) { |
---|
117 | foreach ($resource[$class] as $op => $def) { |
---|
118 | $cop = isset($cres[$class][$op]) ? $cres[$class][$op] : array(); |
---|
119 | if (empty($cop) || !$cop['enabled']) { |
---|
120 | if ($strict) { |
---|
121 | unset($resource[$class][$op]); |
---|
122 | } |
---|
123 | } |
---|
124 | else { |
---|
125 | $resource[$class][$op]['endpoint'] = $cop; |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Process resource runs through all the controllers of a resource and applies |
---|
136 | * some inheritance logic and adds the to the $controllers array. |
---|
137 | * |
---|
138 | * @param string $name |
---|
139 | * The name of the resource |
---|
140 | * @param array &$resource |
---|
141 | * The resource definition |
---|
142 | * @param array &$controllers |
---|
143 | * An that will be fillew with all the controllers for the resource. |
---|
144 | * @return void |
---|
145 | */ |
---|
146 | function _services_process_resource($name, &$resource, &$controllers) { |
---|
147 | $resource['name'] = $name; |
---|
148 | |
---|
149 | // CRUD operations. |
---|
150 | foreach (array('retrieve', 'create', 'update', 'delete', 'index') as $op) { |
---|
151 | if (isset($resource[$op])) { |
---|
152 | $controllers[$name . '/' . $op] = &$resource[$op]; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | // Actions and relationships. |
---|
157 | foreach (array('relationships', 'actions', 'targeted_actions') as $class) { |
---|
158 | if (!isset($resource[$class])) { |
---|
159 | continue; |
---|
160 | } |
---|
161 | |
---|
162 | foreach (array_keys($resource[$class]) as $action_name) { |
---|
163 | $class_singular = trim($class, 's'); |
---|
164 | $controllers[$name . '/' . $class_singular . '/' . $action_name] = &$resource[$class][$action_name]; |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Supplies the resource definitions for Drupal core data |
---|
171 | * |
---|
172 | * @return array |
---|
173 | */ |
---|
174 | function _services_core_resources() { |
---|
175 | require_once("resources/comment_resource.inc"); |
---|
176 | require_once("resources/file_resource.inc"); |
---|
177 | require_once("resources/node_resource.inc"); |
---|
178 | require_once("resources/system_resource.inc"); |
---|
179 | require_once("resources/taxonomy_resource.inc"); |
---|
180 | require_once("resources/user_resource.inc"); |
---|
181 | //require_once("resources/user_picture_resource.inc"); |
---|
182 | |
---|
183 | $resources = array(); |
---|
184 | |
---|
185 | $resources += _comment_resource_definition(); |
---|
186 | $resources += _file_resource_definition(); |
---|
187 | $resources += _node_resource_definition(); |
---|
188 | $resources += _system_resource_definition(); |
---|
189 | $resources += _taxonomy_resource_definition(); |
---|
190 | $resources += _user_resource_definition(); |
---|
191 | //$resources += _user_picture_resource_definition(); |
---|
192 | |
---|
193 | return $resources; |
---|
194 | } |
---|