[FEATURE] Use EnvironmentService to detect TYPO3_MODE in several places
authorAlexander Schnitzler <alex.schnitzler@typovision.de>
Mon, 18 Mar 2013 09:48:47 +0000 (10:48 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 23 Mar 2013 14:58:27 +0000 (15:58 +0100)
In several places the TYPO3_MODE is still detected by
checking the TYPO3_MODE constant directly. This patch
replaces the checks by using the EnvironmentService.

Releases: 6.1
Resolves: #46396
Change-Id: I2f58e1ca175a86845beb053440428cc42f093847
Reviewed-on: https://review.typo3.org/19006
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/extbase/Classes/Configuration/AbstractConfigurationManager.php
typo3/sysext/extbase/Classes/Configuration/ConfigurationManager.php
typo3/sysext/extbase/Classes/Mvc/Web/AbstractRequestHandler.php
typo3/sysext/extbase/Classes/Mvc/Web/BackendRequestHandler.php
typo3/sysext/extbase/Classes/Mvc/Web/FrontendRequestHandler.php
typo3/sysext/extbase/Classes/Mvc/Web/Request.php
typo3/sysext/extbase/Classes/Mvc/Web/Response.php
typo3/sysext/extbase/Classes/Mvc/Web/Routing/UriBuilder.php
typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php
typo3/sysext/extbase/Tests/Unit/Mvc/Web/Routing/UriBuilderTest.php
typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Storage/Typo3DbBackendTest.php

index 016a4c4..3478919 100755 (executable)
@@ -81,6 +81,11 @@ abstract class AbstractConfigurationManager implements \TYPO3\CMS\Core\Singleton
        protected $configurationCache = array();
 
        /**
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
+        */
+       protected $environmentService;
+
+       /**
         * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
         * @return void
         */
@@ -97,6 +102,14 @@ abstract class AbstractConfigurationManager implements \TYPO3\CMS\Core\Singleton
        }
 
        /**
+        * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
+        * @return void
+        */
+       public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService) {
+               $this->environmentService = $environmentService;
+       }
+
+       /**
         * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject
         * @return void
         */
@@ -174,12 +187,12 @@ abstract class AbstractConfigurationManager implements \TYPO3\CMS\Core\Singleton
                                        * stdWrap. Than we convert the configuration to normal TypoScript
                                        * and apply the stdWrap to the storagePid
                                        */
-                               if (TYPO3_MODE !== 'FE') {
+                               if (!$this->environmentService->isEnvironmentInFrontendMode()) {
                                        \TYPO3\CMS\Extbase\Utility\FrontendSimulatorUtility::simulateFrontendEnvironment($this->getContentObject());
                                }
                                $conf = $this->typoScriptService->convertPlainArrayToTypoScriptArray($frameworkConfiguration['persistence']);
                                $frameworkConfiguration['persistence']['storagePid'] = $GLOBALS['TSFE']->cObj->stdWrap($conf['storagePid'], $conf['storagePid.']);
-                               if (TYPO3_MODE !== 'FE') {
+                               if (!$this->environmentService->isEnvironmentInFrontendMode()) {
                                        \TYPO3\CMS\Extbase\Utility\FrontendSimulatorUtility::resetFrontendEnvironment();
                                }
                        }
index e32a311..b7bc7df 100644 (file)
@@ -46,11 +46,32 @@ class ConfigurationManager implements \TYPO3\CMS\Extbase\Configuration\Configura
        protected $concreteConfigurationManager;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
+        */
+       protected $environmentService;
+
+       /**
         * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
         * @return void
         */
        public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
                $this->objectManager = $objectManager;
+       }
+
+       /**
+        * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
+        * @return void
+        */
+       public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService) {
+               $this->environmentService = $environmentService;
+       }
+
+       /**
+        * Initializes the object
+        *
+        * @return void
+        */
+       public function initializeObject() {
                $this->initializeConcreteConfigurationManager();
        }
 
@@ -58,7 +79,7 @@ class ConfigurationManager implements \TYPO3\CMS\Extbase\Configuration\Configura
         * @return void
         */
        protected function initializeConcreteConfigurationManager() {
-               if (TYPO3_MODE === 'FE') {
+               if ($this->environmentService->isEnvironmentInFrontendMode()) {
                        $this->concreteConfigurationManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\FrontendConfigurationManager');
                } else {
                        $this->concreteConfigurationManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\BackendConfigurationManager');
index 0c9bce6..851449e 100644 (file)
@@ -53,6 +53,11 @@ abstract class AbstractRequestHandler implements \TYPO3\CMS\Extbase\Mvc\RequestH
        protected $flashMessageContainer;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
+        */
+       protected $environmentService;
+
+       /**
         * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
         * @return void
         */
@@ -85,6 +90,14 @@ abstract class AbstractRequestHandler implements \TYPO3\CMS\Extbase\Mvc\RequestH
        }
 
        /**
+        * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
+        * @return void
+        */
+       public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService) {
+               $this->environmentService = $environmentService;
+       }
+
+       /**
         * This request handler can handle any web request.
         *
         * @return boolean If the request is a web request, TRUE otherwise FALSE
index 920a37a..2b240e8 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 TYPO3_MODE === 'BE';
+               return $this->environmentService->isEnvironmentInBackendMode();
        }
 }
 
index e6e23fc..5051c15 100644 (file)
@@ -91,7 +91,7 @@ class FrontendRequestHandler extends \TYPO3\CMS\Extbase\Mvc\Web\AbstractRequestH
         * @return boolean If the request is a web request, TRUE otherwise FALSE
         */
        public function canHandleRequest() {
-               return TYPO3_MODE === 'FE';
+               return $this->environmentService->isEnvironmentInFrontendMode();
        }
 }
 
index 0fdb712..f866d77 100644 (file)
@@ -76,6 +76,11 @@ class Request extends \TYPO3\CMS\Extbase\Mvc\Request {
        protected $configurationManager;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
+        */
+       protected $environmentService;
+
+       /**
         * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService
         */
        public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) {
@@ -91,6 +96,15 @@ class Request extends \TYPO3\CMS\Extbase\Mvc\Request {
        }
 
        /**
+        * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
+        *
+        * @return void
+        */
+       public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService) {
+               $this->environmentService = $environmentService;
+       }
+
+       /**
         * Sets the request method
         *
         * @param string $method Name of the request method
@@ -151,7 +165,7 @@ class Request extends \TYPO3\CMS\Extbase\Mvc\Request {
         * @api
         */
        public function getBaseUri() {
-               if (TYPO3_MODE === 'BE') {
+               if ($this->environmentService->isEnvironmentInBackendMode()) {
                        return $this->baseUri . TYPO3_mainDir;
                } else {
                        return $this->baseUri;
index 61039f6..0971349 100644 (file)
@@ -78,7 +78,7 @@ class Response extends \TYPO3\CMS\Extbase\Mvc\Response {
                100 => 'Continue',
                101 => 'Switching Protocols',
                102 => 'Processing',
-               # RFC 2518
+               // RFC 2518
                200 => 'OK',
                201 => 'Created',
                202 => 'Accepted',
@@ -123,6 +123,20 @@ class Response extends \TYPO3\CMS\Extbase\Mvc\Response {
        );
 
        /**
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
+        */
+       protected $environmentService;
+
+       /**
+        * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
+        *
+        * @return void
+        */
+       public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService) {
+               $this->environmentService = $environmentService;
+       }
+
+       /**
         * Sets the HTTP status code and (optionally) a customized message.
         *
         * @param integer $code The status code
@@ -240,9 +254,9 @@ class Response extends \TYPO3\CMS\Extbase\Mvc\Response {
                        throw new \InvalidArgumentException('The additiona header data must be of type String, ' . gettype($additionalHeaderData) . ' given.', 1237370877);
                }
                if ($this->request->isCached()) {
-                       if (TYPO3_MODE === 'FE') {
+                       if ($this->environmentService->isEnvironmentInFrontendMode()) {
                                $pageRenderer = $GLOBALS['TSFE']->getPageRenderer();
-                       } elseif (TYPO3_MODE === 'BE') {
+                       } elseif ($this->environmentService->isEnvironmentInBackendMode()) {
                                $pageRenderer = $GLOBALS['TBE_TEMPLATE']->getPageRenderer();
                        }
                        $pageRenderer->addHeaderData($additionalHeaderData);
index 39d9600..e81c396 100644 (file)
@@ -115,6 +115,11 @@ class UriBuilder {
        protected $argumentPrefix = NULL;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
+        */
+       protected $environmentService;
+
+       /**
         * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
         * @return void
         */
@@ -131,6 +136,14 @@ class UriBuilder {
        }
 
        /**
+        * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
+        * @return void
+        */
+       public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService) {
+               $this->environmentService = $environmentService;
+       }
+
+       /**
         * Life-cycle method that is called by the DI container as soon as this object is completely built
         *
         * @return void
@@ -480,16 +493,16 @@ class UriBuilder {
                if ($extensionName === NULL) {
                        $extensionName = $this->request->getControllerExtensionName();
                }
-               if ($pluginName === NULL && TYPO3_MODE === 'FE') {
+               if ($pluginName === NULL && $this->environmentService->isEnvironmentInFrontendMode()) {
                        $pluginName = $this->extensionService->getPluginNameByAction($extensionName, $controllerArguments['controller'], $controllerArguments['action']);
                }
                if ($pluginName === NULL) {
                        $pluginName = $this->request->getPluginName();
                }
-               if (TYPO3_MODE === 'FE' && $this->configurationManager->isFeatureEnabled('skipDefaultArguments')) {
+               if ($this->environmentService->isEnvironmentInFrontendMode() && $this->configurationManager->isFeatureEnabled('skipDefaultArguments')) {
                        $controllerArguments = $this->removeDefaultControllerAndAction($controllerArguments, $extensionName, $pluginName);
                }
-               if ($this->targetPageUid === NULL && TYPO3_MODE === 'FE') {
+               if ($this->targetPageUid === NULL && $this->environmentService->isEnvironmentInFrontendMode()) {
                        $this->targetPageUid = $this->extensionService->getTargetPidByPlugin($extensionName, $pluginName);
                }
                if ($this->format !== '') {
@@ -540,7 +553,7 @@ class UriBuilder {
         * @see buildFrontendUri()
         */
        public function build() {
-               if (TYPO3_MODE === 'BE') {
+               if ($this->environmentService->isEnvironmentInBackendMode()) {
                        return $this->buildBackendUri();
                } else {
                        return $this->buildFrontendUri();
index 6cf6e6a..5fd4a77 100644 (file)
@@ -82,6 +82,11 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
        protected $tableColumnCache;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
+        */
+       protected $environmentService;
+
+       /**
         * Constructor. takes the database handle from $GLOBALS['TYPO3_DB']
         */
        public function __construct() {
@@ -131,6 +136,14 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
        }
 
        /**
+        * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
+        * @return void
+        */
+       public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService) {
+               $this->environmentService = $environmentService;
+       }
+
+       /**
         * Adds a row to the storage
         *
         * @param string $tableName The database table name
@@ -851,7 +864,7 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
        protected function addEnableFieldsStatement($tableName, array &$sql) {
                \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
                if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
-                       if ($this->getTypo3Mode() === 'FE') {
+                       if ($this->environmentService->isEnvironmentInFrontendMode()) {
                                $statement = $this->getPageRepository()->enableFields($tableName);
                        } else {
                                // TYPO3_MODE === 'BE'
@@ -879,7 +892,7 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
                        $ignoreEnableFields = $querySettings->getIgnoreEnableFields();
                        $enableFieldsToBeIgnored = $querySettings->getEnableFieldsToBeIgnored();
                        $includeDeleted = $querySettings->getIncludeDeleted();
-                       if ($this->getTypo3Mode() === 'FE') {
+                       if ($this->environmentService->isEnvironmentInFrontendMode()) {
                                $statement .= $this->getFrontendConstraintStatement($tableName, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted);
                        } else {
                                // TYPO3_MODE === 'BE'
@@ -1152,7 +1165,7 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
         */
        protected function getPageRepository() {
                if (!$this->pageRepository instanceof \TYPO3\CMS\Frontend\Page\PageRepository) {
-                       if ($this->getTypo3Mode() === 'FE' && is_object($GLOBALS['TSFE'])) {
+                       if ($this->environmentService->isEnvironmentInFrontendMode() && is_object($GLOBALS['TSFE'])) {
                                $this->pageRepository = $GLOBALS['TSFE']->sys_page;
                        } else {
                                $this->pageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
@@ -1229,16 +1242,6 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
                        $this->cacheService->getPageIdStack()->push($pageIdToClear);
                }
        }
-
-       /**
-        * Returns the TYPO3 Mode ("FE" for front-end or "BE" for back-end). This method is necessary to enable unit tests to
-        * mock this constant.
-        *
-        * @return string
-        */
-       protected function getTypo3Mode() {
-               return TYPO3_MODE;
-       }
 }
 
 ?>
\ No newline at end of file
index dde9fbf..938d92d 100644 (file)
@@ -80,6 +80,7 @@ class UriBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $this->uriBuilder->_set('contentObject', $this->mockContentObject);
                $this->uriBuilder->injectConfigurationManager($this->mockConfigurationManager);
                $this->uriBuilder->injectExtensionService($this->mockExtensionService);
+               $this->uriBuilder->injectEnvironmentService($this->objectManager->get('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService'));
        }
 
        public function tearDown() {
index 0e86fd6..085d589 100644 (file)
@@ -329,8 +329,14 @@ class Typo3DbBackendTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue($enableFieldsToBeIgnored));
                $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue($deletedValue));
                $sql = array();
-               $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('getTypo3Mode'), array(), '', FALSE);
-               $mockTypo3DbBackend->expects($this->any())->method('getTypo3Mode')->will($this->returnValue($mode));
+
+               /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
+               $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
+               $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
+
+               /** @var $mockTypo3DbBackend \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend | \PHPUnit_Framework_MockObject_MockObject */
+               $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
+               $mockTypo3DbBackend->injectEnvironmentService($mockEnvironmentService);
                $mockTypo3DbBackend->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
                $this->assertSame($expectedSql, $sql['additionalWhereClause']);
                unset($GLOBALS['TCA'][$tableName]);
@@ -364,8 +370,14 @@ class Typo3DbBackendTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('dummy'), array(), '', FALSE);
                $mockQuerySettings->setRespectEnableFields($respectEnableFields);
                $sql = array();
-               $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('getTypo3Mode'), array(), '', FALSE);
-               $mockTypo3DbBackend->expects($this->any())->method('getTypo3Mode')->will($this->returnValue($mode));
+
+               /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
+               $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
+               $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
+
+               /** @var $mockTypo3DbBackend \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend | \PHPUnit_Framework_MockObject_MockObject */
+               $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
+               $mockTypo3DbBackend->injectEnvironmentService($mockEnvironmentService);
                $mockTypo3DbBackend->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
                $this->assertSame($expectedSql, $sql['additionalWhereClause']);
                unset($GLOBALS['TCA'][$tableName]);
@@ -388,8 +400,14 @@ class Typo3DbBackendTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue(array()));
                $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue(TRUE));
                $sql = array();
-               $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('getTypo3Mode'), array(), '', FALSE);
-               $mockTypo3DbBackend->expects($this->any())->method('getTypo3Mode')->will($this->returnValue('FE'));
+
+               /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
+               $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
+               $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue(TRUE));
+
+               /** @var $mockTypo3DbBackend \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend | \PHPUnit_Framework_MockObject_MockObject */
+               $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
+               $mockTypo3DbBackend->injectEnvironmentService($mockEnvironmentService);
                $mockTypo3DbBackend->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
                unset($GLOBALS['TCA'][$tableName]);
        }