Completed
Pull Request — master (#50)
by
unknown
05:47
created

Dispatcher::isRestlerUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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