OtCacheManager.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Opentalent\OtTemplating\Utility;
  3. use Opentalent\OtTemplating\Page\OtPageRepository;
  4. use Psr\Http\Message\ResponseInterface;
  5. use RuntimeException;
  6. use TYPO3\CMS\Core\Service\OpcodeCacheService;
  7. use TYPO3\CMS\Core\Utility\GeneralUtility;
  8. use TYPO3\CMS\Install\Service\ClearCacheService;
  9. use TYPO3\CMS\Install\Service\Typo3tempFileService;
  10. use TYPO3\CMS\Core\Cache\CacheManager;
  11. class OtCacheManager
  12. {
  13. /**
  14. * Clears the page cache
  15. *
  16. * @param int $pageUid
  17. */
  18. public static function clearPageCache(int $pageUid) {
  19. $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
  20. $cacheManager->flushCachesInGroupByTag(
  21. 'pages',
  22. 'pageId_' . $pageUid
  23. );
  24. }
  25. /**
  26. * Clears cache for each page of the site
  27. *
  28. * @param int $pageUid
  29. */
  30. public static function clearSiteCache(int $pageUid) {
  31. $pageRepository = new OtPageRepository();
  32. $rootPage = $pageRepository->getRootPageFor($pageUid);
  33. $pages = $pageRepository->getAllSubpagesForPage($rootPage['uid']);
  34. $pages[] = $rootPage;
  35. $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
  36. $tags = array_map(function ($page) {
  37. return 'pageId_' . $page['uid'];
  38. }, $pages);
  39. $cacheManager->flushCachesInGroupByTags('pages', $tags);
  40. }
  41. /**
  42. * Clear cache framework and opcode caches
  43. * (@see TYPO3\CMS\Install\Controller\MaintenanceController )
  44. */
  45. public static function clearAllCache(): ResponseInterface
  46. {
  47. GeneralUtility::makeInstance(ClearCacheService::class)->clearAll();
  48. GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive();
  49. }
  50. /**
  51. * Clear typo3temp/assets
  52. * (@see TYPO3\CMS\Install\Controller\MaintenanceController )
  53. */
  54. public static function clearAssetsTempfiles()
  55. {
  56. $typo3tempFileService = GeneralUtility::makeInstance(Typo3tempFileService::class);
  57. try {
  58. $typo3tempFileService->clearAssetsFolder("/typo3temp/assets/css");
  59. } catch (RuntimeException $e) {
  60. // 1501781454 code means directory does not exist, we can ignore this error
  61. if ($e->getCode() != 1501781454) {
  62. throw $e;
  63. }
  64. }
  65. try {
  66. $typo3tempFileService->clearAssetsFolder("/typo3temp/assets/js");
  67. } catch (RuntimeException $e) {
  68. // 1501781454 code means directory does not exist, we can ignore this error
  69. if ($e->getCode() != 1501781454) {
  70. throw $e;
  71. }
  72. }
  73. }
  74. }