PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Composer/Util/Filesystem.php

https://github.com/skug/composer
PHP | 153 lines | 108 code | 18 blank | 27 comment | 20 complexity | b7546689e132401c8b7ae7c74a1a35a9 MD5 | raw file
  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\Util;
  12. /**
  13. * @author Jordi Boggiano <j.boggiano@seld.be>
  14. */
  15. class Filesystem
  16. {
  17. public function removeDirectory($directory)
  18. {
  19. if (!is_dir($directory)) {
  20. return true;
  21. }
  22. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  23. $cmd = sprintf('rmdir /S /Q %s', escapeshellarg(realpath($directory)));
  24. } else {
  25. $cmd = sprintf('rm -rf %s', escapeshellarg($directory));
  26. }
  27. $result = $this->getProcess()->execute($cmd) === 0;
  28. // clear stat cache because external processes aren't tracked by the php stat cache
  29. clearstatcache();
  30. return $result && !is_dir($directory);
  31. }
  32. public function ensureDirectoryExists($directory)
  33. {
  34. if (!is_dir($directory)) {
  35. if (file_exists($directory)) {
  36. throw new \RuntimeException(
  37. $directory.' exists and is not a directory.'
  38. );
  39. }
  40. if (!mkdir($directory, 0777, true)) {
  41. throw new \RuntimeException(
  42. $directory.' does not exist and could not be created.'
  43. );
  44. }
  45. }
  46. }
  47. /**
  48. * Returns the shortest path from $from to $to
  49. *
  50. * @param string $from
  51. * @param string $to
  52. * @param bool $directories if true, the source/target are considered to be directories
  53. * @return string
  54. */
  55. public function findShortestPath($from, $to, $directories = false)
  56. {
  57. if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
  58. throw new \InvalidArgumentException(sprintf('$from (%s) and $to (%s) must be absolute paths.', $from, $to));
  59. }
  60. $from = lcfirst(rtrim(strtr($from, '\\', '/'), '/'));
  61. $to = lcfirst(rtrim(strtr($to, '\\', '/'), '/'));
  62. if ($directories) {
  63. $from .= '/dummy_file';
  64. }
  65. if (dirname($from) === dirname($to)) {
  66. return './'.basename($to);
  67. }
  68. $commonPath = $to;
  69. while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) {
  70. $commonPath = strtr(dirname($commonPath), '\\', '/');
  71. }
  72. if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) {
  73. return $to;
  74. }
  75. $commonPath = rtrim($commonPath, '/') . '/';
  76. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
  77. $commonPathCode = str_repeat('../', $sourcePathDepth);
  78. return ($commonPathCode . substr($to, strlen($commonPath))) ?: './';
  79. }
  80. /**
  81. * Returns PHP code that, when executed in $from, will return the path to $to
  82. *
  83. * @param string $from
  84. * @param string $to
  85. * @param bool $directories if true, the source/target are considered to be directories
  86. * @return string
  87. */
  88. public function findShortestPathCode($from, $to, $directories = false)
  89. {
  90. if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
  91. throw new \InvalidArgumentException(sprintf('$from (%s) and $to (%s) must be absolute paths.', $from, $to));
  92. }
  93. $from = lcfirst(strtr($from, '\\', '/'));
  94. $to = lcfirst(strtr($to, '\\', '/'));
  95. if ($from === $to) {
  96. return $directories ? '__DIR__' : '__FILE__';
  97. }
  98. $commonPath = $to;
  99. while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) {
  100. $commonPath = strtr(dirname($commonPath), '\\', '/');
  101. }
  102. if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) {
  103. return var_export($to, true);
  104. }
  105. $commonPath = rtrim($commonPath, '/') . '/';
  106. if (strpos($to, $from.'/') === 0) {
  107. return '__DIR__ . '.var_export(substr($to, strlen($from)), true);
  108. }
  109. $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories;
  110. $commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth);
  111. $relTarget = substr($to, strlen($commonPath));
  112. return $commonPathCode . (strlen($relTarget) ? '.' . var_export('/' . $relTarget, true) : '');
  113. }
  114. /**
  115. * Checks if the given path is absolute
  116. *
  117. * @param string $path
  118. * @return bool
  119. */
  120. public function isAbsolutePath($path)
  121. {
  122. return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
  123. }
  124. protected function getProcess()
  125. {
  126. return new ProcessExecutor;
  127. }
  128. }