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

/php/System/Dir.php

https://github.com/mattmccormick/Matt-Library
PHP | 172 lines | 105 code | 35 blank | 32 comment | 12 complexity | c50b44f17b751fdc77f90f0e69a7906b MD5 | raw file
  1. <?php
  2. namespace Matt\System;
  3. use Matt\System\File\Text;
  4. use Matt\System;
  5. use Matt\System\File\Archive\Gz;
  6. class Dir extends System
  7. {
  8. private $dir;
  9. public function __construct($directory, $create = false)
  10. {
  11. if ($create && !is_dir($directory)) {
  12. mkdir($directory, 0777, true);
  13. }
  14. if (!is_dir($directory)) {
  15. throw new \Exception("{$directory} is not a directory");
  16. }
  17. $this->dir = $this->addTrailingSlash($directory);
  18. }
  19. /**
  20. * Gets the directory with the trailing slash
  21. * @return string
  22. */
  23. public function __toString()
  24. {
  25. return $this->dir;
  26. }
  27. public function getBasename()
  28. {
  29. return basename($this);
  30. }
  31. /**
  32. * @return Dir
  33. */
  34. public function getParent()
  35. {
  36. return new Dir(dirname($this));
  37. }
  38. /**
  39. * @param string $name
  40. * @return Dir
  41. */
  42. public function getChild($name)
  43. {
  44. return new Dir($this . $name);
  45. }
  46. /**
  47. * @return Gz
  48. */
  49. public function backup()
  50. {
  51. return Gz::compressDir($this);
  52. }
  53. public function getWithoutTrailingSlash()
  54. {
  55. return rtrim($this, DIRECTORY_SEPARATOR);
  56. }
  57. public function delete()
  58. {
  59. foreach ($this->getAllFiles() as $obj) {
  60. $obj->delete();
  61. }
  62. $result = rmdir($this);
  63. $this->dir = null;
  64. return $result;
  65. }
  66. public function copy($destination, $files_only = false)
  67. {
  68. $result = true;
  69. $dir = new Dir($destination, true);
  70. foreach ($this->getAllFiles($files_only) as $obj) {
  71. $out = $obj->copy($dir . $obj->getBasename());
  72. $result = $result && $out; // will be false if any fail
  73. }
  74. return $result;
  75. }
  76. /**
  77. * Get all the files that match $pattern
  78. * @param string $pattern
  79. * @return array of System
  80. */
  81. public function getFiles($pattern)
  82. {
  83. $result = array();
  84. foreach (glob($this . $pattern) as $file) {
  85. $result[] = System::factory($file);
  86. }
  87. return $result;
  88. }
  89. /**
  90. *
  91. * @param string $filename
  92. * @return File
  93. */
  94. public function getFile($filename)
  95. {
  96. return System::factory($this->dir . $filename);
  97. }
  98. public function hasFile($filename)
  99. {
  100. return is_file($this->dir . $filename);
  101. }
  102. /**
  103. * @param string $name
  104. * @param string $data
  105. * @return Text
  106. */
  107. public function addTextFile($name, $data)
  108. {
  109. return Text::create($this . $name, $data);
  110. }
  111. private function addTrailingSlash($path)
  112. {
  113. $real = realpath($path);
  114. if (!$real) {
  115. throw new \Exception("{$path} could not be found. Is the directory created?");
  116. }
  117. return $real . DIRECTORY_SEPARATOR;
  118. }
  119. /**
  120. * @return array of System
  121. */
  122. private function getAllFiles($files_only = false)
  123. {
  124. $files = scandir($this);
  125. $result = array();
  126. foreach ($files as $name) {
  127. if ($name == '.' || $name == '..') {
  128. continue; // ignore these special files as they could be dangerous
  129. }
  130. $sys = System::factory($this . $name);
  131. if (($files_only && $sys->is_file()) || !$files_only) {
  132. $result[] = $sys;
  133. }
  134. }
  135. return $result;
  136. }
  137. }