PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/versionpress/src/Utils/FileSystem.php

https://gitlab.com/vanafroo/voipWEB
PHP | 171 lines | 87 code | 25 blank | 59 comment | 15 complexity | ecd6fa4b39c4396321a446abc3b23eee MD5 | raw file
  1. <?php
  2. namespace VersionPress\Utils;
  3. use FilesystemIterator;
  4. use Nette\Utils\Strings;
  5. use RecursiveDirectoryIterator;
  6. use RecursiveIteratorIterator;
  7. use Traversable;
  8. /**
  9. * Helper functions to work with filesystem. Currently, the functions use either bare implementation
  10. * or {@link http://symfony.com/doc/master/components/filesystem/introduction.html Symfony Filesystem}.
  11. */
  12. class FileSystem
  13. {
  14. /**
  15. * Renames (moves) origin to target.
  16. *
  17. * @see \Symfony\Component\Filesystem\Filesystem::rename()
  18. *
  19. * @param string $origin
  20. * @param string $target
  21. * @param bool $overwrite
  22. */
  23. public static function rename($origin, $target, $overwrite = false)
  24. {
  25. self::possiblyFixGitPermissions($origin);
  26. $fs = new \Symfony\Component\Filesystem\Filesystem();
  27. $fs->rename($origin, $target, $overwrite);
  28. }
  29. /**
  30. * Removes a file / directory. Works recursively.
  31. *
  32. * @see \Symfony\Component\Filesystem\Filesystem::remove()
  33. *
  34. * @param string|Traversable $path Path to a file or directory.
  35. */
  36. public static function remove($path)
  37. {
  38. self::possiblyFixGitPermissions($path);
  39. $fs = new \Symfony\Component\Filesystem\Filesystem();
  40. $fs->remove($path);
  41. }
  42. /**
  43. * Removes the content of a directory (not the directory itself). Works recursively.
  44. *
  45. * @param string $path Path to a directory.
  46. */
  47. public static function removeContent($path)
  48. {
  49. if (!is_dir($path)) {
  50. return;
  51. }
  52. $iterator = new RecursiveIteratorIterator(
  53. new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
  54. RecursiveIteratorIterator::CHILD_FIRST
  55. );
  56. foreach ($iterator as $item) {
  57. if ($item->isDir() && Strings::endsWith($iterator->key(), ".git")) {
  58. self::possiblyFixGitPermissions($iterator->key());
  59. }
  60. }
  61. $fs = new \Symfony\Component\Filesystem\Filesystem();
  62. $fs->remove($iterator);
  63. }
  64. /**
  65. * Copies a file. Uses Symfony's copy but actually honors the third parameter.
  66. *
  67. * @param string $origin
  68. * @param string $target
  69. * @param bool $override
  70. */
  71. public static function copy($origin, $target, $override = false)
  72. {
  73. $fs = new \Symfony\Component\Filesystem\Filesystem();
  74. if (!$override && $fs->exists($target)) {
  75. return;
  76. }
  77. $fs->copy($origin, $target, $override);
  78. }
  79. /**
  80. * Copies a directory. Uses Symfony's mirror() under the cover.
  81. *
  82. * @see \Symfony\Component\Filesystem\Filesystem::mirror()
  83. *
  84. * @param string $origin
  85. * @param string $target
  86. */
  87. public static function copyDir($origin, $target)
  88. {
  89. $fs = new \Symfony\Component\Filesystem\Filesystem();
  90. $fs->mirror($origin, $target);
  91. }
  92. /**
  93. * Creates a directory
  94. *
  95. * @param string $dir
  96. * @param int $mode
  97. */
  98. public static function mkdir($dir, $mode = 0750)
  99. {
  100. $fs = new \Symfony\Component\Filesystem\Filesystem();
  101. $fs->mkdir($dir, $mode);
  102. }
  103. /**
  104. * Git for Windows makes files in `.git/objects` read-only. This method removes the flag
  105. * so that operations like removing the folder work.
  106. *
  107. * @param string $path Either path to `.git` itself or its parent directory (repo root)
  108. */
  109. private static function possiblyFixGitPermissions($path)
  110. {
  111. if (DIRECTORY_SEPARATOR == '/') {
  112. return;
  113. }
  114. $gitDir = null;
  115. if (is_dir($path)) {
  116. if (basename($path) == '.git') {
  117. $gitDir = $path;
  118. } else {
  119. if (is_dir($path . '/.git')) {
  120. $gitDir = $path . '/.git';
  121. }
  122. }
  123. }
  124. if ($gitDir) {
  125. $iterator = new RecursiveIteratorIterator(
  126. new RecursiveDirectoryIterator($gitDir . '/objects', FilesystemIterator::SKIP_DOTS)
  127. );
  128. foreach ($iterator as $item) {
  129. if (is_file($item)) {
  130. chmod($item, 0640);
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Compares two files and returns true if their contents is equal
  137. *
  138. * @param $file1
  139. * @param $file2
  140. * @return bool
  141. */
  142. public static function filesHaveSameContents($file1, $file2)
  143. {
  144. return sha1_file($file1) == sha1_file($file2);
  145. }
  146. }