PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Composer/Downloader/Util/Filesystem.php

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