Skip to content

Commit

Permalink
[TASK] Build extbase requests early in Bootstrap
Browse files Browse the repository at this point in the history
This patch introduces a couple of changes to the extbase request
creation and handling process.

The request builder is used during bootstrap to create an extbase
request object. This enables building the extbase request from the
incoming server request in the future without passing the server
request through a layer of services.

Further, it enables a more sensible API for the request handlers
whose methods canHandleRequest() and handleRequest() did not receive
the extbase request as arguments for evaluation in the past.

The idea is to have the extbase request and an attached context object
as single source of truth regarding request data and avoid storing
that data elsewhere or accessing it through services during runtime.

Releases: master
Resolves: #92703
Change-Id: I6c9c3fd32e1af32ef3f73aa12073d34c8a63a43d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/66281
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack <benni@typo3.org>
  • Loading branch information
alexanderschnitzler authored and bmack committed Oct 27, 2020
1 parent 1e0284a commit ce119a1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 43 deletions.
21 changes: 15 additions & 6 deletions typo3/sysext/extbase/Classes/Core/Bootstrap.php
Expand Up @@ -28,6 +28,7 @@
use TYPO3\CMS\Extbase\Configuration\RequestHandlersConfigurationFactory;
use TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver;
use TYPO3\CMS\Extbase\Mvc\Response as ExtbaseResponse;
use TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder;
use TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
use TYPO3\CMS\Extbase\Service\CacheService;
Expand Down Expand Up @@ -79,18 +80,25 @@ class Bootstrap implements BootstrapInterface
*/
protected $cacheService;

/**
* @var \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
*/
protected $extbaseRequestBuilder;

public function __construct(
ContainerInterface $container,
ConfigurationManagerInterface $configurationManager,
PersistenceManagerInterface $persistenceManager,
RequestHandlerResolver $requestHandlerResolver,
CacheService $cacheService
CacheService $cacheService,
RequestBuilder $extbaseRequestBuilder
) {
$this->container = $container;
$this->configurationManager = $configurationManager;
$this->persistenceManager = $persistenceManager;
$this->requestHandlerResolver = $requestHandlerResolver;
$this->cacheService = $cacheService;
$this->extbaseRequestBuilder = $extbaseRequestBuilder;
}

/**
Expand Down Expand Up @@ -178,9 +186,9 @@ public function run(string $content, array $configuration): string
*/
protected function handleRequest(): string
{
$requestHandler = $this->requestHandlerResolver->resolveRequestHandler();

$response = $requestHandler->handleRequest();
$extbaseRequest = $this->extbaseRequestBuilder->build();
$requestHandler = $this->requestHandlerResolver->resolveRequestHandler($extbaseRequest);
$response = $requestHandler->handleRequest($extbaseRequest);
// If response is NULL after handling the request we need to stop
// This happens for instance, when a USER object was converted to a USER_INT
// @see TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::handleRequest()
Expand Down Expand Up @@ -221,9 +229,10 @@ public function handleBackendRequest(ServerRequestInterface $request): ResponseI

$this->initialize($configuration);

$requestHandler = $this->requestHandlerResolver->resolveRequestHandler();
$extbaseRequest = $this->extbaseRequestBuilder->build();
$requestHandler = $this->requestHandlerResolver->resolveRequestHandler($extbaseRequest);
/** @var ExtbaseResponse $extbaseResponse */
$extbaseResponse = $requestHandler->handleRequest();
$extbaseResponse = $requestHandler->handleRequest($extbaseRequest);

// Convert to PSR-7 response and hand it back to TYPO3 Core
$response = $this->convertExtbaseResponseToPsr7Response($extbaseResponse);
Expand Down
8 changes: 5 additions & 3 deletions typo3/sysext/extbase/Classes/Mvc/RequestHandlerInterface.php
Expand Up @@ -23,16 +23,18 @@ interface RequestHandlerInterface
/**
* Handles a raw request and returns the response.
*
* @return \TYPO3\CMS\Extbase\Mvc\ResponseInterface
* @param RequestInterface $request
* @return ResponseInterface
*/
public function handleRequest();
public function handleRequest(RequestInterface $request);

/**
* Checks if the request handler can handle the current request.
*
* @param RequestInterface $request
* @return bool TRUE if it can handle the request, otherwise FALSE
*/
public function canHandleRequest();
public function canHandleRequest(RequestInterface $request);

/**
* Returns the priority - how eager the handler is to actually handle the
Expand Down
5 changes: 3 additions & 2 deletions typo3/sysext/extbase/Classes/Mvc/RequestHandlerResolver.php
Expand Up @@ -50,10 +50,11 @@ public function __construct(ContainerInterface $container, RequestHandlersConfig
* Analyzes the raw request and tries to find a request handler which can handle
* it. If none is found, an exception is thrown.
*
* @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request A request
* @return \TYPO3\CMS\Extbase\Mvc\RequestHandlerInterface A request handler
* @throws \TYPO3\CMS\Extbase\Mvc\Exception
*/
public function resolveRequestHandler()
public function resolveRequestHandler(RequestInterface $request)
{
$suitableRequestHandlers = [];
foreach ($this->requestHandlersConfiguration->getRegisteredRequestHandlers() as $requestHandlerClassName) {
Expand All @@ -62,7 +63,7 @@ public function resolveRequestHandler()
? $this->container->get($requestHandlerClassName)
: GeneralUtility::makeInstance($requestHandlerClassName)
;
if ($requestHandler->canHandleRequest()) {
if ($requestHandler->canHandleRequest($request)) {
$priority = $requestHandler->getPriority();
if (isset($suitableRequestHandlers[$priority])) {
throw new Exception('More than one request handler with the same priority can handle the request, but only one handler may be active at a time!', 1176475350);
Expand Down
23 changes: 0 additions & 23 deletions typo3/sysext/extbase/Classes/Mvc/Web/AbstractRequestHandler.php
Expand Up @@ -36,11 +36,6 @@ abstract class AbstractRequestHandler implements RequestHandlerInterface
*/
protected $dispatcher;

/**
* @var \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
*/
protected $requestBuilder;

/**
* @var \TYPO3\CMS\Extbase\Service\EnvironmentService
*/
Expand All @@ -54,14 +49,6 @@ public function injectDispatcher(Dispatcher $dispatcher)
$this->dispatcher = $dispatcher;
}

/**
* @param \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder $requestBuilder
*/
public function injectRequestBuilder(RequestBuilder $requestBuilder)
{
$this->requestBuilder = $requestBuilder;
}

/**
* @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
*/
Expand All @@ -78,16 +65,6 @@ public function injectEnvironmentService(EnvironmentService $environmentService)
$this->environmentService = $environmentService;
}

/**
* This request handler can handle any web request.
*
* @return bool If the request is a web request, TRUE otherwise FALSE
*/
public function canHandleRequest()
{
return true;
}

/**
* Returns the priority - how eager the handler is to actually handle the
* request.
Expand Down
14 changes: 9 additions & 5 deletions typo3/sysext/extbase/Classes/Mvc/Web/BackendRequestHandler.php
Expand Up @@ -16,7 +16,9 @@
namespace TYPO3\CMS\Extbase\Mvc\Web;

use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Extbase\Mvc\Response;
use TYPO3\CMS\Extbase\Mvc\Exception\InfiniteLoopException;
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
use TYPO3\CMS\Extbase\Mvc\ResponseInterface;

/**
* A request handler which can handle web requests invoked by the backend.
Expand All @@ -27,20 +29,22 @@ class BackendRequestHandler extends AbstractRequestHandler
/**
* Handles the web request. The response will automatically be sent to the client.
*
* @return Response
* @param RequestInterface $request
* @return ResponseInterface
* @throws InfiniteLoopException
*/
public function handleRequest()
public function handleRequest(RequestInterface $request)
{
$request = $this->requestBuilder->build();
return $this->dispatcher->dispatch($request);
}

/**
* This request handler can handle a web request invoked by the backend.
*
* @param RequestInterface $request
* @return bool If we are in backend mode TRUE otherwise FALSE
*/
public function canHandleRequest()
public function canHandleRequest(RequestInterface $request)
{
return $this->environmentService->isEnvironmentInBackendMode() && !Environment::isCli();
}
Expand Down
13 changes: 9 additions & 4 deletions typo3/sysext/extbase/Classes/Mvc/Web/FrontendRequestHandler.php
Expand Up @@ -16,6 +16,9 @@
namespace TYPO3\CMS\Extbase\Mvc\Web;

use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Mvc\Exception\InfiniteLoopException;
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
use TYPO3\CMS\Extbase\Mvc\ResponseInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
Expand All @@ -40,11 +43,12 @@ public function injectConfigurationManager(ConfigurationManagerInterface $config
/**
* Handles the web request. The response will automatically be sent to the client.
*
* @return \TYPO3\CMS\Extbase\Mvc\ResponseInterface
* @param RequestInterface $request
* @return ResponseInterface
* @throws InfiniteLoopException
*/
public function handleRequest()
public function handleRequest(RequestInterface $request)
{
$request = $this->requestBuilder->build();
if ($this->isActionCacheable($request->getControllerObjectName(), $request->getControllerActionName())) {
$request->setIsCached(true);
} else {
Expand All @@ -63,9 +67,10 @@ public function handleRequest()
/**
* This request handler can handle any web request.
*
* @param RequestInterface $request
* @return bool If the request is a web request, TRUE otherwise FALSE
*/
public function canHandleRequest()
public function canHandleRequest(RequestInterface $request)
{
return $this->environmentService->isEnvironmentInFrontendMode();
}
Expand Down

0 comments on commit ce119a1

Please sign in to comment.