Completed
Pull Request — master (#50)
by
unknown
04:43
created

Dispatcher::process()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 0
cp 0
rs 8.0675
c 0
b 0
f 0
cc 8
nc 8
nop 2
crap 72
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
67
        if ($this->isRestlerUrl("/" . $restlerObj->url)) {
68
            /**
69
             * We might end up with a loaded TSFE->config but an empty
70
             * TSFE->tmpl->setup. That is depending on the state of the caches.
71
             * This in turn will lead to an empty extbase configuration.
72
             * And this will lead to failures loading sys_file_reference
73
             * as it will use the default tableName of tx_extbase_domain_model_filereference
74
             */
75
            // See https://review.typo3.org/c/Packages/TYPO3.CMS/+/60713 for reasons
76
77
            // check for proper template config state
78
            if (!$GLOBALS["TSFE"]->tmpl->loaded) {
79
                if (empty($GLOBALS["TSFE"]->rootLine) && !empty($GLOBALS["TSFE"]->id)) {
80
                    $GLOBALS["TSFE"]->determineId();
81
                    if ($GLOBALS["TSFE"]->tmpl === null) {
82
                        $GLOBALS["TSFE"]->getConfigArray();
83
                    }
84
                }
85
86
                if (!empty($GLOBALS["TSFE"]->tmpl) && !empty($GLOBALS["TSFE"]->rootLine)) {
87
                    $GLOBALS["TSFE"]->tmpl->start($GLOBALS["TSFE"]->rootLine);
88
                }
89
            }
90
91
92
            // wrap reponse into a stream to pass along to the rest of the Typo3 framework
93
            $body = new Stream("php://temp", "wb+");
94
            $body->write($restlerObj->handle());
95
            $body->rewind();
96
97
            return new Response($body, $restlerObj->responseCode);
98
        }
99
        return $handler->handle($request);
100
    }
101
102
    private function isRestlerUrl($uri): bool
103
    {
104
        return \Aoe\Restler\System\Restler\Routes::containsUrl($uri);
105
    }
106
107
}
108