[BUGFIX] Call RequestHandlerResolver also for CLI requests
authorAlexander Schnitzler <alex.schnitzler@typovision.de>
Sun, 6 Jan 2013 11:53:04 +0000 (12:53 +0100)
committerAndreas Wolf <andreas.wolf@typo3.org>
Sat, 13 Apr 2013 18:12:15 +0000 (20:12 +0200)
Currently the bootstrap does not call the RequestHandlerResolver
for CLI requests but instantiates RequestHandler and Response
on its own.

Fixes: #39140
Releases: 6.1
Change-Id: I37a9b31c74f04cadb3a2f99c15f08a4425428de5
Reviewed-on: https://review.typo3.org/17393
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Andreas Wolf
Tested-by: Andreas Wolf
typo3/sysext/extbase/Classes/Core/Bootstrap.php
typo3/sysext/extbase/Classes/Mvc/Cli/RequestHandler.php
typo3/sysext/extbase/Classes/Mvc/Cli/Response.php
typo3/sysext/extbase/Classes/Mvc/Response.php
typo3/sysext/extbase/Classes/Mvc/ResponseInterface.php
typo3/sysext/extbase/Classes/Mvc/Web/BackendRequestHandler.php
typo3/sysext/extbase/Classes/Mvc/Web/Response.php
typo3/sysext/extbase/Classes/Service/EnvironmentService.php
typo3/sysext/extbase/Tests/Unit/Core/BootstrapTest.php

index b9332c2..b11f354 100644 (file)
@@ -88,7 +88,7 @@ class Bootstrap implements \TYPO3\CMS\Extbase\Core\BootstrapInterface {
         * @api
         */
        public function initialize($configuration) {
-               if (!defined('TYPO3_cliMode') || TYPO3_cliMode !== TRUE) {
+               if (!$this->isInCliMode()) {
                        if (!isset($configuration['extensionName']) || strlen($configuration['extensionName']) === 0) {
                                throw new \RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
                        }
@@ -194,52 +194,30 @@ class Bootstrap implements \TYPO3\CMS\Extbase\Core\BootstrapInterface {
         */
        public function run($content, $configuration) {
                $this->initialize($configuration);
-               // CLI
-               if (defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE) {
-                       $content = $this->handleCommandLineRequest();
-               } else {
-                       $content = $this->handleWebRequest();
-               }
-
-               $this->objectManager->get('TYPO3\CMS\Extbase\Service\CacheService')->clearCachesOfRegisteredPageIds();
-               return $content;
-       }
-
-       /**
-        * @return string
-        */
-       protected function handleCommandLineRequest() {
-               $commandLine = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
-               $request = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\RequestBuilder')->build(array_slice($commandLine, 1));
-               $response = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
-               $extensionName = $request->getControllerExtensionName();
-               $this->configurationManager->setConfiguration(array('extensionName' => $extensionName));
-               $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Dispatcher')->dispatch($request, $response);
-               $content = $response->getContent();
-               $this->resetSingletons();
-               return $content;
+               return $this->handleRequest();
        }
 
        /**
         * @return string
         */
-       protected function handleWebRequest() {
+       protected function handleRequest() {
+               /** @var $requestHandlerResolver \TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver */
                $requestHandlerResolver = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\RequestHandlerResolver');
                $requestHandler = $requestHandlerResolver->resolveRequestHandler();
+
                $response = $requestHandler->handleRequest();
                // 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()
                if ($response === NULL) {
                        $this->reflectionService->shutdown();
-                       return '';
-               }
-               if (count($response->getAdditionalHeaderData()) > 0) {
-                       $GLOBALS['TSFE']->additionalHeaderData[] = implode(chr(10), $response->getAdditionalHeaderData());
+                       $content = '';
+               } else {
+                       $content = $response->shutdown();
+                       $this->resetSingletons();
+                       $this->objectManager->get('TYPO3\CMS\Extbase\Service\CacheService')->clearCachesOfRegisteredPageIds();
                }
-               $response->sendHeaders();
-               $content = $response->getContent();
-               $this->resetSingletons();
+
                return $content;
        }
 
@@ -252,6 +230,13 @@ class Bootstrap implements \TYPO3\CMS\Extbase\Core\BootstrapInterface {
                $this->persistenceManager->persistAll();
                $this->reflectionService->shutdown();
        }
+
+       /**
+        * @return boolean
+        */
+       protected function isInCliMode() {
+               return (defined('TYPO3_cliMode') && TYPO3_cliMode);
+       }
 }
 
-?>
+?>
\ No newline at end of file
index 608ab98..5ed3742 100644 (file)
@@ -52,6 +52,11 @@ class RequestHandler implements \TYPO3\CMS\Extbase\Mvc\RequestHandlerInterface {
        protected $flashMessageContainer;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
+        */
+       protected $environmentService;
+
+       /**
         * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
         * @return void
         */
@@ -84,12 +89,21 @@ class RequestHandler implements \TYPO3\CMS\Extbase\Mvc\RequestHandlerInterface {
        }
 
        /**
+        * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
+        * @return void
+        */
+       public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService) {
+               $this->environmentService = $environmentService;
+       }
+
+       /**
         * Handles the request
         *
         * @return void
         */
        public function handleRequest() {
-               $request = $this->requestBuilder->build();
+               $commandLine = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
+               $request = $this->requestBuilder->build(array_slice($commandLine, 1));
                /** @var $response \TYPO3\CMS\Extbase\Mvc\Cli\Response */
                $response = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
                $this->dispatcher->dispatch($request, $response);
@@ -102,7 +116,7 @@ class RequestHandler implements \TYPO3\CMS\Extbase\Mvc\RequestHandlerInterface {
         * @return boolean If the request is a command line request, TRUE otherwise FALSE
         */
        public function canHandleRequest() {
-               return PHP_SAPI === 'cli';
+               return $this->environmentService->isEnvironmentInCliMode();
        }
 
        /**
@@ -112,7 +126,7 @@ class RequestHandler implements \TYPO3\CMS\Extbase\Mvc\RequestHandlerInterface {
         * @return integer The priority of the request handler.
         */
        public function getPriority() {
-               return 90;
+               return 100;
        }
 }
 
index 9ef48ec..1738c9d 100644 (file)
@@ -69,7 +69,7 @@ class Response extends \TYPO3\CMS\Extbase\Mvc\Response {
         */
        public function send() {
                if ($this->content !== NULL) {
-                       echo $this->getContent();
+                       echo $this->shutdown();
                }
        }
 }
index bdbd8ba..91cf98c 100644 (file)
@@ -72,6 +72,18 @@ class Response implements \TYPO3\CMS\Extbase\Mvc\ResponseInterface {
        }
 
        /**
+        * Fetches the content, returns and clears it.
+        *
+        * @return string
+        * @api
+        */
+       public function shutdown() {
+               $content = $this->getContent();
+               $this->setContent('');
+               return $content;
+       }
+
+       /**
         * Returns the content of the response.
         *
         * @return string
index 89081b2..3e1344f 100644 (file)
@@ -59,6 +59,14 @@ interface ResponseInterface {
         * @api
         */
        public function getContent();
+
+       /**
+        * Returns the response content without sending it.
+        *
+        * @return string The response content
+        * @api
+        */
+       public function shutdown();
 }
 
 ?>
\ No newline at end of file
index d85d854..44267c7 100644 (file)
@@ -54,7 +54,7 @@ class BackendRequestHandler extends \TYPO3\CMS\Extbase\Mvc\Web\AbstractRequestHa
         * @return boolean If we are in backend mode TRUE otherwise FALSE
         */
        public function canHandleRequest() {
-               return $this->environmentService->isEnvironmentInBackendMode();
+               return $this->environmentService->isEnvironmentInBackendMode() && !$this->environmentService->isEnvironmentInCliMode();
        }
 }
 
index 0971349..67c9de6 100644 (file)
@@ -288,6 +288,19 @@ class Response extends \TYPO3\CMS\Extbase\Mvc\Response {
        public function getRequest() {
                return $this->request;
        }
+
+       /**
+        * Sends additional headers and returns the content
+        *
+        * @return null|string
+        */
+       public function shutdown() {
+               if (count($this->getAdditionalHeaderData()) > 0) {
+                       $GLOBALS['TSFE']->additionalHeaderData[] = implode(chr(10), $this->getAdditionalHeaderData());
+               }
+               $this->sendHeaders();
+               return parent::shutdown();
+       }
 }
 
 ?>
\ No newline at end of file
index 60bc9ef..602f98d 100644 (file)
@@ -51,6 +51,15 @@ class EnvironmentService {
        }
 
        /**
+        * Detects if we are running a script from the command line.
+        *
+        * @return boolean
+        */
+       public function isEnvironmentInCliMode() {
+               return $this->isEnvironmentInBackendMode() && defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE;
+       }
+
+       /**
         * @return string
         */
        public function getServerRequestMethod() {
index cd28cdf..21808eb 100644 (file)
@@ -47,12 +47,12 @@ class BootstrapTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
        }
 
        public function tearDown() {
+               \TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances();
                \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
        }
 
        /**
         * @test
-        * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
         */
        public function configureObjectManagerRespectsOverridingOfAlternativeObjectRegistrationViaPluginConfiguration() {
                /** @var $objectContainer \TYPO3\CMS\Extbase\Object\Container\Container|\PHPUnit_Framework_MockObject_MockObject */
@@ -76,6 +76,37 @@ class BootstrapTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $bootstrapMock->_set('configurationManager', $configurationManagerMock);
                $bootstrapMock->configureObjectManager();
        }
+
+       /**
+        * @test
+        */
+       public function cliRequestHandlerIsFetchedByRequestHandlerResolver() {
+               /** @var $requestHandlerResolver \TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver|\PHPUnit_Framework_MockObject_MockObject */
+               $requestHandlerResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\RequestHandlerResolver', array('resolveRequestHandler'));
+
+               /** @var $cliRequestHandler \TYPO3\CMS\Extbase\Mvc\Cli\RequestHandler|\PHPUnit_Framework_MockObject_MockObject */
+               $cliRequestHandler = $this->getMock('TYPO3\CMS\Extbase\Mvc\Cli\RequestHandler', array('canHandleRequest'));
+               $cliRequestHandler->expects($this->any())->method('canHandleRequest')->will($this->returnValue(TRUE));
+               $cliRequestHandler->injectObjectManager($this->objectManager);
+               $cliRequestHandler->injectRequestBuilder($this->objectManager->get('TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder'));
+               $cliRequestHandler->injectDispatcher($this->objectManager->get('TYPO3\CMS\Extbase\Mvc\Dispatcher'));
+               $cliRequestHandler->injectFlashMessageContainer($this->objectManager->get('TYPO3\CMS\Extbase\Mvc\Controller\FlashMessageContainer'));
+
+               /** @var $cliResponse \TYPO3\CMS\Extbase\Mvc\Cli\Response */
+               $cliResponse = $this->getMock('TYPO3\CMS\Extbase\Mvc\Cli\Response', array('send'));
+
+               /** @var $bootstrap \TYPO3\CMS\Extbase\Core\Bootstrap |\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
+               $bootstrap = $this->getAccessibleMock('TYPO3\CMS\Extbase\Core\Bootstrap', array('isInCliMode'));
+               $bootstrap->expects($this->once())->method('isInCliMode')->will($this->returnValue(TRUE));
+
+               $requestHandlerResolver->expects($this->once())->method('resolveRequestHandler')->will($this->returnValue($cliRequestHandler));
+
+               \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance('TYPO3\\CMS\\Extbase\\Mvc\\RequestHandlerResolver', $requestHandlerResolver);
+               \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance('TYPO3\CMS\Extbase\Mvc\Cli\RequestHandler', $cliRequestHandler);
+               \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance('TYPO3\CMS\Extbase\Mvc\Cli\Response', $cliResponse);
+
+               $bootstrap->run('', array());
+       }
 }
 
 ?>
\ No newline at end of file