|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\rest; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Controller; |
|
6
|
|
|
use Ubiquity\orm\DAO; |
|
7
|
|
|
use Ubiquity\controllers\Startup; |
|
8
|
|
|
use Ubiquity\utils\base\UString; |
|
9
|
|
|
use Ubiquity\cache\CacheManager; |
|
10
|
|
|
use Ubiquity\utils\http\URequest; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author jc |
|
14
|
|
|
* Abstract base class for Rest controllers |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class RestController extends Controller { |
|
18
|
|
|
protected $config; |
|
19
|
|
|
protected $model; |
|
20
|
|
|
protected $contentType; |
|
21
|
|
|
protected $restCache; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ResponseFormatter |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $responseFormatter; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var RestServer |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $server; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(){ |
|
33
|
|
|
if(!\headers_sent()){ |
|
34
|
|
|
@\set_exception_handler(array ($this,'_errorHandler' )); |
|
35
|
|
|
$this->config=Startup::getConfig(); |
|
36
|
|
|
$this->server=new RestServer($this->config); |
|
37
|
|
|
$this->server->cors(); |
|
38
|
|
|
$this->responseFormatter=new ResponseFormatter(); |
|
39
|
|
|
$this->contentType="application/json"; |
|
40
|
|
|
$this->server->_setContentType($this->contentType); |
|
41
|
|
|
$this->restCache=CacheManager::getRestCacheController(\get_class($this)); |
|
42
|
|
|
} |
|
43
|
|
|
if (! $this->isValid (Startup::getAction())) |
|
44
|
|
|
$this->onInvalidControl (); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function isValid($action){ |
|
48
|
|
|
if(isset($this->restCache["authorizations"])){ |
|
49
|
|
|
if(\array_search($action, $this->restCache["authorizations"])!==false){ |
|
50
|
|
|
return $this->server->isValid(); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function onInvalidControl(){ |
|
57
|
|
|
throw new \Exception('HTTP/1.1 401 Unauthorized, you need an access token for this request',401); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Realise the connection to the server |
|
62
|
|
|
* To override in derived classes to define your own authentication |
|
63
|
|
|
*/ |
|
64
|
|
|
public function connect(){ |
|
65
|
|
|
$this->server->connect($this); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function initialize(){ |
|
69
|
|
|
$thisClass=\get_class($this); |
|
70
|
|
|
if(!isset($this->model)) |
|
71
|
|
|
$this->model=CacheManager::getRestResource($thisClass); |
|
72
|
|
|
if(!isset($this->model)){ |
|
73
|
|
|
$modelsNS=$this->config["mvcNS"]["models"]; |
|
74
|
|
|
$this->model=$modelsNS."\\".$this->responseFormatter->getModel($thisClass); |
|
75
|
|
|
} |
|
76
|
|
|
$this->connectDb($this->config); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function finalize(){ |
|
80
|
|
|
parent::finalize(); |
|
81
|
|
|
$this->server->finalizeTokens(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
public function _errorHandler($e){ |
|
87
|
|
|
$code=500; |
|
88
|
|
|
if($e->getCode()!==0) |
|
89
|
|
|
$code=$e->getCode(); |
|
90
|
|
|
$this->_setResponseCode($code); |
|
91
|
|
|
echo $this->responseFormatter->formatException($e); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function _setResponseCode($value){ |
|
95
|
|
|
\http_response_code($value); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function connectDb($config){ |
|
99
|
|
|
$db=$config["database"]; |
|
100
|
|
|
if($db["dbName"]!==""){ |
|
101
|
|
|
DAO::connect($db["type"],$db["dbName"],@$db["serverName"],@$db["port"],@$db["user"],@$db["password"],@$db["options"],@$db["cache"]); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Updates $instance with $values |
|
107
|
|
|
* To eventually be redefined in derived classes |
|
108
|
|
|
* @param object $instance the instance to update |
|
109
|
|
|
* @param array|null $values |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function _setValuesToObject($instance,$values=null){ |
|
112
|
|
|
if(URequest::isJSON()){ |
|
113
|
|
|
$values=\json_decode($values,true); |
|
114
|
|
|
} |
|
115
|
|
|
URequest::setValuesToObject($instance,$values); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns all objects for the resource $model |
|
120
|
|
|
* @route("cache"=>false) |
|
121
|
|
|
*/ |
|
122
|
|
|
public function index() { |
|
123
|
|
|
$datas=DAO::getAll($this->model); |
|
124
|
|
|
echo $this->responseFormatter->get($datas); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Default route for requiring a single object |
|
129
|
|
|
* @route("{id}","methods"=>["get","options"]) |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getById($id){ |
|
132
|
|
|
return $this->getOne($id,true,true); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns a list of objects from the server |
|
137
|
|
|
* @param string $condition the sql Where part |
|
138
|
|
|
* @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands |
|
139
|
|
|
* @param boolean $useCache |
|
140
|
|
|
*/ |
|
141
|
|
|
public function get($condition="1=1",$included=false,$useCache=false){ |
|
142
|
|
|
try{ |
|
143
|
|
|
$condition=\urldecode($condition); |
|
144
|
|
|
$included=$this->getIncluded($included); |
|
145
|
|
|
$useCache=UString::isBooleanTrue($useCache); |
|
146
|
|
|
$datas=DAO::getAll($this->model,$condition,$included,$useCache); |
|
|
|
|
|
|
147
|
|
|
echo $this->responseFormatter->get($datas); |
|
148
|
|
|
}catch (\Exception $e){ |
|
149
|
|
|
$this->_setResponseCode(500); |
|
150
|
|
|
echo $this->responseFormatter->formatException($e); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get the first object corresponding to the $keyValues |
|
156
|
|
|
* @param string $keyValues primary key(s) value(s) or condition |
|
157
|
|
|
* @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands |
|
158
|
|
|
* @param boolean $useCache if true then response is cached |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getOne($keyValues,$included=false,$useCache=false){ |
|
161
|
|
|
$keyValues=\urldecode($keyValues); |
|
162
|
|
|
$included=$this->getIncluded($included); |
|
163
|
|
|
$useCache=UString::isBooleanTrue($useCache); |
|
164
|
|
|
$data=DAO::getOne($this->model, $keyValues,$included,$useCache); |
|
|
|
|
|
|
165
|
|
|
if(isset($data)){ |
|
166
|
|
|
$_SESSION["_restInstance"]=$data; |
|
167
|
|
|
echo $this->responseFormatter->getOne($data); |
|
168
|
|
|
} |
|
169
|
|
|
else{ |
|
170
|
|
|
$this->_setResponseCode(404); |
|
171
|
|
|
echo $this->responseFormatter->format(["message"=>"No result found","keyValues"=>$keyValues]); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
private function getIncluded($included){ |
|
176
|
|
|
if(!UString::isBoolean($included)){ |
|
177
|
|
|
return explode(",", $included); |
|
178
|
|
|
} |
|
179
|
|
|
return UString::isBooleanTrue($included); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function _format($arrayMessage){ |
|
183
|
|
|
return $this->responseFormatter->format($arrayMessage); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param string $member |
|
188
|
|
|
* @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands |
|
189
|
|
|
* @param boolean $useCache |
|
190
|
|
|
* @throws \Exception |
|
191
|
|
|
*/ |
|
192
|
|
View Code Duplication |
public function getOneToMany($member,$included=false,$useCache=false){ |
|
|
|
|
|
|
193
|
|
|
if(isset($_SESSION["_restInstance"])){ |
|
194
|
|
|
$included=$this->getIncluded($included); |
|
195
|
|
|
$useCache=UString::isBooleanTrue($useCache); |
|
196
|
|
|
$datas=DAO::getOneToMany($_SESSION["_restInstance"], $member,$included,$useCache); |
|
197
|
|
|
echo $this->responseFormatter->get($datas); |
|
198
|
|
|
}else{ |
|
199
|
|
|
throw new \Exception("You have to call getOne before calling getOneToMany."); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param string $member |
|
205
|
|
|
* @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands |
|
206
|
|
|
* @param boolean $useCache |
|
207
|
|
|
* @throws \Exception |
|
208
|
|
|
*/ |
|
209
|
|
View Code Duplication |
public function getManyToMany($member,$included=false,$useCache=false){ |
|
|
|
|
|
|
210
|
|
|
if(isset($_SESSION["_restInstance"])){ |
|
211
|
|
|
$included=$this->getIncluded($included); |
|
212
|
|
|
$useCache=UString::isBooleanTrue($useCache); |
|
213
|
|
|
$datas=DAO::getManyToMany($_SESSION["_restInstance"], $member,$included,null,$useCache); |
|
214
|
|
|
echo $this->responseFormatter->get($datas); |
|
215
|
|
|
}else{ |
|
216
|
|
|
throw new \Exception("You have to call getOne before calling getManyToMany."); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Update an instance of $model selected by the primary key $keyValues |
|
222
|
|
|
* Require members values in $_POST array |
|
223
|
|
|
* @param array $keyValues |
|
224
|
|
|
* @authorization |
|
225
|
|
|
*/ |
|
226
|
|
View Code Duplication |
public function update(...$keyValues){ |
|
|
|
|
|
|
227
|
|
|
$instance=DAO::getOne($this->model, $keyValues); |
|
228
|
|
|
if(isset($instance)){ |
|
229
|
|
|
$this->_setValuesToObject($instance,URequest::getInput()); |
|
230
|
|
|
$result=DAO::update($instance); |
|
231
|
|
|
if($result){ |
|
232
|
|
|
echo $this->responseFormatter->format(["status"=>"updated","data"=>$this->responseFormatter->cleanRestObject($instance)]); |
|
233
|
|
|
}else{ |
|
234
|
|
|
throw new \Exception("Unable to update the instance"); |
|
235
|
|
|
} |
|
236
|
|
|
}else{ |
|
237
|
|
|
$this->_setResponseCode(404); |
|
238
|
|
|
echo $this->responseFormatter->format(["message"=>"No result found","keyValues"=>$keyValues]); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Insert a new instance of $model |
|
244
|
|
|
* Require members values in $_POST array |
|
245
|
|
|
* @authorization |
|
246
|
|
|
*/ |
|
247
|
|
|
public function add(){ |
|
248
|
|
|
$model=$this->model; |
|
249
|
|
|
$instance=new $model(); |
|
250
|
|
|
if(isset($instance)){ |
|
251
|
|
|
$this->_setValuesToObject($instance,URequest::getInput()); |
|
252
|
|
|
$result=DAO::insert($instance); |
|
253
|
|
|
if($result){ |
|
254
|
|
|
echo $this->responseFormatter->format(["status"=>"inserted","data"=>$this->responseFormatter->cleanRestObject($instance)]); |
|
255
|
|
|
}else{ |
|
256
|
|
|
throw new \Exception("Unable to insert the instance"); |
|
257
|
|
|
} |
|
258
|
|
|
}else{ |
|
259
|
|
|
$this->_setResponseCode(500); |
|
260
|
|
|
echo $this->responseFormatter->format(["message"=>"Unable to create ".$model." instance"]); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Delete the instance of $model selected by the primary key $keyValues |
|
266
|
|
|
* Requires an authorization with access token |
|
267
|
|
|
* @param array $keyValues |
|
268
|
|
|
* @route("methods"=>["delete"]) |
|
269
|
|
|
* @authorization |
|
270
|
|
|
*/ |
|
271
|
|
View Code Duplication |
public function delete(...$keyValues){ |
|
|
|
|
|
|
272
|
|
|
$instance=DAO::getOne($this->model, $keyValues); |
|
273
|
|
|
if(isset($instance)){ |
|
274
|
|
|
$result=DAO::remove($instance); |
|
275
|
|
|
if($result){ |
|
276
|
|
|
echo $this->responseFormatter->format(["status"=>"deleted","data"=>$this->responseFormatter->cleanRestObject($instance)]); |
|
277
|
|
|
}else{ |
|
278
|
|
|
throw new \Exception("Unable to delete the instance"); |
|
279
|
|
|
} |
|
280
|
|
|
}else{ |
|
281
|
|
|
$this->_setResponseCode(404); |
|
282
|
|
|
echo $this->responseFormatter->format(["message"=>"No result found","keyValues"=>$keyValues]); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: