PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Gitten/LocalFile.php

https://bitbucket.org/kayahr/gitten
PHP | 276 lines | 128 code | 22 blank | 126 comment | 24 complexity | 3111641c8a970161f65074e9bb010565 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (C) 2013 Klaus Reimer <k@ailis.de>
  4. * See LICENSE.md for licensing information.
  5. */
  6. namespace Gitten;
  7. /**
  8. * A file or directory on the local file system.
  9. *
  10. * @author Klaus Reimer <k@ailis.de>
  11. */
  12. final class LocalFile extends File
  13. {
  14. /** The path to the file relative to the repository base directory. */
  15. private $path;
  16. /** The cached description. Access it with getDescription(). */
  17. private $description = null;
  18. /** The cached content. Access it with getContent(). */
  19. private $content = null;
  20. /**
  21. * Constructs a new file.
  22. *
  23. * @param string $path
  24. * The path to the file relative to the repository base
  25. * directory. If not specified then the file points
  26. * at the repository base directory.
  27. */
  28. public function __construct($path = ".")
  29. {
  30. $this->path = $path;
  31. }
  32. /**
  33. * Returns the path to this file. The path is relative to the repository
  34. * base directory. When the file is the repository base directory itself
  35. * then this method returns a dot character.
  36. *
  37. * @return string
  38. * The path to this file relative to the repository base
  39. * directory.
  40. */
  41. public function getPath()
  42. {
  43. return $this->path;
  44. }
  45. /**
  46. * Returns the URL to this file.
  47. *
  48. * @return string
  49. * The URL.
  50. */
  51. public function getUrl()
  52. {
  53. return PHP_BASEURL . "/" . $this->path;
  54. }
  55. /**
  56. * Returns the file name.
  57. *
  58. * @return string
  59. * The file name.
  60. */
  61. public function getName()
  62. {
  63. return basename($this->path);
  64. }
  65. /**
  66. * Returns the absolute path to the file.
  67. *
  68. * @return string
  69. * The absolute path to the file.
  70. */
  71. public function getAbsolutePath()
  72. {
  73. global $cfg;
  74. $repoBase = $cfg->getRepoBase();
  75. if ($this->isRoot())
  76. return rtrim($repoBase, "/");
  77. else
  78. return rtrim($repoBase, "/") . "/" . $this->path;
  79. }
  80. /**
  81. * Checks if file is a directory.
  82. *
  83. * @return boolean
  84. * True if file is a directory, false if not.
  85. */
  86. public function isDirectory()
  87. {
  88. return is_dir($this->getAbsolutePath());
  89. }
  90. /**
  91. * Checks if this file is a Git repository.
  92. *
  93. * @return boolean
  94. * True if file is a Git repository, false if not.
  95. */
  96. public function isRepository()
  97. {
  98. $absPath = $this->getAbsolutePath();
  99. if (!is_dir($absPath)) return false;
  100. if (preg_match("/.*\\.git\$/", $this->path)) return true;
  101. if (is_dir($absPath . "/.git")) return true;
  102. return false;
  103. }
  104. /**
  105. * Returns the file size.
  106. *
  107. * @return FileSize
  108. * The file size.
  109. */
  110. public function getSize()
  111. {
  112. return new FileSize(filesize($this->getAbsolutePath()));
  113. }
  114. /**
  115. * Returns the last modified time.
  116. *
  117. * @return DateTime
  118. * The last modified time.
  119. */
  120. public function getLastModified()
  121. {
  122. return new DateTime(filemtime($this->getAbsolutePath()));
  123. }
  124. /**
  125. * Checks if this is the root directory (The one pointing at the
  126. * repository base directory).
  127. *
  128. * @return boolean
  129. * True if root directory, false if not.
  130. */
  131. public function isRoot()
  132. {
  133. return $this->path == ".";
  134. }
  135. /**
  136. * Returns the parent directory. Null if there is no parent because this
  137. * is the root directory.
  138. *
  139. * @return LocalFile
  140. * The parent directory or null if none.
  141. */
  142. public function getParent()
  143. {
  144. if ($this->isRoot()) return null;
  145. return new LocalFile(dirname($this->path));
  146. }
  147. /**
  148. * Returns all parent directories without the root directory.
  149. *
  150. * @return LocalFile[]
  151. * All parent directories.
  152. */
  153. public function getParents()
  154. {
  155. $parents = array();
  156. if ($this->isRoot()) return $parents;
  157. $parent = $this->getParent();
  158. while (!$parent->isRoot())
  159. {
  160. $parents[] = $parent;
  161. $parent = $parent->getParent();
  162. }
  163. return array_reverse($parents);
  164. }
  165. /**
  166. * Returns the children files of this directory. Empty if there are
  167. * no children or if the current file is not a directory. The children
  168. * are sorted alphabetically with the directories at the top.
  169. *
  170. * @return LocalFile[] The children files. May be empty.
  171. */
  172. public function getChildren()
  173. {
  174. $children = array();
  175. if (!$this->isDirectory()) return $children;
  176. $dir = opendir($this->getAbsolutePath());
  177. while ($filename = readdir($dir))
  178. {
  179. if ($filename == "." || $filename == "..") continue;
  180. $children[] = new LocalFile($this->path . "/" . $filename);
  181. }
  182. usort($children, function(LocalFile $a, LocalFile $b) {
  183. if ($a->isDirectory() && !$b->isDirectory()) return -1;
  184. if (!$a->isDirectory() && $b->isDirectory()) return 1;
  185. return $a->getName() > $b->getName() ? 1 : -1;
  186. });
  187. return $children;
  188. }
  189. /**
  190. * Returns the child file with the specified name.
  191. *
  192. * @param string $name
  193. * The name of the child.
  194. * @return LocalFile
  195. * The child file.
  196. */
  197. public function getChild($name)
  198. {
  199. if ($this->isRoot())
  200. return new LocalFile($name);
  201. else
  202. return new LocalFile($this->path . "/" . $name);
  203. }
  204. /**
  205. * Returns the file type.
  206. *
  207. * @return string
  208. * The file type.
  209. */
  210. public function getType()
  211. {
  212. if ($this->isRepository()) return "repository";
  213. if ($this->isDirectory()) return "directory";
  214. return "file";
  215. }
  216. /**
  217. * Returns the description. Only repositories can have a description.
  218. * So for normal directories and files this method always returns an empty
  219. * string.
  220. *
  221. * @return string
  222. * The description.
  223. */
  224. public function getDescription()
  225. {
  226. if (is_null($this->description))
  227. {
  228. if (!$this->isRepository())
  229. {
  230. $this->description = "";
  231. }
  232. else
  233. {
  234. $repo = new Repo($this);
  235. $this->description = $repo->getDescription();
  236. }
  237. }
  238. return $this->description;
  239. }
  240. /**
  241. * Returns the raw content of the file.
  242. *
  243. * @return string
  244. * The raw file content.
  245. */
  246. public function getContent()
  247. {
  248. if (is_null($this->content))
  249. {
  250. $this->content = file_get_contents($this->getAbsolutePath());
  251. }
  252. return $this->content;
  253. }
  254. }