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

/phpunit/Helper/FileSystem2.php

https://gitlab.com/x33n/ImpressPages
PHP | 235 lines | 176 code | 38 blank | 21 comment | 42 complexity | 98aaabc82cb995c8b90e4ce9f540b8e8 MD5 | raw file
  1. <?php
  2. /**
  3. * @package ImpressPages
  4. *
  5. */
  6. namespace PhpUnit\Helper;
  7. use Ip\Internal\Update\Helper\FileSystemException;
  8. class FileSystem2
  9. {
  10. public function createWritableDir($dir)
  11. {
  12. //if (substr($dir, 0, 1) != '/') {
  13. //throw new \Exception('Absolute path required', \IpUpdate\Library\Exception::OTHER);
  14. //}
  15. if ($dir == '/' && !is_writable($dir)) {
  16. $this->throwWritePermissionsError($dir);
  17. }
  18. $dir = $this->removeTrailingSlash($dir); //remove trailing slash
  19. $parentDir = $this->getParentDir($dir);
  20. if (!file_exists($parentDir) || !is_dir($parentDir)) {
  21. $this->createWritableDir($parentDir);
  22. }
  23. if (!is_writable($parentDir)) {
  24. $this->throwWritePermissionsError($parentDir);
  25. }
  26. if (!file_exists($dir)) {
  27. mkdir($dir);
  28. } else {
  29. $this->makeWritable($dir);
  30. }
  31. }
  32. /**
  33. * Make directory or file and all subdirs and files writable
  34. * @param string $dir
  35. * @param int $permissions eg 0755. ZERO IS REQUIRED. Applied only to files and folders that are not writable.
  36. * @return boolean
  37. */
  38. function makeWritable($path, $permissions = null)
  39. {
  40. $answer = true;
  41. if(!file_exists($path)) {
  42. return false;
  43. }
  44. if (!is_writable($path)) {
  45. if ($permissions == null) {
  46. $permissions = $this->getParentPermissions($path);
  47. }
  48. $oldErrorHandler = set_error_handler(array('PhpUnit\Helper\FileSystem2', 'handleError'));
  49. try {
  50. $success = chmod($path, $permissions);
  51. }
  52. catch (FileSystemException $e) {
  53. //do nothing. This is just the way to avoid warnings
  54. }
  55. if ($oldErrorHandler) { //dev tools has no oldErrorHandler error handler. So we have to check.
  56. set_error_handler($oldErrorHandler);
  57. }
  58. if (!is_writable($path)) {
  59. $this->throwWritePermissionsError($path);
  60. }
  61. }
  62. if (is_dir($path)) {
  63. $path = $this->removeTrailingSlash($path);
  64. if ($handle = opendir($path)) {
  65. while (false !== ($file = readdir($handle))) {
  66. if($file == ".." || $file == ".") {
  67. continue;
  68. }
  69. if (is_dir($path.'/'.$file)) {
  70. $this->makeWritable($path.'/'.$file, $permissions);
  71. } else {
  72. if (!is_writable($path.'/'.$file)) {
  73. if ($permissions == null) {
  74. $permissions = $this->getParentPermissions($path);
  75. }
  76. $oldErrorHandler = set_error_handler(array('PhpUnit\Helper\FileSystem2', 'handleError'));
  77. try {
  78. chmod($path.'/'.$file, $permissions);
  79. } catch (FileSystemException $e) {
  80. //do nothing. This is just the way to avoid warnings
  81. }
  82. if ($oldErrorHandler) { //dev tools has no oldErrorHandler error handler. So we have to check.
  83. set_error_handler($oldErrorHandler);
  84. }
  85. }
  86. if (!is_writable($path.'/'.$file)) {
  87. $this->throwWritePermissionsError($path.'/'.$file);
  88. }
  89. }
  90. }
  91. closedir($handle);
  92. }
  93. }
  94. return $answer;
  95. }
  96. public function rm($dir) {
  97. if (!file_exists($dir)) {
  98. return;
  99. }
  100. if (!is_writable($dir)) {
  101. $this->makeWritable($dir, 0777);
  102. }
  103. if (is_dir($dir)) {
  104. if ($handle = opendir($dir)) {
  105. while (false !== ($file = readdir($handle))) {
  106. if($file == ".." || $file == ".") {
  107. continue;
  108. }
  109. $this->rm($dir.'/'.$file);
  110. }
  111. closedir($handle);
  112. }
  113. if (!is_writable($this->getParentDir($dir))) {
  114. $this->makeWritable($this->getParentDir($dir), 0777);
  115. }
  116. rmdir($dir);
  117. } else {
  118. unlink($dir);
  119. }
  120. }
  121. /**
  122. * Remove everything from dir. Make it empty
  123. * @var string $dir
  124. */
  125. public function clean($dir) {
  126. if (!file_exists($dir) || !is_dir($dir)) {
  127. throw new \Exception("Directory doesn't exist: ".$dir, \Exception::UNKNOWN);
  128. }
  129. if ($handle = opendir($dir)) {
  130. while (false !== ($file = readdir($handle))) {
  131. if($file == ".." || $file == ".") {
  132. continue;
  133. }
  134. $this->rm($dir.'/'.$file);
  135. }
  136. closedir($handle);
  137. }
  138. }
  139. /**
  140. * This is special copy. It copies all content from source into destination directory. But not the source folder it self.
  141. * @param string $source
  142. * @param string $dest
  143. * @throws \Exception
  144. */
  145. public function cpContent($source, $dest)
  146. {
  147. if (!is_dir($source) || !is_dir($dest)) {
  148. throw new \Exception("Source or destination is not a folder. Source: ".$source.". Destination: ".$dest."", \Exception::UNKNOWN);
  149. }
  150. $dir_handle=opendir($source);
  151. while($file=readdir($dir_handle)){
  152. if($file!="." && $file!=".."){
  153. if(is_dir($source."/".$file)){
  154. mkdir($dest."/".$file);
  155. $this->cpContent($source."/".$file, $dest."/".$file);
  156. } else {
  157. copy($source."/".$file, $dest."/".$file);
  158. }
  159. }
  160. }
  161. closedir($dir_handle);
  162. }
  163. public function getParentDir($path)
  164. {
  165. $path = $this->removeTrailingSlash($path);
  166. $parentDir = substr($path, 0, strrpos($path, '/') + 1);
  167. return $parentDir;
  168. }
  169. private function throwWritePermissionsError($dir)
  170. {
  171. $errorData = array (
  172. 'file' => $dir
  173. );
  174. throw new \Exception("Can't write directory", \Exception::WRITE_PERMISSION, $errorData);
  175. }
  176. private function getParentPermissions($path)
  177. {
  178. return fileperms($this->getParentDir($path));
  179. }
  180. private function removeTrailingSlash($path)
  181. {
  182. return preg_replace('{/$}', '', $path);
  183. }
  184. public static function handleError($errno, $errstr, $errfile, $errline, array $errcontext)
  185. {
  186. // error was suppressed with the @-operator
  187. if (0 === error_reporting()) {
  188. return false;
  189. }
  190. throw new FileSystemException($errstr, $errno);
  191. }
  192. }