Filesystem.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Downloader\Util;
  12. /**
  13. * @author Jordi Boggiano <j.boggiano@seld.be>
  14. */
  15. class Filesystem
  16. {
  17. public function removeDirectory($directory)
  18. {
  19. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  20. system(sprintf('rmdir /S /Q %s', escapeshellarg(realpath($directory))));
  21. } else {
  22. system(sprintf('rm -rf %s', escapeshellarg($directory)));
  23. }
  24. }
  25. public function ensureDirectoryExists($directory)
  26. {
  27. if (!is_dir($directory)) {
  28. if (file_exists($directory)) {
  29. throw new \RuntimeException(
  30. $directory.' exists and is not a directory.'
  31. );
  32. }
  33. if (!mkdir($directory, 0777, true)) {
  34. throw new \RuntimeException(
  35. $directory.' does not exist and could not be created.'
  36. );
  37. }
  38. }
  39. }
  40. /**
  41. * Returns the shortest path from $from to $to
  42. *
  43. * @param string $from
  44. * @param string $to
  45. * @return string
  46. */
  47. public function findShortestPath($from, $to)
  48. {
  49. if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
  50. throw new \InvalidArgumentException('from and to must be absolute paths');
  51. }
  52. if (dirname($from) === dirname($to)) {
  53. return './'.basename($to);
  54. }
  55. $from = rtrim(strtr($from, '\\', '/'), '/');
  56. $to = rtrim(strtr($to, '\\', '/'), '/');
  57. $commonPath = strtr(dirname($to), '\\', '/');
  58. while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) {
  59. $commonPath = strtr(dirname($commonPath), '\\', '/');
  60. }
  61. if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) {
  62. return $to;
  63. }
  64. if (strpos($from, $to) === 0) {
  65. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
  66. return str_repeat('../', $sourcePathDepth);
  67. }
  68. $commonPath = rtrim($commonPath, '/') . '/';
  69. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
  70. $commonPathCode = str_repeat('../', $sourcePathDepth);
  71. return $commonPathCode . substr($to, strlen($commonPath));
  72. }
  73. /**
  74. * Returns PHP code that, when executed in $from, will return the path to $to
  75. *
  76. * @param string $from
  77. * @param string $to
  78. * @param Boolean $directories if true, the source/target are considered to be directories
  79. * @return string
  80. */
  81. public function findShortestPathCode($from, $to, $directories = false)
  82. {
  83. if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
  84. throw new \InvalidArgumentException('from and to must be absolute paths');
  85. }
  86. if ($from === $to) {
  87. return $directories ? '__DIR__' : '__FILE__';
  88. }
  89. $from = strtr($from, '\\', '/');
  90. $to = strtr($to, '\\', '/');
  91. $commonPath = dirname($to);
  92. while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/$}i', $commonPath)) {
  93. $commonPath = strtr(dirname($commonPath), '\\', '/');
  94. }
  95. if (0 !== strpos($from, $commonPath) || '/' === $commonPath) {
  96. return var_export($to, true);
  97. }
  98. $commonPath = rtrim($commonPath, '/') . '/';
  99. if (strpos($to, $from) === 0) {
  100. return '__DIR__ . '.var_export(substr($to, strlen($from)), true);
  101. }
  102. if (strpos($from, $to) === 0) {
  103. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') - 1 + $directories;
  104. return str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth);
  105. }
  106. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories;
  107. $commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth);
  108. return $commonPathCode . '.' . var_export('/' . substr($to, strlen($commonPath)), true);
  109. }
  110. /**
  111. * Checks if the given path is absolute
  112. *
  113. * @param string $path
  114. * @return Boolean
  115. */
  116. public function isAbsolutePath($path)
  117. {
  118. return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
  119. }
  120. }