PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Filesystem/Filesystem.php

https://github.com/Exercise/symfony
PHP | 284 lines | 147 code | 34 blank | 103 comment | 41 complexity | 8eb2a41f34421aa640299334c3ec20bd MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Filesystem;
  11. /**
  12. * Provides basic utility to manipulate the file system.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Filesystem
  17. {
  18. /**
  19. * Copies a file.
  20. *
  21. * This method only copies the file if the origin file is newer than the target file.
  22. *
  23. * By default, if the target already exists, it is not overridden.
  24. *
  25. * @param string $originFile The original filename
  26. * @param string $targetFile The target filename
  27. * @param array $override Whether to override an existing file or not
  28. */
  29. public function copy($originFile, $targetFile, $override = false)
  30. {
  31. $this->mkdir(dirname($targetFile));
  32. if (!$override && is_file($targetFile)) {
  33. $doCopy = filemtime($originFile) > filemtime($targetFile);
  34. } else {
  35. $doCopy = true;
  36. }
  37. if ($doCopy) {
  38. copy($originFile, $targetFile);
  39. }
  40. }
  41. /**
  42. * Creates a directory recursively.
  43. *
  44. * @param string|array|\Traversable $dirs The directory path
  45. * @param int $mode The directory mode
  46. *
  47. * @return Boolean true if the directory has been created, false otherwise
  48. */
  49. public function mkdir($dirs, $mode = 0777)
  50. {
  51. $ret = true;
  52. foreach ($this->toIterator($dirs) as $dir) {
  53. if (is_dir($dir)) {
  54. continue;
  55. }
  56. $ret = @mkdir($dir, $mode, true) && $ret;
  57. }
  58. return $ret;
  59. }
  60. /**
  61. * Creates empty files.
  62. *
  63. * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to create
  64. */
  65. public function touch($files)
  66. {
  67. foreach ($this->toIterator($files) as $file) {
  68. touch($file);
  69. }
  70. }
  71. /**
  72. * Removes files or directories.
  73. *
  74. * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to remove
  75. */
  76. public function remove($files)
  77. {
  78. $files = iterator_to_array($this->toIterator($files));
  79. $files = array_reverse($files);
  80. foreach ($files as $file) {
  81. if (!file_exists($file) && !is_link($file)) {
  82. continue;
  83. }
  84. if (is_dir($file) && !is_link($file)) {
  85. $this->remove(new \FilesystemIterator($file));
  86. rmdir($file);
  87. } else {
  88. unlink($file);
  89. }
  90. }
  91. }
  92. /**
  93. * Change mode for an array of files or directories.
  94. *
  95. * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change mode
  96. * @param integer $mode The new mode
  97. * @param integer $umask The mode mask (octal)
  98. */
  99. public function chmod($files, $mode, $umask = 0000)
  100. {
  101. $currentUmask = umask();
  102. umask($umask);
  103. foreach ($this->toIterator($files) as $file) {
  104. chmod($file, $mode);
  105. }
  106. umask($currentUmask);
  107. }
  108. /**
  109. * Renames a file.
  110. *
  111. * @param string $origin The origin filename
  112. * @param string $target The new filename
  113. *
  114. * @throws \RuntimeException When target file already exists
  115. * @throws \RuntimeException When origin cannot be renamed
  116. */
  117. public function rename($origin, $target)
  118. {
  119. // we check that target does not exist
  120. if (is_readable($target)) {
  121. throw new \RuntimeException(sprintf('Cannot rename because the target "%s" already exist.', $target));
  122. }
  123. if (false === @rename($origin, $target)) {
  124. throw new \RuntimeException(sprintf('Cannot rename "%s" to "%s".', $origin, $target));
  125. }
  126. }
  127. /**
  128. * Creates a symbolic link or copy a directory.
  129. *
  130. * @param string $originDir The origin directory path
  131. * @param string $targetDir The symbolic link name
  132. * @param Boolean $copyOnWindows Whether to copy files if on Windows
  133. */
  134. public function symlink($originDir, $targetDir, $copyOnWindows = false)
  135. {
  136. if (!function_exists('symlink') && $copyOnWindows) {
  137. $this->mirror($originDir, $targetDir);
  138. return;
  139. }
  140. $ok = false;
  141. if (is_link($targetDir)) {
  142. if (readlink($targetDir) != $originDir) {
  143. unlink($targetDir);
  144. } else {
  145. $ok = true;
  146. }
  147. }
  148. if (!$ok) {
  149. symlink($originDir, $targetDir);
  150. }
  151. }
  152. /**
  153. * Given an existing path, convert it to a path relative to a given starting path
  154. *
  155. * @param string $endPath Absolute path of target
  156. * @param string $startPath Absolute path where traversal begins
  157. *
  158. * @return string Path of target relative to starting path
  159. */
  160. public function makePathRelative($endPath, $startPath)
  161. {
  162. // Find for which character the the common path stops
  163. $offset = 0;
  164. while (isset($startPath[$offset]) && isset($endPath[$offset]) && $startPath[$offset] === $endPath[$offset]) {
  165. $offset++;
  166. }
  167. // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
  168. $diffPath = trim(substr($startPath, $offset), DIRECTORY_SEPARATOR);
  169. $depth = strlen($diffPath) > 0 ? substr_count($diffPath, DIRECTORY_SEPARATOR) + 1 : 0;
  170. // Repeated "../" for each level need to reach the common path
  171. $traverser = str_repeat('../', $depth);
  172. // Construct $endPath from traversing to the common path, then to the remaining $endPath
  173. return $traverser.substr($endPath, $offset);
  174. }
  175. /**
  176. * Mirrors a directory to another.
  177. *
  178. * @param string $originDir The origin directory
  179. * @param string $targetDir The target directory
  180. * @param \Traversable $iterator A Traversable instance
  181. * @param array $options An array of boolean options
  182. * Valid options are:
  183. * - $options['override'] Whether to override an existing file on copy or not (see copy())
  184. * - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink())
  185. *
  186. * @throws \RuntimeException When file type is unknown
  187. */
  188. public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array())
  189. {
  190. $copyOnWindows = false;
  191. if (isset($options['copy_on_windows']) && !function_exists('symlink')) {
  192. $copyOnWindows = $options['copy_on_windows'];
  193. }
  194. if (null === $iterator) {
  195. $flags = $copyOnWindows ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS;
  196. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
  197. }
  198. if ('/' === substr($targetDir, -1) || '\\' === substr($targetDir, -1)) {
  199. $targetDir = substr($targetDir, 0, -1);
  200. }
  201. if ('/' === substr($originDir, -1) || '\\' === substr($originDir, -1)) {
  202. $originDir = substr($originDir, 0, -1);
  203. }
  204. foreach ($iterator as $file) {
  205. $target = str_replace($originDir, $targetDir, $file->getPathname());
  206. if (is_dir($file)) {
  207. $this->mkdir($target);
  208. } elseif (!$copyOnWindows && is_link($file)) {
  209. $this->symlink($file, $target);
  210. } elseif (is_file($file) || ($copyOnWindows && is_link($file))) {
  211. $this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
  212. } else {
  213. throw new \RuntimeException(sprintf('Unable to guess "%s" file type.', $file));
  214. }
  215. }
  216. }
  217. /**
  218. * Returns whether the file path is an absolute path.
  219. *
  220. * @param string $file A file path
  221. *
  222. * @return Boolean
  223. */
  224. public function isAbsolutePath($file)
  225. {
  226. if ($file[0] == '/' || $file[0] == '\\'
  227. || (strlen($file) > 3 && ctype_alpha($file[0])
  228. && $file[1] == ':'
  229. && ($file[2] == '\\' || $file[2] == '/')
  230. )
  231. || null !== parse_url($file, PHP_URL_SCHEME)
  232. ) {
  233. return true;
  234. }
  235. return false;
  236. }
  237. /**
  238. * @param mixed $files
  239. *
  240. * @return \Traversable
  241. */
  242. private function toIterator($files)
  243. {
  244. if (!$files instanceof \Traversable) {
  245. $files = new \ArrayObject(is_array($files) ? $files : array($files));
  246. }
  247. return $files;
  248. }
  249. }