-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjson_source.php
More file actions
227 lines (210 loc) · 4.91 KB
/
Copy pathjson_source.php
File metadata and controls
227 lines (210 loc) · 4.91 KB
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
<?php
/**
* JsonSource class.
*
* The model object for each request type (create, read, update, delete)
* must have a member property that defines which api section to refer to.
* Example: plugin tasks model Task in this app would have a property
* $apiModel that would be set to Task.
*
* Put this in config/database.php
* var $json_api = array(
* 'datasource' => 'jsonSource',
* 'base_url' => 'cake json api url'
* );
* Then in any model that uses the api (or in app model if they all do) add this:
* public $useDbConfig = 'json_api';
*
*Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2010, Loadsys Consulting, Inc. (http://www.loadsys.com)
* @version $1.0$
* @modifiedby $LastChangedBy: Joey Trapp (Loadsys) $
* @lastmodified $Date: 2010-10-14$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Core', 'HttpSocket');
class JsonSource extends Datasource {
/**
* cacheSources
*
* (default value: true)
*
* @var bool
* @access public
*/
public $cacheSources = true;
/**
* _baseConfig
*
* @var mixed
* @access public
*/
public $_baseConfig = array(
'url' => ''
);
/**
* _http
*
* (default value: null)
*
* @var mixed
* @access protected
*/
protected $_http = null;
/**
* _statusInterpreter
*
* (default value: null)
*
* @var mixed
* @access protected
*/
protected $_statusInterpreter = null;
/**
* __construct function.
*
* @access public
* @param mixed $config
* @return void
*/
public function __construct($config) {
parent::__construct($config);
$this->_http =& new HttpSocket();
}
/**
* listSources function.
*
* @access public
* @param mixed $data. (default: null)
* @return void
*/
public function listSources($data = null) {
return true;
}
/**
* describe function.
*
* @access public
* @param mixed &$Model
* @return void
*/
public function describe(&$Model) {
if (property_exists($Model, 'jsonSchema')) {
$Model->_schema = $Model->jsonSchema;
} else {
$Model->_schema = array();
}
return $Model->_schema;
}
/**
* read function.
*
* @access public
* @param mixed &$Model
* @param array $query. (default: array())
* @return void
*/
public function read(&$Model, $query = array()) {
$url = $this->_setApiUrl($Model);
return $this->_makeApiCall($url, 'get');
}
/**
* create function.
*
* @access public
* @param mixed &$Model
* @param mixed $fields. (default: null)
* @param mixed $values. (default: null)
* @return void
*/
public function create(&$Model, $fields = null, $values = null) {
$url = $this->_setApiUrl($Model);
$data = array_combine($fields, $values);
return $this->_makeApiCall($url, 'post', $data);
}
/**
* update function.
*
* @access public
* @param mixed &$Model
* @param mixed $fields. (default: null)
* @param mixed $values. (default: null)
* @return void
*/
public function update(&$Model, $fields = null, $values = null) {
$url = $this->_setApiUrl($Model, $Model->id);
$data = array_combine($fields, $values);
return $this->_makeApiCall($url, 'put', $data);
}
/**
* delete function.
*
* @access public
* @param mixed &$Model
* @param mixed $id. (default: null)
* @return void
*/
public function delete(&$Model, $id = null) {
$url = $this->_setApiUrl($Model, $id);
return $this->_makeApiCall($url, 'delete');
}
/**
* calculate function.
*
* @access public
* @param mixed &$Model
* @return void
*/
public function calculate(&$Model) {
return array('count' => true);
}
/**
* Generic method for taking a url, http protocol type, and an
* array of data and making the api call.
*
* @access protected
* @param mixed $url. (default: null)
* @param mixed $type. (default: null)
* @param mixed $data. (default: null)
* @return void
*/
protected function _makeApiCall($url = null, $type = null, $data = null) {
if (!$url || !$type) {
return false;
}
$response = json_decode($this->_http->{$type}($url, $data), true);
return $response;
}
/**
* Based on the member property $apiModel in the passed in
* Model class, returns the url with the appropriate subdirectory
*
* @access protected
* @param mixed &$Model
* @return void
*/
protected function _setApiUrl(&$Model, $id = null) {
$url = $this->config['base_url'];
if (substr($url, strlen($url) - 1) != '/') {
$url .= '/';
}
if (property_exists($Model, 'apiModel')) {
$url .= Inflector::pluralize(strtolower($Model->apiModel));
}
if (property_exists($Model, 'apiAction')) {
$url .= '/'.strtolower(Inflector::underscore($Model->apiAction));
$url = str_replace('.json', '', $url);
if ($id) {
$url .= '/'.$id;
}
}
if (substr($url, strlen($url) - 5) != '.json') {
$url .= '.json';
}
return $url;
}
}
?>