Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassEnable.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->enableCache();
42  }
43 
44  return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('adminhtml/*');
45  }
46 
52  private function enableCache()
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  if (!$this->_cacheState->isEnabled($code)) {
63  $this->_cacheState->setEnabled($code, true);
64  $updatedTypes++;
65  }
66  }
67  if ($updatedTypes > 0) {
68  $this->_cacheState->persist();
69  $this->messageManager->addSuccessMessage(__("%1 cache type(s) enabled.", $updatedTypes));
70  }
71  } catch (LocalizedException $e) {
72  $this->messageManager->addErrorMessage($e->getMessage());
73  } catch (\Exception $e) {
74  $this->messageManager->addExceptionMessage($e, __('An error occurred while enabling cache.'));
75  }
76  }
77 
84  private function getState()
85  {
86  if ($this->state === null) {
87  $this->state = ObjectManager::getInstance()->get(State::class);
88  }
89 
90  return $this->state;
91  }
92 }
__()
Definition: __.php:13
$code
Definition: info.phtml:12