44 private $response_sent =
false;
55 $this->hooks = $hooks;
57 $this->
input = $input;
60 if ($this->
input->get(
'elgg_fetch_messages',
true)) {
61 $message_filter = [$this,
'appendMessages'];
65 if ($this->
input->get(
'elgg_fetch_deps',
true)) {
66 $deps_filter = [$this,
'appendDeps'];
122 $api_response->setData((
object)[
125 $api_response = $this->filterApiResponse($api_response, $hook_type);
126 $response = $this->buildHttpResponse($api_response);
128 $this->response_sent =
true;
140 $api_response = $this->filterApiResponse($api_response, $hook_type);
141 $response = $this->buildHttpResponse($api_response);
143 $this->response_sent =
true;
155 $response =
new JsonResponse([
'error' => $msg], $status);
157 $this->response_sent =
true;
169 private function filterApiResponse(
AjaxResponse $api_response, $hook_type =
'') {
170 $api_response->
setTtl($this->
input->get(
'elgg_response_ttl', 0,
false));
174 $api_response = $this->hooks->trigger($hook, $hook_type, null, $api_response);
176 throw new RuntimeException(
"The value returned by hook [$hook, $hook_type] was not an ApiResponse");
180 return $api_response;
192 private function buildHttpResponse(
AjaxResponse $api_response, $allow_removing_headers =
true) {
194 return new JsonResponse([
'error' =>
"The response was cancelled"], 400);
197 $response =
new JsonResponse($api_response->
getData());
199 $ttl = $api_response->
getTtl();
202 if ($allow_removing_headers) {
203 header_remove(
'Expires');
204 header_remove(
'Pragma');
205 header_remove(
'Cache-Control');
209 $response->headers->remove(
'Cache-Control');
211 $response->setClientTtl($ttl);
214 $response->headers->set(
'Expires', gmdate(
'D, d M Y H:i:s \G\M\T',
time() + $ttl));
233 $response->
getData()->_elgg_msgs = (object)$this->msgs->dumpRegister();
250 $response->
getData()->_elgg_deps = (array) $this->amd_config->getDependencies();
$object
These two snippets demonstrates triggering an event and how to register for that event.
isCancelled()
Has the response been cancelled?
setTtl($ttl=0)
Set the max-age for client caching.
isAjax2Request()
Did the request come from the elgg/Ajax module?
appendMessages($hook, $type, AjaxResponse $response, $params)
Send system messages back with the response.
Models the Ajax API service.
__construct(PluginHooksService $hooks, SystemMessagesService $msgs, Input $input, Config $amdConfig)
Constructor.
decodeJson($string)
Attempt to JSON decode the given string.
appendDeps($hook, $type, AjaxResponse $response, $params)
Send required AMD modules list back with the response.
respondFromOutput($output, $hook_type= '', $try_decode=true)
Send a JSON HTTP response with the given output.
respondFromApiResponse(AjaxResponse $api_response, $hook_type= '')
Send a JSON HTTP response based on the given API response.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
getData()
Get the response data, which will be a stdClass object with property "value".
isReady()
Is the service ready to respond to the request?
elgg table input[type=checkbox]
getTtl()
Get the max-age for client caching.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
respondWithError($msg, $status=400)
Send a JSON HTTP 400 response.