MassDisable.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Backend\Controller\Adminhtml\Cache;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Controller\ResultFactory;
  10. use Magento\Framework\App\State;
  11. use Magento\Framework\App\ObjectManager;
  12. /**
  13. * Controller disables some types of cache
  14. */
  15. class MassDisable extends \Magento\Backend\Controller\Adminhtml\Cache
  16. {
  17. /**
  18. * Authorization level of a basic admin session
  19. *
  20. * @see _isAllowed()
  21. */
  22. const ADMIN_RESOURCE = 'Magento_Backend::toggling_cache_type';
  23. /**
  24. * @var State
  25. */
  26. private $state;
  27. /**
  28. * Mass action for cache disabling
  29. *
  30. * @return \Magento\Backend\Model\View\Result\Redirect
  31. */
  32. public function execute()
  33. {
  34. if ($this->getState()->getMode() === State::MODE_PRODUCTION) {
  35. $this->messageManager->addErrorMessage(__('You can\'t change status of cache type(s) in production mode'));
  36. } else {
  37. $this->disableCache();
  38. }
  39. return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('adminhtml/*');
  40. }
  41. /**
  42. * Disable cache
  43. *
  44. * @return void
  45. */
  46. private function disableCache()
  47. {
  48. try {
  49. $types = $this->getRequest()->getParam('types');
  50. $updatedTypes = 0;
  51. if (!is_array($types)) {
  52. $types = [];
  53. }
  54. $this->_validateTypes($types);
  55. foreach ($types as $code) {
  56. $this->_cacheTypeList->cleanType($code);
  57. if ($this->_cacheState->isEnabled($code)) {
  58. $this->_cacheState->setEnabled($code, false);
  59. $updatedTypes++;
  60. }
  61. }
  62. if ($updatedTypes > 0) {
  63. $this->_cacheState->persist();
  64. $this->messageManager->addSuccessMessage(__("%1 cache type(s) disabled.", $updatedTypes));
  65. }
  66. } catch (LocalizedException $e) {
  67. $this->messageManager->addErrorMessage($e->getMessage());
  68. } catch (\Exception $e) {
  69. $this->messageManager->addExceptionMessage($e, __('An error occurred while disabling cache.'));
  70. }
  71. }
  72. /**
  73. * Get State Instance
  74. *
  75. * @return State
  76. * @deprecated 100.2.0
  77. */
  78. private function getState()
  79. {
  80. if ($this->state === null) {
  81. $this->state = ObjectManager::getInstance()->get(State::class);
  82. }
  83. return $this->state;
  84. }
  85. }