34 @\set_exception_handler(array ($this,
'_errorHandler' ));
37 $this->server->cors();
39 $this->contentType=
"application/json";
40 $this->server->_setContentType($this->contentType);
48 if(isset($this->restCache[
"authorizations"])){
49 if(\array_search($action, $this->restCache[
"authorizations"])!==
false){
50 return $this->server->isValid();
57 throw new \Exception(
'HTTP/1.1 401 Unauthorized, you need an access token for this request',401);
65 $this->server->connect($this);
69 $thisClass=\get_class($this);
70 if(!isset($this->model))
72 if(!isset($this->model)){
73 $modelsNS=$this->config[
"mvcNS"][
"models"];
74 $this->model=$modelsNS.
"\\".$this->responseFormatter->getModel($thisClass);
81 $this->server->finalizeTokens();
91 echo $this->responseFormatter->formatException($e);
95 \http_response_code($value);
100 if($db[
"dbName"]!==
""){
101 DAO::connect($db[
"type"],$db[
"dbName"],@$db[
"serverName"],@$db[
"port"],@$db[
"user"],@$db[
"password"],@$db[
"options"],@$db[
"cache"]);
113 $values=\json_decode($values,
true);
124 echo $this->responseFormatter->get($datas);
132 return $this->
getOne($id,
true,
true);
142 public function get($condition=
"1=1",$loadManyToOne=
false,$loadOneToMany=
false,$useCache=
false){
144 $condition=\urldecode($condition);
148 $datas=
DAO::getAll($this->model,$condition,$loadManyToOne,$loadOneToMany,$useCache);
149 echo $this->responseFormatter->get($datas);
150 }
catch (\Exception $e){
152 echo $this->responseFormatter->formatException($e);
163 public function getOne($keyValues,$loadManyToOne=
false,$loadOneToMany=
false,$useCache=
false){
164 $keyValues=\urldecode($keyValues);
168 $data=
DAO::getOne($this->model, $keyValues,$loadManyToOne,$loadOneToMany,$useCache);
170 $_SESSION[
"_restInstance"]=$data;
171 echo $this->responseFormatter->getOne($data);
175 echo $this->responseFormatter->format([
"message"=>
"No result found",
"keyValues"=>$keyValues]);
180 return $this->responseFormatter->format($arrayMessage);
189 if(isset($_SESSION[
"_restInstance"])){
192 echo $this->responseFormatter->get($datas);
194 throw new \Exception(
"You have to call getOne before calling getOneToMany.");
204 if(isset($_SESSION[
"_restInstance"])){
207 echo $this->responseFormatter->get($datas);
209 throw new \Exception(
"You have to call getOne before calling getManyToMany.");
221 if(isset($instance)){
225 echo $this->responseFormatter->format([
"status"=>
"updated",
"data"=>$this->responseFormatter->cleanRestObject($instance)]);
227 throw new \Exception(
"Unable to update the instance");
231 echo $this->responseFormatter->format([
"message"=>
"No result found",
"keyValues"=>$keyValues]);
243 if(isset($instance)){
247 echo $this->responseFormatter->format([
"status"=>
"inserted",
"data"=>$this->responseFormatter->cleanRestObject($instance)]);
249 throw new \Exception(
"Unable to insert the instance");
253 echo $this->responseFormatter->format([
"message"=>
"Unable to create ".
$model.
" instance"]);
264 public function delete(...$keyValues){
266 if(isset($instance)){
269 echo $this->responseFormatter->format([
"status"=>
"deleted",
"data"=>$this->responseFormatter->cleanRestObject($instance)]);
271 throw new \Exception(
"Unable to delete the instance");
275 echo $this->responseFormatter->format([
"message"=>
"No result found",
"keyValues"=>$keyValues]);
_setValuesToObject($instance, $values=null)
Updates $instance with $values To eventually be redefined in derived classes.
static getRestResource($controllerClass)
static getOne($className, $keyValues, $loadManyToOne=true, $loadOneToMany=false, $useCache=NULL)
Returns an instance of $className from the database, from $keyvalues values of the primary key...
static setValuesToObject($object, $values=null)
Affects member to member the values of the associative array $values to the members of the object $ob...
update(... $keyValues)
Update an instance of $model selected by the primary key $keyValues Require members values in $_POST ...
static connect($dbType, $dbName, $serverName="127.0.0.1", $port="3306", $user="root", $password="", $options=[], $cache=false)
Establishes the connection to the database using the past parameters.
getById($id)
Default route for requiring a single object ("{id}","methods"=>["get","options"]) ...
Base class for controllers.
static getManyToMany($instance, $member, $array=null, $useCache=NULL)
Assigns / loads the child records in the $member member of $instance.
add()
Insert a new instance of $model Require members values in $_POST array .
static getAll($className, $condition='', $loadManyToOne=true, $loadOneToMany=false, $useCache=NULL)
Returns an array of $className objects from the database.
static isJSON()
Returns true if request contentType is set to json.
static getRestCacheController($controllerClass)
static getInput()
Returns the query data, for PUT, DELETE PATCH methods.
static getOneToMany($instance, $member, $useCache=NULL, $annot=null)
Assign / load the child records in the $member member of $instance.
connect()
Realise the connection to the server To override in derived classes to define your own authentication...
getOne($keyValues, $loadManyToOne=false, $loadOneToMany=false, $useCache=false)
Get the first object corresponding to the $keyValues.
getOneToMany($member, $useCache=false)
static remove($instance)
Deletes the object $instance from the database.
getManyToMany($member, $useCache=false)
static update($instance, $updateMany=false)
Updates an existing $instance in the database.
index()
Returns all objects for the resource $model ("cache"=>false)
static insert($instance, $insertMany=false)
Inserts a new instance $ instance into the database.