[TASK] Streamline hooks and middlewares related to PSR-15
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / TypoScriptFrontendInitialization.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Middleware;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Doctrine\DBAL\Exception\ConnectionException;
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\MiddlewareInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
23 use Psr\Log\LoggerAwareInterface;
24 use Psr\Log\LoggerAwareTrait;
25 use TYPO3\CMS\Core\Database\ConnectionPool;
26 use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Frontend\Controller\ErrorController;
29 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
30
31 /**
32 * Creates an instance of TypoScriptFrontendController and makes this globally available
33 * via $GLOBALS['TSFE'].
34 *
35 * For now, GeneralUtility::_GP() is used in favor of $request->getQueryParams() due to
36 * hooks who could have $_GET/$_POST modified before.
37 *
38 * @internal this middleware might get removed in TYPO3 v10.0.
39 */
40 class TypoScriptFrontendInitialization implements MiddlewareInterface, LoggerAwareInterface
41 {
42 use LoggerAwareTrait;
43
44 /**
45 * Creates an instance of TSFE and sets it as a global variable,
46 * also pings the database in order ensure a valid database connection.
47 *
48 * @param ServerRequestInterface $request
49 * @param RequestHandlerInterface $handler
50 * @return ResponseInterface
51 */
52 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
53 {
54 $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
55 TypoScriptFrontendController::class,
56 null,
57 GeneralUtility::_GP('id'),
58 GeneralUtility::_GP('type'),
59 null,
60 GeneralUtility::_GP('cHash'),
61 null,
62 GeneralUtility::_GP('MP')
63 );
64 if (GeneralUtility::_GP('no_cache')) {
65 $GLOBALS['TSFE']->set_no_cache('&no_cache=1 has been supplied, so caching is disabled! URL: "' . (string)$request->getUri() . '"');
66 }
67
68 // Set up the database connection and see if the connection can be established
69 try {
70 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
71 $connection->connect();
72 } catch (ConnectionException $exception) {
73 // Cannot connect to current database
74 $message = 'Cannot connect to the configured database "' . $connection->getDatabase() . '"';
75 $this->logger->emergency($message, ['exception' => $exception]);
76 try {
77 return GeneralUtility::makeInstance(ErrorController::class)->unavailableAction($request, $message);
78 } catch (ServiceUnavailableException $e) {
79 throw new ServiceUnavailableException($message, 1526013723);
80 }
81 }
82 // Call post processing function for DB connection:
83 if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['connectToDB'])) {
84 trigger_error('The "connectToDB" hook will be removed in TYPO3 v10.0 in favor of PSR-15. Use a middleware instead.', E_USER_DEPRECATED);
85 $_params = ['pObj' => &$GLOBALS['TSFE']];
86 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['connectToDB'] as $_funcRef) {
87 GeneralUtility::callUserFunction($_funcRef, $_params, $GLOBALS['TSFE']);
88 }
89 }
90
91 return $handler->handle($request);
92 }
93 }