‪TYPO3CMS  ‪main
AjaxDispatcher.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
27 
35 {
36  protected array ‪$classMap = [
37  'RemoteServer' => RemoteServer::class,
38  'MassActions' => MassActionHandler::class,
39  'Actions' => ActionHandler::class,
40  ];
41 
42  public function ‪dispatch(ServerRequestInterface $request): ResponseInterface
43  {
44  $callStack = json_decode($request->getBody()->getContents());
45  if (!is_array($callStack)) {
46  $callStack = [$callStack];
47  }
48  $results = [];
49  foreach ($callStack as $call) {
50  $className = $this->classMap[$call->action];
51  $method = $call->method;
52  $parameters = $call->data;
53  if ($parameters[1] === null) {
54  // Hack to have $request as second argument.
55  unset($parameters[1]);
56  }
57  $parameters[] = $request;
58  $instance = GeneralUtility::makeInstance($className);
59  $results[] = $this->‪buildResultFromResponse($instance->$method(...$parameters), $call);
60  }
61  return new ‪JsonResponse($results);
62  }
63 
70  protected function ‪buildResultFromResponse($responseFromMethod, $call)
71  {
72  $tmp = new \stdClass();
73  $tmp->action = $call->action;
74  $tmp->method = $call->method;
75  $tmp->result = $responseFromMethod;
76  $tmp->tid = $call->tid;
77  $tmp->type = $call->type;
78  return $tmp;
79  }
80 }
‪TYPO3\CMS\Workspaces\Controller\AjaxDispatcher\$classMap
‪array $classMap
Definition: AjaxDispatcher.php:36
‪TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler
Definition: MassActionHandler.php:32
‪TYPO3\CMS\Workspaces\Controller\AjaxDispatcher\dispatch
‪dispatch(ServerRequestInterface $request)
Definition: AjaxDispatcher.php:42
‪TYPO3\CMS\Workspaces\Controller\AjaxDispatcher\buildResultFromResponse
‪stdClass buildResultFromResponse($responseFromMethod, $call)
Definition: AjaxDispatcher.php:70
‪TYPO3\CMS\Workspaces\Controller\Remote\RemoteServer
Definition: RemoteServer.php:56
‪TYPO3\CMS\Workspaces\Controller\AjaxDispatcher
Definition: AjaxDispatcher.php:35
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪TYPO3\CMS\Workspaces\Controller\Remote\ActionHandler
Definition: ActionHandler.php:38
‪TYPO3\CMS\Workspaces\Controller
Definition: AjaxController.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52