Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GeneratedFiles.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
13 
18 {
22  const REGENERATE_FLAG = '/var/.regenerate';
23 
27  private $directoryList;
28 
32  private $write;
33 
40  public function __construct(DirectoryList $directoryList, WriteFactory $writeFactory)
41  {
42  $this->directoryList = $directoryList;
43  $this->write = $writeFactory->create(BP);
44  }
45 
54  public function regenerate()
55  {
56  $this->cleanGeneratedFiles();
57  }
58 
64  public function cleanGeneratedFiles()
65  {
66  if ($this->write->isExist(self::REGENERATE_FLAG)) {
67  $enabledCacheTypes = [];
68 
69  //TODO: to be removed in scope of MAGETWO-53476
70  $deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
71  $configPool = new ConfigFilePool();
72  $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
73  if ($this->write->isExist($this->write->getRelativePath($envPath))) {
74  $enabledCacheTypes = $this->getEnabledCacheTypes();
75  $this->disableAllCacheTypes();
76  }
77  //TODO: Till here
78 
79  $cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE));
80  $generationPath = $this->write->getRelativePath(
81  $this->directoryList->getPath(DirectoryList::GENERATED_CODE)
82  );
83  $diPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATED_METADATA));
84 
85  // Clean generated/code dir
86  if ($this->write->isDirectory($generationPath)) {
87  $this->write->delete($generationPath);
88  }
89 
90  // Clean generated/metadata
91  if ($this->write->isDirectory($diPath)) {
92  $this->write->delete($diPath);
93  }
94 
95  // Clean var/cache
96  if ($this->write->isDirectory($cachePath)) {
97  $this->write->delete($cachePath);
98  }
99  $this->write->delete(self::REGENERATE_FLAG);
100  $this->enableCacheTypes($enabledCacheTypes);
101  }
102  }
103 
110  public function requestRegeneration()
111  {
112  $this->write->touch(self::REGENERATE_FLAG);
113  }
114 
120  private function getEnabledCacheTypes()
121  {
122  $enabledCacheTypes = [];
123  $envPath = $this->getEnvPath();
124  if ($this->write->isReadable($this->write->getRelativePath($envPath))) {
125  $envData = include $envPath;
126  if (isset($envData['cache_types'])) {
127  $cacheStatus = $envData['cache_types'];
128  $enabledCacheTypes = array_filter($cacheStatus, function ($value) {
129  return $value;
130  });
131  $enabledCacheTypes = array_keys($enabledCacheTypes);
132  }
133  }
134  return $enabledCacheTypes;
135  }
136 
143  private function getEnvPath()
144  {
145  $deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
146  $configPool = new ConfigFilePool();
147  $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
148  return $envPath;
149  }
150 
156  private function disableAllCacheTypes()
157  {
158  $envPath = $this->getEnvPath();
159  if ($this->write->isWritable($this->write->getRelativePath($envPath))) {
160  $envData = include $envPath;
161 
162  if (isset($envData['cache_types'])) {
163  $cacheTypes = array_keys($envData['cache_types']);
164 
165  foreach ($cacheTypes as $cacheType) {
166  $envData['cache_types'][$cacheType] = 0;
167  }
168 
169  $formatter = new PhpFormatter();
170  $contents = $formatter->format($envData);
171 
172  $this->write->writeFile($this->write->getRelativePath($envPath), $contents);
173  if (function_exists('opcache_invalidate')) {
174  opcache_invalidate(
175  $this->write->getAbsolutePath($envPath)
176  );
177  }
178  }
179  }
180  }
181 
189  private function enableCacheTypes($cacheTypes)
190  {
191  if (empty($cacheTypes)) {
192  return;
193  }
194  $envPath = $this->getEnvPath();
195  if ($this->write->isReadable($this->write->getRelativePath($envPath))) {
196  $envData = include $envPath;
197  foreach ($cacheTypes as $cacheType) {
198  if (isset($envData['cache_types'][$cacheType])) {
199  $envData['cache_types'][$cacheType] = 1;
200  }
201  }
202 
203  $formatter = new PhpFormatter();
204  $contents = $formatter->format($envData);
205 
206  $this->write->writeFile($this->write->getRelativePath($envPath), $contents);
207  if (function_exists('opcache_invalidate')) {
208  opcache_invalidate($this->write->getAbsolutePath($envPath));
209  }
210  }
211  }
212 }
$contents
Definition: website.php:14
$deploymentConfig
create($path, $driverCode=DriverPool::FILE, $createPermissions=null)
$value
Definition: gender.phtml:16
__construct(DirectoryList $directoryList, WriteFactory $writeFactory)
const BP
Definition: autoload.php:14