GeneratedFiles.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Code;
  7. use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Config\File\ConfigFilePool;
  10. use Magento\Framework\Filesystem\Directory\WriteFactory;
  11. use Magento\Framework\Filesystem\Directory\WriteInterface;
  12. /**
  13. * Regenerates generated code and DI configuration
  14. */
  15. class GeneratedFiles
  16. {
  17. /**
  18. * Separator literal to assemble timer identifier from timer names
  19. */
  20. const REGENERATE_FLAG = '/var/.regenerate';
  21. /**
  22. * @var DirectoryList
  23. */
  24. private $directoryList;
  25. /**
  26. * @var WriteInterface
  27. */
  28. private $write;
  29. /**
  30. * Constructor
  31. *
  32. * @param DirectoryList $directoryList
  33. * @param WriteFactory $writeFactory
  34. */
  35. public function __construct(DirectoryList $directoryList, WriteFactory $writeFactory)
  36. {
  37. $this->directoryList = $directoryList;
  38. $this->write = $writeFactory->create(BP);
  39. }
  40. /**
  41. * Clean generated code and DI configuration
  42. *
  43. * @return void
  44. *
  45. * @deprecated 100.1.0
  46. * @see \Magento\Framework\Code\GeneratedFiles::cleanGeneratedFiles
  47. */
  48. public function regenerate()
  49. {
  50. $this->cleanGeneratedFiles();
  51. }
  52. /**
  53. * Clean generated/code, generated/metadata and var/cache
  54. *
  55. * @return void
  56. */
  57. public function cleanGeneratedFiles()
  58. {
  59. if ($this->write->isExist(self::REGENERATE_FLAG)) {
  60. $enabledCacheTypes = [];
  61. //TODO: to be removed in scope of MAGETWO-53476
  62. $deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
  63. $configPool = new ConfigFilePool();
  64. $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
  65. if ($this->write->isExist($this->write->getRelativePath($envPath))) {
  66. $enabledCacheTypes = $this->getEnabledCacheTypes();
  67. $this->disableAllCacheTypes();
  68. }
  69. //TODO: Till here
  70. $cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE));
  71. $generationPath = $this->write->getRelativePath(
  72. $this->directoryList->getPath(DirectoryList::GENERATED_CODE)
  73. );
  74. $diPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATED_METADATA));
  75. // Clean generated/code dir
  76. if ($this->write->isDirectory($generationPath)) {
  77. $this->write->delete($generationPath);
  78. }
  79. // Clean generated/metadata
  80. if ($this->write->isDirectory($diPath)) {
  81. $this->write->delete($diPath);
  82. }
  83. // Clean var/cache
  84. if ($this->write->isDirectory($cachePath)) {
  85. $this->write->delete($cachePath);
  86. }
  87. $this->write->delete(self::REGENERATE_FLAG);
  88. $this->enableCacheTypes($enabledCacheTypes);
  89. }
  90. }
  91. /**
  92. * Create flag for cleaning up generated/code, generated/metadata and var/cache directories for subsequent
  93. * regeneration of this content
  94. *
  95. * @return void
  96. */
  97. public function requestRegeneration()
  98. {
  99. $this->write->touch(self::REGENERATE_FLAG);
  100. }
  101. /**
  102. * Reads Cache configuration from env.php and returns indexed array containing all the enabled cache types.
  103. *
  104. * @return string[]
  105. */
  106. private function getEnabledCacheTypes()
  107. {
  108. $enabledCacheTypes = [];
  109. $envPath = $this->getEnvPath();
  110. if ($this->write->isReadable($this->write->getRelativePath($envPath))) {
  111. $envData = include $envPath;
  112. if (isset($envData['cache_types'])) {
  113. $cacheStatus = $envData['cache_types'];
  114. $enabledCacheTypes = array_filter($cacheStatus, function ($value) {
  115. return $value;
  116. });
  117. $enabledCacheTypes = array_keys($enabledCacheTypes);
  118. }
  119. }
  120. return $enabledCacheTypes;
  121. }
  122. /**
  123. * Returns path to env.php file
  124. *
  125. * @return string
  126. * @throws \Exception
  127. */
  128. private function getEnvPath()
  129. {
  130. $deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
  131. $configPool = new ConfigFilePool();
  132. $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
  133. return $envPath;
  134. }
  135. /**
  136. * Disables all cache types by updating env.php.
  137. *
  138. * @return void
  139. */
  140. private function disableAllCacheTypes()
  141. {
  142. $envPath = $this->getEnvPath();
  143. if ($this->write->isWritable($this->write->getRelativePath($envPath))) {
  144. $envData = include $envPath;
  145. if (isset($envData['cache_types'])) {
  146. $cacheTypes = array_keys($envData['cache_types']);
  147. foreach ($cacheTypes as $cacheType) {
  148. $envData['cache_types'][$cacheType] = 0;
  149. }
  150. $formatter = new PhpFormatter();
  151. $contents = $formatter->format($envData);
  152. $this->write->writeFile($this->write->getRelativePath($envPath), $contents);
  153. if (function_exists('opcache_invalidate')) {
  154. opcache_invalidate(
  155. $this->write->getAbsolutePath($envPath)
  156. );
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * Enables appropriate cache types in app/etc/env.php based on the passed in $cacheTypes array
  163. * TODO: to be removed in scope of MAGETWO-53476
  164. *
  165. * @param string[] $cacheTypes
  166. * @return void
  167. */
  168. private function enableCacheTypes($cacheTypes)
  169. {
  170. if (empty($cacheTypes)) {
  171. return;
  172. }
  173. $envPath = $this->getEnvPath();
  174. if ($this->write->isReadable($this->write->getRelativePath($envPath))) {
  175. $envData = include $envPath;
  176. foreach ($cacheTypes as $cacheType) {
  177. if (isset($envData['cache_types'][$cacheType])) {
  178. $envData['cache_types'][$cacheType] = 1;
  179. }
  180. }
  181. $formatter = new PhpFormatter();
  182. $contents = $formatter->format($envData);
  183. $this->write->writeFile($this->write->getRelativePath($envPath), $contents);
  184. if (function_exists('opcache_invalidate')) {
  185. opcache_invalidate($this->write->getAbsolutePath($envPath));
  186. }
  187. }
  188. }
  189. }