MassEnable.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 enables some types of cache
  14. */
  15. class MassEnable 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 enabling
  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->enableCache();
  38. }
  39. return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('adminhtml/*');
  40. }
  41. /**
  42. * Enable cache
  43. *
  44. * @return void
  45. */
  46. private function enableCache()
  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. if (!$this->_cacheState->isEnabled($code)) {
  57. $this->_cacheState->setEnabled($code, true);
  58. $updatedTypes++;
  59. }
  60. }
  61. if ($updatedTypes > 0) {
  62. $this->_cacheState->persist();
  63. $this->messageManager->addSuccessMessage(__("%1 cache type(s) enabled.", $updatedTypes));
  64. }
  65. } catch (LocalizedException $e) {
  66. $this->messageManager->addErrorMessage($e->getMessage());
  67. } catch (\Exception $e) {
  68. $this->messageManager->addExceptionMessage($e, __('An error occurred while enabling cache.'));
  69. }
  70. }
  71. /**
  72. * Get State Instance
  73. *
  74. * @return State
  75. * @deprecated 100.2.0
  76. */
  77. private function getState()
  78. {
  79. if ($this->state === null) {
  80. $this->state = ObjectManager::getInstance()->get(State::class);
  81. }
  82. return $this->state;
  83. }
  84. }