[!!!][TASK] Remove deprecated frontend-related hooks and include scripts
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / TypoScriptFrontendInitialization.php
index 5e2db74..b83d686 100644 (file)
@@ -15,11 +15,17 @@ namespace TYPO3\CMS\Frontend\Middleware;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Doctrine\DBAL\Exception\ConnectionException;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Server\MiddlewareInterface;
 use Psr\Http\Server\RequestHandlerInterface;
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerAwareTrait;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Frontend\Controller\ErrorController;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 
 /**
@@ -29,12 +35,15 @@ use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
  * For now, GeneralUtility::_GP() is used in favor of $request->getQueryParams() due to
  * hooks who could have $_GET/$_POST modified before.
  *
- * @internal
+ * @internal this middleware might get removed in TYPO3 v10.0.
  */
-class TypoScriptFrontendInitialization implements MiddlewareInterface
+class TypoScriptFrontendInitialization implements MiddlewareInterface, LoggerAwareInterface
 {
+    use LoggerAwareTrait;
+
     /**
-     * Creates an instance of TSFE and sets it as a global variable
+     * Creates an instance of TSFE and sets it as a global variable,
+     * also pings the database in order ensure a valid database connection.
      *
      * @param ServerRequestInterface $request
      * @param RequestHandlerInterface $handler
@@ -47,12 +56,29 @@ class TypoScriptFrontendInitialization implements MiddlewareInterface
             null,
             GeneralUtility::_GP('id'),
             GeneralUtility::_GP('type'),
-            GeneralUtility::_GP('no_cache'),
+            null,
             GeneralUtility::_GP('cHash'),
             null,
             GeneralUtility::_GP('MP')
         );
-        $GLOBALS['TSFE']->connectToDB();
+        if (GeneralUtility::_GP('no_cache')) {
+            $GLOBALS['TSFE']->set_no_cache('&no_cache=1 has been supplied, so caching is disabled! URL: "' . (string)$request->getUri() . '"');
+        }
+
+        // Set up the database connection and see if the connection can be established
+        try {
+            $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
+            $connection->connect();
+        } catch (ConnectionException $exception) {
+            // Cannot connect to current database
+            $message = 'Cannot connect to the configured database "' . $connection->getDatabase() . '"';
+            $this->logger->emergency($message, ['exception' => $exception]);
+            try {
+                return GeneralUtility::makeInstance(ErrorController::class)->unavailableAction($request, $message);
+            } catch (ServiceUnavailableException $e) {
+                throw new ServiceUnavailableException($message, 1526013723);
+            }
+        }
         return $handler->handle($request);
     }
 }