CleanupFiles.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\State;
  7. use Magento\Framework\Filesystem;
  8. use Magento\Framework\Exception\FileSystemException;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. /**
  11. * A service for cleaning up application state
  12. */
  13. class CleanupFiles
  14. {
  15. /**
  16. * File system
  17. *
  18. * @var Filesystem
  19. */
  20. private $filesystem;
  21. /**
  22. * Constructor
  23. *
  24. * @param Filesystem $filesystem
  25. */
  26. public function __construct(Filesystem $filesystem)
  27. {
  28. $this->filesystem = $filesystem;
  29. }
  30. /**
  31. * Clears all files that are subject of code generation
  32. *
  33. * @return string[]
  34. */
  35. public function clearCodeGeneratedFiles()
  36. {
  37. return array_merge(
  38. $this->clearCodeGeneratedClasses(),
  39. $this->clearMaterializedViewFiles()
  40. );
  41. }
  42. /**
  43. * Clears code-generated classes
  44. *
  45. * @return string[]
  46. */
  47. public function clearCodeGeneratedClasses()
  48. {
  49. return array_merge(
  50. $this->emptyDir(DirectoryList::GENERATED_CODE),
  51. $this->emptyDir(DirectoryList::GENERATED_METADATA)
  52. );
  53. }
  54. /**
  55. * Clears materialized static view files
  56. *
  57. * @return string[]
  58. */
  59. public function clearMaterializedViewFiles()
  60. {
  61. return array_merge(
  62. $this->emptyDir(DirectoryList::STATIC_VIEW),
  63. $this->emptyDir(DirectoryList::VAR_DIR, DirectoryList::TMP_MATERIALIZATION_DIR)
  64. );
  65. }
  66. /**
  67. * Clears all files
  68. *
  69. * @return string[]
  70. */
  71. public function clearAllFiles()
  72. {
  73. return array_merge(
  74. $this->emptyDir(DirectoryList::STATIC_VIEW),
  75. $this->emptyDir(DirectoryList::VAR_DIR)
  76. );
  77. }
  78. /**
  79. * Deletes contents of specified directory
  80. *
  81. * @param string $code
  82. * @param string|null $subPath
  83. * @return string[]
  84. */
  85. private function emptyDir($code, $subPath = null)
  86. {
  87. $messages = [];
  88. $dir = $this->filesystem->getDirectoryWrite($code);
  89. $dirPath = $dir->getAbsolutePath();
  90. if (!$dir->isExist()) {
  91. $messages[] = "The directory '{$dirPath}' doesn't exist - skipping cleanup";
  92. return $messages;
  93. }
  94. foreach ($dir->search('*', $subPath) as $path) {
  95. if ($path !== '.' && $path !== '..') {
  96. $messages[] = $dirPath . $path;
  97. try {
  98. $dir->delete($path);
  99. } catch (FileSystemException $e) {
  100. $messages[] = $e->getMessage();
  101. }
  102. }
  103. }
  104. return $messages;
  105. }
  106. }