-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.cb_resource_provider.php
97 lines (90 loc) · 4.18 KB
/
class.cb_resource_provider.php
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
<?php
/* This file is part of cbapi.
* Copyright © 2011-2012 stiftung kulturserver.de ggmbh <[email protected]>
*
* cbapi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cbapi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cbapi. If not, see <http://www.gnu.org/licenses/>.
*/
Cb::import('CbAbstractProvider', 'CbResourceHandlerInterface', 'CbResourceHandler', 'CbContentAdapter');
/**
* Resource providers handle AJAX requests (or any other requests) by clients,
* execute the necessary actions and output the results in the required format.
* You should register handlers for specific resources. You can also specify an
* authorization provider which checks if the specified method is allowed on the
* selected resource. If no authorization provider is given all actions are
* allowed.
*
* While CbRpcProvider ist descigned to provide an RPC-like interface this is
* designed to provide a standard REST interface. Depending on what you want to
* do you should choose one or the other.
*/
class CbResourceProvider extends CbAbstractProvider {
protected $default_resource; ///< Resource to be used if none is specified.
/**
* Create a content provider.
* @param array $handlers Handlers for various methods.
* @param array $config Application Configuration.
* @param @deprecated CbAuthorizationProvider $auth_provider Authorization Provider to be used.
* @param @deprecated string $default_resource Default resource to be used if none is specified.
* @param @deprecated CbContentFormatter $formatter Content formatter for the output.
* @param @deprecated number $deprecated Ignored.
*/
public function __construct(array $handlers = array(), $config = null,
$auth_provider = null, string $default_resource = null,
CbContentFormatter $formatter = null, $deprecated = null) {
if (is_array($config)) {
$config = array_merge(array(
'default_handler' => $config['default_handler'] ? null : 'CbResourceHandler',
'auth_provider' => null,
'formatter' => null
), $config);
} else {
$config = array(
'default_handler' => $config ? $config : 'CbResourceHandler',
'auth_provider' => $auth_provider,
'formatter' => $formatter
);
}
parent::__construct($handlers, $config);
$this->default_resource = isset($config['default_resource']) ?
$config['default_resource'] : $default_resource;
}
private function resolveHandler($request) {
$resource = isset($request['resource']) ? $request['resource'] : $this->default_resource;
return $this->getHandler($resource);
}
protected function execHandler($method, $request) {
// TODO: throw a proper exception if method doesn't exist (and things like
// __call aren't implemented either).
$unsafe = in_array($method, array('delete', 'put', 'post'));
return new CbContentAdapter($this->resolveHandler($request), $method, $request, $unsafe);
}
public function handle(array $request = null) {
// Make sure the method override is lowercase so that it matches ACLs
// This cannot be done for the RPC interface as we accept arbitrary
// methods there.
if (isset($request['method'])) $request['method'] = strtolower($request['method']);
if (isset($_POST['method'])) $_POST['method'] = strtolower($_POST['method']);
parent::handle($request);
}
protected function getMetadata($method, $request)
{
$handler = $this->resolveHandler($request);
$meta = array('name' => $request['resource']);
if (method_exists($handler, 'meta')) {
return array_merge($meta, $handler->meta($method, $request));
} else {
return $meta;
}
}
}