Completed
Pull Request — master (#50)
by
unknown
11:37
created

Dispatcher::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\MiddlewareInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
use TYPO3\CMS\Core\Http\Response;
33
use TYPO3\CMS\Core\Http\Stream;
34
35
class Dispatcher extends RestlerBuilderAware implements MiddlewareInterface
36
{
37
    /**
38
     * Process an incoming server request.
39
     *
40
     * Processes an incoming server request in order to produce a response.
41
     * If unable to produce the response itself, it may delegate to the provided
42
     * request handler to do so.
43 1
     */
44
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
45 1
    {
46 1
        if ($this->isRestlerPrefix($request->getUri()->getPath())) {
47
            $restlerObj = $this->getRestlerBuilder()->build($request);
48
49
            if ($this->isRestlerUrl('/' . $restlerObj->url)) {
50
                /**
51 1
                 * We might end up with a loaded TSFE->config but an empty
52
                 * TSFE->tmpl->setup. That is depending on the state of the caches.
53 1
                 * This in turn will lead to an empty extbase configuration.
54 1
                 * And this will lead to failures loading sys_file_reference
55 1
                 * as it will use the default tableName of tx_extbase_domain_model_filereference
56
                 */
57
                // See https://review.typo3.org/c/Packages/TYPO3.CMS/+/60713 for reasons
58
59
                // check for proper template config state
60
                if (!$GLOBALS['TSFE']->tmpl->loaded) {
61
                    if (empty($GLOBALS['TSFE']->rootLine) && !empty($GLOBALS['TSFE']->id)) {
62
                        $GLOBALS['TSFE']->determineId();
63
                        if ($GLOBALS['TSFE']->tmpl === null) {
64
                            $GLOBALS['TSFE']->getConfigArray();
65
                        }
66
                    }
67
68
                    if (!empty($GLOBALS['TSFE']->tmpl) && !empty($GLOBALS['TSFE']->rootLine)) {
69
                        $GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine);
70
                    }
71
                }
72
73
                // wrap reponse into a stream to pass along to the rest of the Typo3 framework
74
                $body = new Stream('php://temp', 'wb+');
75
                $body->write($restlerObj->handle());
76
                $body->rewind();
77
78
                return new Response($body, $restlerObj->responseCode);
79
            }
80
        }
81
82
        return $handler->handle($request);
83
    }
84
85
    private function isRestlerUrl($uri): bool
86
    {
87
        return \Aoe\Restler\System\Restler\Routes::containsUrl($uri);
88
    }
89
}
90