51 if (!\headers_sent()) {
52 @\set_exception_handler(array($this,
'_errorHandler'));
53 $this->config = Startup::getConfig();
55 $this->server->cors();
58 $this->server->_setContentType($this->contentType);
59 $this->restCache = CacheManager::getRestCacheController(\get_class($this));
71 if (isset ($this->restCache [
'authorizations'])) {
72 if (\array_search($action, $this->restCache [
'authorizations']) !==
false) {
73 return $this->server->isValid(
function ($datas =
null) use ($action) {
101 if (isset ($this->restCache [
"authorizations"])) {
102 return \array_search($action, $this->restCache [
"authorizations"]) !==
false;
108 throw new \Exception (
'HTTP/1.1 401 Unauthorized, you need an access token for this request', 401);
117 $resp = $this->server->connect($datas);
118 echo $this->_format($resp);
126 $resp = $this->server->refreshToken();
127 echo $this->_format($resp);
135 $this->server->finalizeTokens();
139 $this->_setResponseCode(Router::getStatusCode());
140 echo $this->_getResponseFormatter()->formatException($e);
144 \http_response_code($value);
154 public function _get($condition =
'1=1', $include =
false, $useCache =
false) {
156 $condition = $this->getCondition($condition);
157 $include = $this->getInclude($include);
158 $useCache = UString::isBooleanTrue($useCache);
159 $datas = DAO::getAll($this->model, $condition, $include,
null, $useCache);
160 echo $this->_getResponseFormatter()->get($datas);
161 }
catch (\Exception $e) {
162 $this->_setResponseCode(500);
163 echo $this->_getResponseFormatter()->formatException($e);
176 $filter = $this->getCondition($this->getRequestParam(
'filter',
'1=1'));
178 if (isset ($_GET [
'page'])) {
179 $pageNumber = $_GET [
'page'] [
'number'];
180 $pageSize = $_GET [
'page'] [
'size'] ?? 1;
181 $pages = $this->generatePagination($filter, $pageNumber, $pageSize);
183 $datas = DAO::getAll($this->model, $filter, $this->getInclude($this->getRequestParam(
'include',
false)));
184 EventsManager::trigger(RestEvents::BEFORE_GET_ALL, $datas, $this);
185 echo $this->_getResponseFormatter()->get($datas, $pages);
195 public function _getOne($keyValues, $include =
false, $useCache =
false) {
196 $keyValues = $this->getCondition($keyValues);
197 $include = $this->getInclude($include);
198 $useCache = UString::isBooleanTrue($useCache);
199 $data = DAO::getById($this->model, $keyValues, $include, $useCache);
200 EventsManager::trigger(RestEvents::BEFORE_GET_ONE, $data, $this);
202 $_SESSION [
"_restInstance"] = $data;
203 echo $this->_getResponseFormatter()->getOne($data);
205 $this->_setResponseCode(404);
206 echo $this->_getResponseFormatter()->format(RestError::notFound($keyValues,
"RestController/getOne")->asArray());
211 return $this->_getResponseFormatter()->format($arrayMessage);
221 public function _getManyToOne($ids, $member, $include =
false, $useCache =
false) {
222 $this->getAssociatedMemberValues_($ids,
function ($instance, $member, $include, $useCache) {
223 return DAO::getManyToOne($instance, $member, $include, $useCache);
224 }, $member, $include, $useCache,
false);
235 public function _getOneToMany($ids, $member, $include =
false, $useCache =
false) {
236 $this->getAssociatedMemberValues_($ids,
function ($instance, $member, $include, $useCache) {
237 return DAO::getOneToMany($instance, $member, $include, $useCache);
238 }, $member, $include, $useCache,
true);
249 public function _getManyToMany($ids, $member, $include =
false, $useCache =
false) {
250 $this->getAssociatedMemberValues_($ids,
function ($instance, $member, $include, $useCache) {
251 return DAO::getManyToMany($instance, $member, $include,
null, $useCache);
252 }, $member, $include, $useCache,
true);
262 $instance = DAO::getById($this->model, $keyValues,
false);
263 $this->operate_($instance,
function ($instance) {
264 $datas = $this->getDatas();
265 $this->_setValuesToObject($instance, $datas);
266 EventsManager::trigger(RestEvents::BEFORE_UPDATE, $instance, $datas, $this);
267 if ($this->_validateInstance($instance, \array_keys($datas))) {
268 return $this->updateOperation($instance, $datas,
true);
271 },
'updated',
'Unable to update the instance', $keyValues);
279 $model = $this->model;
280 $instance =
new $model ();
281 $this->operate_($instance,
function ($instance) use ($model) {
282 $datas = $this->getDatas();
283 $this->_setValuesToObject($instance, $datas);
284 EventsManager::trigger(RestEvents::BEFORE_INSERT, $instance, $datas, $this);
285 $fields = \array_keys(OrmUtils::getSerializableFields($model));
286 if ($this->_validateInstance($instance, $fields, [
'id' =>
false])) {
287 $this->_setResponseCode(201);
288 return $this->addOperation($instance, $datas,
true);
291 },
'inserted',
'Unable to insert the instance', []);
304 $relations = OrmUtils::getAnnotFieldsInRelations($this->model);
305 if (isset ($relations [$member])) {
306 $include = $this->getRequestParam(
'include',
true);
307 switch ($relations [$member] [
'type']) {
309 $this->_getManyToOne($id, $member, $include);
312 $this->_getOneToMany($id, $member, $include);
315 $this->_getManyToMany($id, $member, $include);
327 $instance = DAO::getById($this->model, $keyValues,
false);
328 $this->operate_($instance,
function ($instance) {
329 return DAO::remove($instance);
330 },
'deleted',
'Unable to delete the instance', $keyValues);
343 return 'restController.tpl';
Manager for caches (Router, Rest, models).
Base class for controllers.
static getAction()
Returns tha active action.
Abstract base class for Rest controllers.
__construct()
Constructor initialize $view variable.
_delete(... $keyValues)
Delete the instance of $model selected by the primary key $keyValues.
refreshToken()
Refresh an active token.
_getOneToMany($ids, $member, $include=false, $useCache=false)
_getAll()
Returns all the instances from the model $resource.
_update(... $keyValues)
Update an instance of $model selected by the primary key $keyValues Require members values in $_POST ...
_getOne($keyValues, $include=false, $useCache=false)
Get the first object corresponding to the $keyValues.
_getManyToMany($ids, $member, $include=false, $useCache=false)
_getManyToOne($ids, $member, $include=false, $useCache=false)
connect()
Realize the connection to the server.
initialize()
Method called before each action Can be override in derived class.
finalize()
Method called after each action Can be override in derived class.
_getRelationShip($id, $member)
Returns an associated member value(s).
checkPermissions($action, $datas=null)
To override in derived classes.
onInvalidControl()
Called if isValid () returns false To be override in sub classes.
_add()
Insert a new instance of $model Require members values in $_POST array.
isValid($action)
Returns True if access to the controller is allowed To be override in sub classes.
static _getTemplateFile()
Returns the template for creating this type of controller.
_get($condition='1=1', $include=false, $useCache=false)
Returns a list of objects from the server.
requireAuth($action)
Returns true if $action require an authentification with token.
static getRoutesPathByController($controller)
Gateway class between database and object model.
Object/relational mapping utilities.