Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CleanupFiles.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
23  private $filesystem;
24 
30  public function __construct(Filesystem $filesystem)
31  {
32  $this->filesystem = $filesystem;
33  }
34 
40  public function clearCodeGeneratedFiles()
41  {
42  return array_merge(
45  );
46  }
47 
53  public function clearCodeGeneratedClasses()
54  {
55  return array_merge(
56  $this->emptyDir(DirectoryList::GENERATED_CODE),
57  $this->emptyDir(DirectoryList::GENERATED_METADATA)
58  );
59  }
60 
66  public function clearMaterializedViewFiles()
67  {
68  return array_merge(
69  $this->emptyDir(DirectoryList::STATIC_VIEW),
71  );
72  }
73 
79  public function clearAllFiles()
80  {
81  return array_merge(
82  $this->emptyDir(DirectoryList::STATIC_VIEW),
83  $this->emptyDir(DirectoryList::VAR_DIR)
84  );
85  }
86 
94  private function emptyDir($code, $subPath = null)
95  {
96  $messages = [];
97 
98  $dir = $this->filesystem->getDirectoryWrite($code);
99  $dirPath = $dir->getAbsolutePath();
100  if (!$dir->isExist()) {
101  $messages[] = "The directory '{$dirPath}' doesn't exist - skipping cleanup";
102  return $messages;
103  }
104  foreach ($dir->search('*', $subPath) as $path) {
105  if ($path !== '.' && $path !== '..') {
106  $messages[] = $dirPath . $path;
107  try {
108  $dir->delete($path);
109  } catch (FileSystemException $e) {
110  $messages[] = $e->getMessage();
111  }
112  }
113  }
114 
115  return $messages;
116  }
117 }
$filesystem
$code
Definition: info.phtml:12