PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/nette/utils/src/Utils/FileSystem.php

https://gitlab.com/tekoestudio/capacitacion-tipsy
PHP | 157 lines | 121 code | 14 blank | 22 comment | 12 complexity | 57dcd74a8494d1f9a7b4993df754f687 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. namespace Nette\Utils;
  7. use Nette;
  8. /**
  9. * File system tool.
  10. */
  11. class FileSystem
  12. {
  13. use Nette\StaticClass;
  14. /**
  15. * Creates a directory.
  16. * @return void
  17. * @throws Nette\IOException
  18. */
  19. public static function createDir($dir, $mode = 0777)
  20. {
  21. if (!is_dir($dir) && !@mkdir($dir, $mode, TRUE) && !is_dir($dir)) { // @ - dir may already exist
  22. throw new Nette\IOException("Unable to create directory '$dir'. " . error_get_last()['message']);
  23. }
  24. }
  25. /**
  26. * Copies a file or directory.
  27. * @return void
  28. * @throws Nette\IOException
  29. */
  30. public static function copy($source, $dest, $overwrite = TRUE)
  31. {
  32. if (stream_is_local($source) && !file_exists($source)) {
  33. throw new Nette\IOException("File or directory '$source' not found.");
  34. } elseif (!$overwrite && file_exists($dest)) {
  35. throw new Nette\InvalidStateException("File or directory '$dest' already exists.");
  36. } elseif (is_dir($source)) {
  37. static::createDir($dest);
  38. foreach (new \FilesystemIterator($dest) as $item) {
  39. static::delete($item->getPathname());
  40. }
  41. foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
  42. if ($item->isDir()) {
  43. static::createDir($dest . '/' . $iterator->getSubPathName());
  44. } else {
  45. static::copy($item->getPathname(), $dest . '/' . $iterator->getSubPathName());
  46. }
  47. }
  48. } else {
  49. static::createDir(dirname($dest));
  50. if (@stream_copy_to_stream(fopen($source, 'r'), fopen($dest, 'w')) === FALSE) { // @ is escalated to exception
  51. throw new Nette\IOException("Unable to copy file '$source' to '$dest'.");
  52. }
  53. }
  54. }
  55. /**
  56. * Deletes a file or directory.
  57. * @return void
  58. * @throws Nette\IOException
  59. */
  60. public static function delete($path)
  61. {
  62. if (is_file($path) || is_link($path)) {
  63. $func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
  64. if (!@$func($path)) { // @ is escalated to exception
  65. throw new Nette\IOException("Unable to delete '$path'.");
  66. }
  67. } elseif (is_dir($path)) {
  68. foreach (new \FilesystemIterator($path) as $item) {
  69. static::delete($item->getPathname());
  70. }
  71. if (!@rmdir($path)) { // @ is escalated to exception
  72. throw new Nette\IOException("Unable to delete directory '$path'.");
  73. }
  74. }
  75. }
  76. /**
  77. * Renames a file or directory.
  78. * @return void
  79. * @throws Nette\IOException
  80. * @throws Nette\InvalidStateException if the target file or directory already exist
  81. */
  82. public static function rename($name, $newName, $overwrite = TRUE)
  83. {
  84. if (!$overwrite && file_exists($newName)) {
  85. throw new Nette\InvalidStateException("File or directory '$newName' already exists.");
  86. } elseif (!file_exists($name)) {
  87. throw new Nette\IOException("File or directory '$name' not found.");
  88. } else {
  89. static::createDir(dirname($newName));
  90. static::delete($newName);
  91. if (!@rename($name, $newName)) { // @ is escalated to exception
  92. throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'.");
  93. }
  94. }
  95. }
  96. /**
  97. * Reads file content.
  98. * @return string
  99. * @throws Nette\IOException
  100. */
  101. public static function read($file)
  102. {
  103. $content = @file_get_contents($file); // @ is escalated to exception
  104. if ($content === FALSE) {
  105. throw new Nette\IOException("Unable to read file '$file'.");
  106. }
  107. return $content;
  108. }
  109. /**
  110. * Writes a string to a file.
  111. * @return void
  112. * @throws Nette\IOException
  113. */
  114. public static function write($file, $content, $mode = 0666)
  115. {
  116. static::createDir(dirname($file));
  117. if (@file_put_contents($file, $content) === FALSE) { // @ is escalated to exception
  118. throw new Nette\IOException("Unable to write file '$file'.");
  119. }
  120. if ($mode !== NULL && !@chmod($file, $mode)) { // @ is escalated to exception
  121. throw new Nette\IOException("Unable to chmod file '$file'.");
  122. }
  123. }
  124. /**
  125. * Is path absolute?
  126. * @return bool
  127. */
  128. public static function isAbsolute($path)
  129. {
  130. return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
  131. }
  132. }