11 use Symfony\Component\HttpFoundation\JsonResponse;
45 private $response_sent =
false;
50 private $allowed_views = [];
61 $this->hooks = $hooks;
63 $this->
input = $input;
66 if ($this->
input->get(
'elgg_fetch_messages',
true)) {
67 $message_filter = [$this,
'appendMessages'];
71 if ($this->
input->get(
'elgg_fetch_deps',
true)) {
72 $deps_filter = [$this,
'appendDeps'];
128 $api_response->setData((
object)[
131 $api_response = $this->filterApiResponse($api_response, $hook_type);
132 $response = $this->buildHttpResponse($api_response);
134 $this->response_sent =
true;
146 $api_response = $this->filterApiResponse($api_response, $hook_type);
147 $response = $this->buildHttpResponse($api_response);
149 $this->response_sent =
true;
161 $response =
new JsonResponse([
'error' => $msg], $status);
163 $this->response_sent =
true;
175 private function filterApiResponse(
AjaxResponse $api_response, $hook_type =
'') {
176 $api_response->
setTtl($this->
input->get(
'elgg_response_ttl', 0,
false));
180 $api_response = $this->hooks->trigger($hook, $hook_type,
null, $api_response);
182 throw new RuntimeException(
"The value returned by hook [$hook, $hook_type] was not an ApiResponse");
186 return $api_response;
198 private function buildHttpResponse(AjaxResponse $api_response, $allow_removing_headers =
null) {
199 if ($api_response->isCancelled()) {
200 return new JsonResponse([
'error' =>
"The response was cancelled"], 400);
203 $response =
new JsonResponse($api_response->getData());
205 $ttl = $api_response->getTtl();
208 if (!isset($allow_removing_headers)) {
209 $allow_removing_headers = !
elgg()->isTestingApplication();
212 if ($allow_removing_headers) {
213 header_remove(
'Expires');
214 header_remove(
'Pragma');
215 header_remove(
'Cache-Control');
219 $response->headers->remove(
'Cache-Control');
221 $response->setClientTtl($ttl);
224 $response->headers->set(
'Expires', gmdate(
'D, d M Y H:i:s \G\M\T',
time() + $ttl));
246 $response->getData()->_elgg_msgs = (object)$this->msgs->dumpRegister();
266 $response->getData()->_elgg_deps = (array) $this->amd_config->getDependencies();
277 $this->allowed_views[
$view] =
true;
287 unset($this->allowed_views[
$view]);
295 return array_keys($this->allowed_views);
elgg table input[type=checkbox]
Models the Ajax API service.
__construct(PluginHooksService $hooks, SystemMessagesService $msgs, Input $input, Config $amdConfig)
Constructor.
isAjax2Request()
Did the request come from the elgg/Ajax module?
unregisterView($view)
Unregister a view for ajax calls.
respondFromOutput($output, $hook_type='', $try_decode=true)
Send a JSON HTTP response with the given output.
appendDeps($hook, $type, $response, $params)
Send required AMD modules list back with the response.
respondFromApiResponse(AjaxResponse $api_response, $hook_type='')
Send a JSON HTTP response based on the given API response.
isReady()
Is the service ready to respond to the request?
getViews()
Returns an array of views allowed for ajax calls.
registerView($view)
Register a view to be available for ajax calls.
appendMessages($hook, $type, $response, $params)
Send system messages back with the response.
decodeJson($string)
Attempt to JSON decode the given string.
respondWithError($msg='', $status=400)
Send a JSON HTTP 400 response.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
if(! $display_name) $type
$object
These two snippets demonstrates triggering an event and how to register for that event.
setTtl($ttl=0)
Set the max-age for client caching.