1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\rest; |
4
|
|
|
|
5
|
|
|
use Ubiquity\orm\DAO; |
6
|
|
|
use Ubiquity\utils\base\UString; |
7
|
|
|
use Ubiquity\utils\http\URequest; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* @author jc |
12
|
|
|
* @property ResponseFormatter $responseFormatter |
13
|
|
|
*/ |
14
|
|
|
trait RestControllerUtilitiesTrait { |
15
|
|
|
|
16
|
|
|
abstract public function _setResponseCode($value); |
17
|
|
|
|
18
|
|
|
protected function operate_($instance, $callback, $status, $exceptionMessage, $keyValues) { |
19
|
|
|
if (isset ( $instance )) { |
20
|
|
|
$result = $callback ( $instance ); |
21
|
|
|
if ($result) { |
22
|
|
|
$formatter = $this->_getResponseFormatter (); |
23
|
|
|
echo $formatter->format ( [ "status" => $status,"data" => $formatter->cleanRestObject ( $instance ) ] ); |
24
|
|
|
} else { |
25
|
|
|
throw new \Exception ( $exceptionMessage ); |
26
|
|
|
} |
27
|
|
|
} else { |
28
|
|
|
$this->_setResponseCode ( 404 ); |
29
|
|
|
echo $this->_getResponseFormatter ()->format ( [ "message" => "No result found","keyValues" => $keyValues ] ); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
protected function _getResponseFormatter() { |
34
|
2 |
|
if (! isset ( $this->responseFormatter )) { |
35
|
|
|
$this->responseFormatter = $this->getResponseFormatter (); |
36
|
|
|
} |
37
|
2 |
|
return $this->responseFormatter; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* To override, returns the active formatter for the response |
42
|
|
|
* |
43
|
|
|
* @return \Ubiquity\controllers\rest\ResponseFormatter |
44
|
|
|
*/ |
45
|
|
|
protected function getResponseFormatter() { |
46
|
|
|
return new ResponseFormatter (); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
protected function connectDb($config) { |
50
|
1 |
|
$db = $config ["database"]; |
51
|
1 |
|
if ($db ["dbName"] !== "") { |
52
|
1 |
|
DAO::connect ( $db ["type"], $db ["dbName"], @$db ["serverName"], @$db ["port"], @$db ["user"], @$db ["password"], @$db ["options"], @$db ["cache"] ); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Updates $instance with $values |
58
|
|
|
* To eventually be redefined in derived classes |
59
|
|
|
* |
60
|
|
|
* @param object $instance |
61
|
|
|
* the instance to update |
62
|
|
|
* @param array|null $values |
63
|
|
|
*/ |
64
|
|
|
protected function _setValuesToObject($instance, $values = null) { |
65
|
|
|
if (URequest::isJSON ()) { |
66
|
|
|
$values = \json_decode ( $values, true ); |
67
|
|
|
} |
68
|
|
|
URequest::setValuesToObject ( $instance, $values ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @param string|boolean $included |
74
|
|
|
* @return array|boolean |
75
|
|
|
*/ |
76
|
1 |
|
private function getIncluded($included) { |
77
|
1 |
|
if (! UString::isBooleanStr ( $included )) { |
78
|
|
|
return explode ( ",", $included ); |
79
|
|
|
} |
80
|
1 |
|
return UString::isBooleanTrue ( $included ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* |
85
|
|
|
* @param callable $getDatas |
86
|
|
|
* @param string $member |
87
|
|
|
* @param boolean|string $included |
88
|
|
|
* if true, loads associate members with associations, if string, example : client.*,commands |
89
|
|
|
* @param boolean $useCache |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
|
|
protected function getMany_($getDatas, $member, $included = false, $useCache = false) { |
93
|
|
|
if (isset ( $_SESSION ["_restInstance"] )) { |
94
|
|
|
$included = $this->getIncluded ( $included ); |
95
|
|
|
$useCache = UString::isBooleanTrue ( $useCache ); |
96
|
|
|
$datas = $getDatas ( $_SESSION ["_restInstance"], $member, $included, $useCache ); |
97
|
|
|
echo $this->_getResponseFormatter ()->get ( $datas ); |
98
|
|
|
} else { |
99
|
|
|
throw new \Exception ( "You have to call getOne before calling getManyToMany or getOneToMany." ); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|