[TASK] Create own response instance in controller actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Controller / AjaxDispatcher.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Http\JsonResponse;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Workspaces\Controller\Remote\ActionHandler;
22 use TYPO3\CMS\Workspaces\Controller\Remote\MassActionHandler;
23 use TYPO3\CMS\Workspaces\Controller\Remote\RemoteServer;
24
25 /**
26 * Implements the AJAX functionality for the various asynchronous calls
27 */
28 class AjaxDispatcher
29 {
30 /**
31 * @var array
32 */
33 protected $classMap = [
34 'RemoteServer' => RemoteServer::class,
35 'MassActions' => MassActionHandler::class,
36 'Actions' => ActionHandler::class
37 ];
38
39 /**
40 * @param ServerRequestInterface $request
41 * @return ResponseInterface
42 */
43 public function dispatch(ServerRequestInterface $request): ResponseInterface
44 {
45 $callStack = \GuzzleHttp\json_decode($request->getBody()->getContents());
46 if (!is_array($callStack)) {
47 $callStack = [$callStack];
48 }
49 $results = [];
50 foreach ($callStack as $call) {
51 $className = $this->classMap[$call->action];
52 $method = $call->method;
53 $parameters = $call->data;
54 $instance = GeneralUtility::makeInstance($className);
55 $results[] = $this->buildResultFromResponse(call_user_func_array([$instance, $method], $parameters), $call);
56 }
57 return (new JsonResponse())->setPayload($results);
58 }
59
60 /**
61 * @param mixed $responseFromMethod
62 * @param \stdClass $call
63 *
64 * @return \stdClass
65 */
66 protected function buildResultFromResponse($responseFromMethod, $call)
67 {
68 $tmp = new \stdClass();
69 $tmp->action = $call->action;
70 $tmp->debug = '';
71 $tmp->method = $call->method;
72 $tmp->result = $responseFromMethod;
73 $tmp->tid = $call->tid;
74 $tmp->type = $call->type;
75 return $tmp;
76 }
77 }