Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassDisable.php
Go to the documentation of this file.
1 <?php
8 
13 
18 {
24  const ADMIN_RESOURCE = 'Magento_Backend::toggling_cache_type';
25 
29  private $state;
30 
36  public function execute()
37  {
38  if ($this->getState()->getMode() === State::MODE_PRODUCTION) {
39  $this->messageManager->addErrorMessage(__('You can\'t change status of cache type(s) in production mode'));
40  } else {
41  $this->disableCache();
42  }
43 
44  return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('adminhtml/*');
45  }
46 
52  private function disableCache()
53  {
54  try {
55  $types = $this->getRequest()->getParam('types');
56  $updatedTypes = 0;
57  if (!is_array($types)) {
58  $types = [];
59  }
60  $this->_validateTypes($types);
61  foreach ($types as $code) {
62  $this->_cacheTypeList->cleanType($code);
63  if ($this->_cacheState->isEnabled($code)) {
64  $this->_cacheState->setEnabled($code, false);
65  $updatedTypes++;
66  }
67  }
68  if ($updatedTypes > 0) {
69  $this->_cacheState->persist();
70  $this->messageManager->addSuccessMessage(__("%1 cache type(s) disabled.", $updatedTypes));
71  }
72  } catch (LocalizedException $e) {
73  $this->messageManager->addErrorMessage($e->getMessage());
74  } catch (\Exception $e) {
75  $this->messageManager->addExceptionMessage($e, __('An error occurred while disabling cache.'));
76  }
77  }
78 
85  private function getState()
86  {
87  if ($this->state === null) {
88  $this->state = ObjectManager::getInstance()->get(State::class);
89  }
90 
91  return $this->state;
92  }
93 }
__()
Definition: __.php:13
$code
Definition: info.phtml:12