Helper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Backup\Filesystem;
  7. use Magento\Framework\Backup\Filesystem\Iterator\Filter;
  8. use RecursiveDirectoryIterator;
  9. use RecursiveIteratorIterator;
  10. /**
  11. * Filesystem helper
  12. *
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. */
  15. class Helper
  16. {
  17. /**
  18. * Constant can be used in getInfo() function as second parameter.
  19. * Check whether directory and all files/sub directories are writable
  20. *
  21. * @const int
  22. */
  23. const INFO_WRITABLE = 1;
  24. /**
  25. * Constant can be used in getInfo() function as second parameter.
  26. * Check whether directory and all files/sub directories are readable
  27. *
  28. * @const int
  29. */
  30. const INFO_READABLE = 2;
  31. /**
  32. * Constant can be used in getInfo() function as second parameter.
  33. * Get directory size
  34. *
  35. * @const int
  36. */
  37. const INFO_SIZE = 4;
  38. /**
  39. * Constant can be used in getInfo() function as second parameter.
  40. * Combination of INFO_WRITABLE, INFO_READABLE, INFO_SIZE
  41. *
  42. * @const int
  43. */
  44. const INFO_ALL = 7;
  45. /**
  46. * Recursively delete $path
  47. *
  48. * @param string $path
  49. * @param array $skipPaths
  50. * @param bool $removeRoot
  51. * @return void
  52. * @throws \Magento\Framework\Exception\LocalizedException
  53. * @SuppressWarnings(PHPMD.ShortMethodName)
  54. */
  55. public function rm($path, $skipPaths = [], $removeRoot = false)
  56. {
  57. $filesystemIterator = new RecursiveIteratorIterator(
  58. new RecursiveDirectoryIterator($path),
  59. RecursiveIteratorIterator::CHILD_FIRST
  60. );
  61. $iterator = new Filter($filesystemIterator, $skipPaths);
  62. foreach ($iterator as $item) {
  63. $item->isDir() ? @rmdir($item->__toString()) : @unlink($item->__toString());
  64. }
  65. if ($removeRoot && is_dir($path)) {
  66. @rmdir($path);
  67. }
  68. }
  69. /**
  70. * Get information (readable, writable, size) about $path
  71. *
  72. * @param string $path
  73. * @param int $infoOptions
  74. * @param array $skipFiles
  75. * @return array
  76. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  77. * @SuppressWarnings(PHPMD.NPathComplexity)
  78. */
  79. public function getInfo($path, $infoOptions = self::INFO_ALL, $skipFiles = [])
  80. {
  81. $info = [];
  82. if ($infoOptions & self::INFO_READABLE) {
  83. $info['readable'] = true;
  84. $info['readableMeta'] = [];
  85. }
  86. if ($infoOptions & self::INFO_WRITABLE) {
  87. $info['writable'] = true;
  88. $info['writableMeta'] = [];
  89. }
  90. if ($infoOptions & self::INFO_SIZE) {
  91. $info['size'] = 0;
  92. }
  93. $filesystemIterator = new RecursiveIteratorIterator(
  94. new RecursiveDirectoryIterator($path),
  95. RecursiveIteratorIterator::CHILD_FIRST
  96. );
  97. $iterator = new Filter($filesystemIterator, $skipFiles);
  98. foreach ($iterator as $item) {
  99. if ($item->isLink()) {
  100. continue;
  101. }
  102. if ($infoOptions & self::INFO_WRITABLE && !$item->isWritable()) {
  103. $info['writable'] = false;
  104. $info['writableMeta'][] = $item->getPathname();
  105. }
  106. if ($infoOptions & self::INFO_READABLE && !$item->isReadable()) {
  107. $info['readable'] = false;
  108. $info['readableMeta'][] = $item->getPathname();
  109. }
  110. if ($infoOptions & self::INFO_SIZE && !$item->isDir()) {
  111. $info['size'] += $item->getSize();
  112. }
  113. }
  114. return $info;
  115. }
  116. }