CacheManager.php 2.4 KB

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