[+TASK] Extbase: Moved static cache utility class to a service
authorBastian Waidelich <bastian@typo3.org>
Fri, 18 Mar 2011 17:54:19 +0000 (18:54 +0100)
committerFelix Oertel <f@oer.tel>
Tue, 12 Apr 2011 19:05:31 +0000 (21:05 +0200)
Relates to: #13864

Change-Id: Ie5360a9e898d9173af7e5bcc567d98113f884421

typo3/sysext/extbase/Classes/MVC/Controller/ActionController.php
typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php
typo3/sysext/extbase/Classes/Service/CacheService.php [new file with mode: 0644]
typo3/sysext/extbase/Classes/Utility/Cache.php
typo3/sysext/extbase/Tests/Unit/Service/CacheServiceTest.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/Unit/Service/FlexFormServiceTest.php

index a5dbf07..b7a7da2 100644 (file)
@@ -41,6 +41,11 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll
        protected $reflectionService;
 
        /**
+        * @var Tx_Extbase_Service_CacheService
+        */
+       protected $cacheService;
+
+       /**
         * The current view, as resolved by resolveView()
         *
         * @var Tx_Extbase_MVC_View_ViewInterface
@@ -91,8 +96,6 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll
        protected $errorMethodName = 'errorAction';
 
        /**
-        * Injects the reflection service
-        *
         * @param Tx_Extbase_Reflection_Service $reflectionService
         * @return void
         */
@@ -101,6 +104,14 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll
        }
 
        /**
+        * @param Tx_Extbase_Service_CacheService $cacheService
+        * @return void
+        */
+       public function injectCacheService(Tx_Extbase_Service_CacheService $cacheService) {
+               $this->cacheService = $cacheService;
+       }
+
+       /**
         * Checks if the current request type is supported by the controller.
         *
         * If your controller only supports certain request types, either
@@ -457,7 +468,7 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll
                if (isset($extbaseSettings['persistence']['enableAutomaticCacheClearing']) && $extbaseSettings['persistence']['enableAutomaticCacheClearing'] === '1') {
                        if (isset($GLOBALS['TSFE'])) {
                                $pageUid = $GLOBALS['TSFE']->id;
-                               Tx_Extbase_Utility_Cache::clearPageCache(array($pageUid));
+                               $this->cacheService->clearPageCache(array($pageUid));
                        }
                }
        }
index f0cab31..ab68b16 100644 (file)
@@ -76,6 +76,11 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
        protected $configurationManager;
 
        /**
+        * @var Tx_Extbase_Service_CacheService
+        */
+       protected $cacheService;
+
+       /**
         * Constructor. takes the database handle from $GLOBALS['TYPO3_DB']
         */
        public function __construct() {
@@ -101,6 +106,14 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
        }
 
        /**
+        * @param Tx_Extbase_Service_CacheService $cacheService
+        * @return void
+        */
+       public function injectCacheService(Tx_Extbase_Service_CacheService $cacheService) {
+               $this->cacheService = $cacheService;
+       }
+
+       /**
         * Adds a row to the storage
         *
         * @param string $tableName The database table name
@@ -1042,7 +1055,7 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis
                }
 
                // TODO check if we can hand this over to the Dispatcher to clear the page only once, this will save around 10% time while inserting and updating
-               Tx_Extbase_Utility_Cache::clearPageCache($pageIdsToClear);
+               $this->cacheService->clearPageCache($pageIdsToClear);
        }
 }
 
diff --git a/typo3/sysext/extbase/Classes/Service/CacheService.php b/typo3/sysext/extbase/Classes/Service/CacheService.php
new file mode 100644 (file)
index 0000000..63e39bc
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2011 Extbase Team
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * Cache clearing helper functions
+ *
+ * @package Extbase
+ * @subpackage Service
+ */
+class Tx_Extbase_Service_CacheService implements t3lib_Singleton {
+
+       /**
+        * This is a wrapper for the global constant "TYPO3_UseCachingFramework" to make this class testable
+        * @var boolean
+        */
+       protected $useCachingFramework = TYPO3_UseCachingFramework;
+
+       /**
+        * Clears the page cache
+        *
+        * @param mixed $pageIdsToClear (int) single or (array) multiple pageIds to clear the cache for
+        * @return void
+        */
+       public function clearPageCache($pageIdsToClear = NULL) {
+               if ($pageIdsToClear !== NULL && !is_array($pageIdsToClear)) {
+                       $pageIdsToClear = array(intval($pageIdsToClear));
+               }
+
+               $this->flushPageCache($pageIdsToClear);
+               $this->flushPageSectionCache($pageIdsToClear);
+       }
+
+       /**
+        * Flushes cache_pages or cachingframework_cache_pages.
+        *
+        * @param array $pageIdsToClear pageIds to clear the cache for
+        * @return void
+        */
+       protected function flushPageCache($pageIds = NULL) {
+               if ($this->useCachingFramework) {
+                       $pageCache = $GLOBALS['typo3CacheManager']->getCache('cache_pages');
+
+                       if ($pageIds !== NULL) {
+                               foreach ($pageIds as $pageId) {
+                                       $pageCache->flushByTag('pageId_' . $pageId);
+                               }
+                       } else {
+                               $pageCache->flush();
+                       }
+               } elseif ($pageIds !== NULL) {
+                       $GLOBALS['TYPO3_DB']->exec_DELETEquery(
+                               'cache_pages',
+                               'page_id IN (' . implode(',', $GLOBALS['TYPO3_DB']->cleanIntArray($pageIds)) . ')'
+                       );
+               } else {
+                       $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery('cache_pages');
+               }
+       }
+
+       /**
+        * Flushes cache_pagesection or cachingframework_cache_pagesection.
+        *
+        * @param array $pageIdsToClear pageIds to clear the cache for
+        * @return void
+        */
+       protected function flushPageSectionCache($pageIds = NULL) {
+               if ($this->useCachingFramework) {
+                       $pageSectionCache = $GLOBALS['typo3CacheManager']->getCache('cache_pagesection');
+
+                       if ($pageIds !== NULL) {
+                               foreach ($pageIds as $pageId) {
+                                       $pageSectionCache->flushByTag('pageId_' . $pageId);
+                               }
+                       } else {
+                               $pageSectionCache->flush();
+                       }
+               } elseif ($pageIds !== NULL) {
+                       $GLOBALS['TYPO3_DB']->exec_DELETEquery(
+                               'cache_pagesection',
+                               'page_id IN (' . implode(',', $GLOBALS['TYPO3_DB']->cleanIntArray($pageIds)) . ')'
+                       );
+               } else {
+                       $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery('cache_pagesection');
+               }
+       }
+}
+?>
\ No newline at end of file
index 390d03c..1c1b70c 100644 (file)
@@ -30,7 +30,7 @@
  *
  * @package Extbase
  * @subpackage Utility
- * @version $Id$
+ * @deprecated since Extbase 1.4.0; will be removed in Extbase 1.6.0. Please use Tx_Extbase_Service_CacheService instead
  */
 class Tx_Extbase_Utility_Cache {
 
@@ -41,66 +41,10 @@ class Tx_Extbase_Utility_Cache {
         * @return void
         */
        static public function clearPageCache($pageIdsToClear = NULL) {
-               if ($pageIdsToClear !== NULL && !is_array($pageIdsToClear)) {
-                       $pageIdsToClear = array(intval($pageIdsToClear));
-               }
-
-               self::flushPageCache($pageIdsToClear);
-               self::flushPageSectionCache($pageIdsToClear);
-       }
-
-       /**
-        * Flushes cache_pages or cachinframework_cache_pages.
-        *
-        * @param array $pageIdsToClear pageIds to clear the cache for
-        * @return void
-        */
-       static protected function flushPageCache($pageIds = NULL) {
-               if (TYPO3_UseCachingFramework) {
-                       $pageCache = $GLOBALS['typo3CacheManager']->getCache('cache_pages');
-
-                       if ($pageIds !== NULL) {
-                               foreach ($pageIds as $pageId) {
-                                       $pageCache->flushByTag('pageId_' . $pageId);
-                               }
-                       } else {
-                               $pageCache->flush();
-                       }
-               } elseif ($pageIds !== NULL) {
-                       $GLOBALS['TYPO3_DB']->exec_DELETEquery(
-                               'cache_pages',
-                               'page_id IN (' . implode(',', $GLOBALS['TYPO3_DB']->cleanIntArray($pageIds)) . ')'
-                       );
-               } else {
-                       $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery('cache_pages');
-               }
-       }
-
-       /**
-        * Flushes cache_pagesection or cachingframework_cache_pagesection.
-        *
-        * @param array $pageIdsToClear pageIds to clear the cache for
-        * @return void
-        */
-       static protected function flushPageSectionCache($pageIds = NULL) {
-               if (TYPO3_UseCachingFramework) {
-                       $pageSectionCache = $GLOBALS['typo3CacheManager']->getCache('cache_pagesection');
-
-                       if ($pageIds !== NULL) {
-                               foreach ($pageIds as $pageId) {
-                                       $pageSectionCache->flushByTag('pageId_' . $pageId);
-                               }
-                       } else {
-                               $pageSectionCache->flush();
-                       }
-               } elseif ($pageIds !== NULL) {
-                       $GLOBALS['TYPO3_DB']->exec_DELETEquery(
-                               'cache_pagesection',
-                               'page_id IN (' . implode(',', $GLOBALS['TYPO3_DB']->cleanIntArray($pageIds)) . ')'
-                       );
-               } else {
-                       $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery('cache_pagesection');
-               }
+               t3lib_div::logDeprecatedFunction();
+               $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
+               $cacheService = $objectManager->get('Tx_Extbase_Service_CacheService');
+               $cacheService->clearPageCache($pageIdsToClear);
        }
 }
 ?>
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Tests/Unit/Service/CacheServiceTest.php b/typo3/sysext/extbase/Tests/Unit/Service/CacheServiceTest.php
new file mode 100644 (file)
index 0000000..d523ddf
--- /dev/null
@@ -0,0 +1,195 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2011 Extbase Team
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+
+/**
+ * Testcase for class Tx_Extbase_Service_CacheService
+ *
+ * @package Extbase
+ * @subpackage extbase
+ */
+
+class Tx_Extbase_Tests_Unit_Service_CacheServiceTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
+
+       /**
+        * @var Tx_Extbase_Service_CacheService
+        */
+       protected $cacheService;
+
+       /**
+        * @var t3lib_DB
+        */
+       protected $typo3DbBackup;
+
+       /**
+        * @var t3lib_cache_Manager
+        */
+       protected $cacheManagerBackup;
+
+       public function setUp() {
+               $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
+               $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB');
+
+               $this->cacheManagerBackup = $GLOBALS['typo3CacheManager'];
+               $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager');
+
+               $this->cacheService = $this->getAccessibleMock('Tx_Extbase_Service_CacheService', array('dummy'));
+       }
+
+       /**
+        * @test
+        */
+       public function clearPageCacheConvertsPageIdsToArray() {
+               $cacheService = $this->getMock('Tx_Extbase_Service_CacheService', array('flushPageCache', 'flushPageSectionCache'));
+               $cacheService->expects($this->once())->method('flushPageCache')->with(array(123));
+               $cacheService->expects($this->once())->method('flushPageSectionCache')->with(array(123));
+               $cacheService->clearPageCache(123);
+       }
+
+       /**
+        * @test
+        */
+       public function clearPageCacheConvertsPageIdsToNumericArray() {
+               $cacheService = $this->getMock('Tx_Extbase_Service_CacheService', array('flushPageCache', 'flushPageSectionCache'));
+               $cacheService->expects($this->once())->method('flushPageCache')->with(array(0));
+               $cacheService->expects($this->once())->method('flushPageSectionCache')->with(array(0));
+               $cacheService->clearPageCache('Foo');
+       }
+
+       /**
+        * @test
+        */
+       public function clearPageCacheDoesNotConvertPageIdsIfNoneAreSpecified() {
+               $cacheService = $this->getMock('Tx_Extbase_Service_CacheService', array('flushPageCache', 'flushPageSectionCache'));
+               $cacheService->expects($this->once())->method('flushPageCache')->with(NULL);
+               $cacheService->expects($this->once())->method('flushPageSectionCache')->with(NULL);
+               $cacheService->clearPageCache();
+       }
+
+       /**
+        * @test
+        */
+       public function flushPageCacheUsesCacheManagerToFlushCacheOfSpecifiedPagesIfCachingFrameworkIsEnabled() {
+               $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
+               $mockCacheFrontend->expects($this->at(0))->method('flushByTag')->with('pageId_1');
+               $mockCacheFrontend->expects($this->at(1))->method('flushByTag')->with('pageId_2');
+               $mockCacheFrontend->expects($this->at(2))->method('flushByTag')->with('pageId_3');
+
+               $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pages')->will($this->returnValue($mockCacheFrontend));
+
+               $this->cacheService->_set('useCachingFramework', TRUE);
+               $this->cacheService->_call('flushPageCache', array(1,2,3));
+       }
+
+       /**
+        * @test
+        */
+       public function flushPageCacheUsesCacheManagerToFlushCacheOfAllPagesIfCachingFrameworkIsEnabledAndPageIdsIsNull() {
+               $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
+               $mockCacheFrontend->expects($this->once())->method('flush');
+
+               $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pages')->will($this->returnValue($mockCacheFrontend));
+
+               $this->cacheService->_set('useCachingFramework', TRUE);
+               $this->cacheService->_call('flushPageCache');
+       }
+
+       /**
+        * @test
+        */
+       public function flushPageCacheFlushesCacheOfSpecifiedPagesDirectlyIfCachingFrameworkIsDisabled() {
+               $GLOBALS['TYPO3_DB']->expects($this->once())->method('cleanIntArray')->with(array(1,2,3))->will($this->returnValue(array(3,2,1)));
+               $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with(
+                       'cache_pages',
+                       'page_id IN (3,2,1)'
+               );
+
+               $this->cacheService->_set('useCachingFramework', FALSE);
+               $this->cacheService->_call('flushPageCache', array(1,2,3));
+       }
+
+       /**
+        * test
+        */
+       public function flushPageCacheFlushesCacheOfAllPagesDirectlyIfCachingFrameworkIsDisabled() {
+               $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_TRUNCATEquery')->with('cache_pages');
+
+               $this->cacheService->_set('useCachingFramework', FALSE);
+               $this->cacheService->_call('flushPageCache');
+       }
+
+       /**
+        * @test
+        */
+       public function flushPageSectionCacheUsesCacheManagerToFlushCacheOfSpecifiedPageSectionsIfCachingFrameworkIsEnabled() {
+               $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
+               $mockCacheFrontend->expects($this->at(0))->method('flushByTag')->with('pageId_1');
+               $mockCacheFrontend->expects($this->at(1))->method('flushByTag')->with('pageId_2');
+               $mockCacheFrontend->expects($this->at(2))->method('flushByTag')->with('pageId_3');
+
+               $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pagesection')->will($this->returnValue($mockCacheFrontend));
+
+               $this->cacheService->_set('useCachingFramework', TRUE);
+               $this->cacheService->_call('flushPageSectionCache', array(1,2,3));
+       }
+
+       /**
+        * @test
+        */
+       public function flushPageSectionCacheUsesCacheManagerToFlushCacheOfAllPageSectionsIfCachingFrameworkIsEnabledAndPageIdsIsNull() {
+               $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
+               $mockCacheFrontend->expects($this->once())->method('flush');
+
+               $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pagesection')->will($this->returnValue($mockCacheFrontend));
+
+               $this->cacheService->_set('useCachingFramework', TRUE);
+               $this->cacheService->_call('flushPageSectionCache');
+       }
+
+       /**
+        * @test
+        */
+       public function flushPageSectionCacheFlushesCacheOfSpecifiedPageSectionsDirectlyIfCachingFrameworkIsDisabled() {
+               $GLOBALS['TYPO3_DB']->expects($this->once())->method('cleanIntArray')->with(array(1,2,3))->will($this->returnValue(array(3,2,1)));
+               $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with(
+                       'cache_pagesection',
+                       'page_id IN (3,2,1)'
+               );
+
+               $this->cacheService->_set('useCachingFramework', FALSE);
+               $this->cacheService->_call('flushPageSectionCache', array(1,2,3));
+       }
+
+       /**
+        * test
+        */
+       public function flushPageSectionCacheCacheFlushesCacheOfAllPageSectionsDirectlyIfCachingFrameworkIsDisabled() {
+               $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_TRUNCATEquery')->with('cache_pagesection');
+
+               $this->cacheService->_set('useCachingFramework', FALSE);
+               $this->cacheService->_call('flushPageSectionCache');
+       }
+
+}
+?>
\ No newline at end of file
index ff990eb..9d15510 100644 (file)
@@ -30,7 +30,7 @@
  * @subpackage extbase
  */
 
-class Tx_Extbase_Tests_Unit_Service_FlexFormServiceTest extends tx_phpunit_testcase {
+class Tx_Extbase_Tests_Unit_Service_FlexFormServiceTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
 
        /**
         * @var Tx_Extbase_Service_FlexFormService