|
1
|
|
|
<?php |
|
2
|
|
|
namespace Aoe\Restler\System; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2015 AOE GmbH <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* All rights reserved |
|
10
|
|
|
* |
|
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
12
|
|
|
* free software; you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU General Public License as published by |
|
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
15
|
|
|
* (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* The GNU General Public License can be found at |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
19
|
|
|
* |
|
20
|
|
|
* This script is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
26
|
|
|
***************************************************************/ |
|
27
|
|
|
|
|
28
|
|
|
use Aoe\Restler\System\Restler\Builder as RestlerBuilder; |
|
29
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
30
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
31
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
32
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
33
|
|
|
use TYPO3\CMS\Core\Http\Response; |
|
34
|
|
|
use TYPO3\CMS\Core\Http\Stream; |
|
35
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
36
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManager; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @package Restler |
|
40
|
|
|
*/ |
|
41
|
|
|
class Dispatcher implements MiddlewareInterface |
|
42
|
|
|
{ |
|
43
|
1 |
|
/** |
|
44
|
|
|
* @var RestlerBuilder |
|
45
|
1 |
|
*/ |
|
46
|
1 |
|
private $restlerBuilder; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct(ObjectManager $objectManager = null) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!$objectManager) { |
|
51
|
1 |
|
$objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
|
52
|
|
|
} |
|
53
|
1 |
|
$this->restlerBuilder = $objectManager->get(RestlerBuilder::class); |
|
54
|
1 |
|
} |
|
55
|
1 |
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Process an incoming server request. |
|
58
|
|
|
* |
|
59
|
|
|
* Processes an incoming server request in order to produce a response. |
|
60
|
|
|
* If unable to produce the response itself, it may delegate to the provided |
|
61
|
|
|
* request handler to do so. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
64
|
|
|
{ |
|
65
|
|
|
$restlerObj = $this->restlerBuilder->build($request); |
|
66
|
|
|
if ($this->isRestlerUrl($request->getUri()->getPath())) { |
|
67
|
|
|
/** |
|
68
|
|
|
* We might end up with a loaded TSFE->config but an empty |
|
69
|
|
|
* TSFE->tmpl->setup. That is depending on the state of the caches. |
|
70
|
|
|
* This in turn will lead to an empty extbase configuration. |
|
71
|
|
|
* And this will lead to failures loading sys_file_reference |
|
72
|
|
|
* as it will use the default tableName of tx_extbase_domain_model_filereference |
|
73
|
|
|
*/ |
|
74
|
|
|
// check for proper template config state |
|
75
|
|
|
if (!$GLOBALS['TSFE']->tmpl->loaded) { |
|
76
|
|
|
if (empty($GLOBALS['TSFE']->rootLine) && !empty($GLOBALS['TSFE']->id)) { |
|
77
|
|
|
$GLOBALS['TSFE']->determineId(); |
|
78
|
|
|
if ($GLOBALS['TSFE']->tmpl === null) { |
|
79
|
|
|
$GLOBALS['TSFE']->getConfigArray(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!empty($GLOBALS['TSFE']->tmpl) && !empty($GLOBALS['TSFE']->rootLine)) { |
|
84
|
|
|
$GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
// wrap reponse into a stream to pass along to the rest of the Typo3 framework |
|
90
|
|
|
$body = new Stream('php://temp', 'wb+'); |
|
91
|
|
|
$body->write($restlerObj->handle()); |
|
92
|
|
|
$body->rewind(); |
|
93
|
|
|
|
|
94
|
|
|
return new Response($body, $restlerObj->responseCode); |
|
95
|
|
|
} |
|
96
|
|
|
return $handler->handle($request); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function isRestlerUrl($uri): bool |
|
100
|
|
|
{ |
|
101
|
|
|
return \Aoe\Restler\System\Restler\Routes::containsUrl($uri); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|