/framework/Vcs/lib/Horde/Vcs/File/Base.php

https://github.com/ewandor/horde · PHP · 315 lines · 145 code · 40 blank · 130 comment · 14 complexity · b9a5123d5adb609fd729d7cdb7d9ccc7 MD5 · raw file

  1. <?php
  2. /**
  3. * Base file class.
  4. *
  5. * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @package Vcs
  11. */
  12. abstract class Horde_Vcs_File_Base
  13. {
  14. /**
  15. * The current driver.
  16. *
  17. * @var string
  18. */
  19. protected $_driver;
  20. /**
  21. * The directory of this file.
  22. *
  23. * @var string
  24. */
  25. protected $_dir;
  26. /**
  27. * The name of this file.
  28. *
  29. * @var string
  30. */
  31. protected $_name;
  32. /**
  33. * TODO
  34. *
  35. * @var array
  36. */
  37. protected $_logs = array();
  38. /**
  39. * TODO
  40. *
  41. * @var array
  42. */
  43. protected $_revs = array();
  44. /**
  45. * TODO
  46. */
  47. protected $_rep;
  48. /**
  49. * TODO
  50. *
  51. * @var string
  52. */
  53. protected $_branch = null;
  54. /**
  55. * Have we initalized logs and revisions?
  56. *
  57. * @var boolean
  58. */
  59. protected $_initialized = false;
  60. /**
  61. * Constructor.
  62. *
  63. * @param string $filename Full path (inside the source root) to this file.
  64. * @param array $opts Additional parameters:
  65. * - 'branch': (string)
  66. */
  67. public function __construct($filename, $opts = array())
  68. {
  69. $this->_name = basename($filename);
  70. $this->_dir = dirname($filename);
  71. if ($this->_dir == '.') {
  72. $this->_dir = '';
  73. }
  74. if (!empty($opts['branch'])) {
  75. $this->_branch = $opts['branch'];
  76. }
  77. }
  78. /**
  79. * When serializing, don't return the repository object
  80. */
  81. public function __sleep()
  82. {
  83. return array_diff(array_keys(get_object_vars($this)), array('_rep'));
  84. }
  85. abstract protected function _init();
  86. protected function _ensureInitialized()
  87. {
  88. if (!$this->_initialized) {
  89. $this->_initialized = true;
  90. $this->_init();
  91. }
  92. }
  93. /**
  94. * TODO
  95. */
  96. public function setRepository($rep)
  97. {
  98. $this->_rep = $rep;
  99. }
  100. /**
  101. * TODO - better name, wrap an object around this?
  102. */
  103. public function getBlob($revision)
  104. {
  105. return $this->_rep->checkout($this->getPath(), $revision);
  106. }
  107. /**
  108. * Has the file been deleted?
  109. *
  110. * @return boolean Is this file deleted?
  111. */
  112. public function isDeleted()
  113. {
  114. return false;
  115. }
  116. /**
  117. * Returns name of the current file without the repository extensions.
  118. *
  119. * @return string Filename without repository extension
  120. */
  121. public function getFileName()
  122. {
  123. return $this->_name;
  124. }
  125. /**
  126. * Returns the last revision of the current file on the HEAD branch.
  127. *
  128. * @return string Last revision of the current file.
  129. * @throws Horde_Vcs_Exception
  130. */
  131. public function getRevision()
  132. {
  133. $this->_ensureInitialized();
  134. if (!isset($this->_revs[0])) {
  135. throw new Horde_Vcs_Exception('No revisions');
  136. }
  137. return $this->_revs[0];
  138. }
  139. /**
  140. * Returns the revision before the specified revision.
  141. *
  142. * @param string $rev A revision.
  143. *
  144. * @return string The previous revision or null if the first revision.
  145. */
  146. public function getPreviousRevision($rev)
  147. {
  148. $this->_ensureInitialized();
  149. $key = array_search($rev, $this->_revs);
  150. return (($key !== false) && isset($this->_revs[$key + 1]))
  151. ? $this->_revs[$key + 1]
  152. : null;
  153. }
  154. /**
  155. * @param string $rev The revision identifier.
  156. */
  157. protected function _getLog($rev = null)
  158. {
  159. $class = 'Horde_Vcs_Log_' . $this->_driver;
  160. if (!is_null($rev) && !empty($this->_cache)) {
  161. $cacheId = implode('|', array($class, $this->sourceroot, $fl->getPath(), $rev, $this->_cacheVersion));
  162. // Individual revisions can be cached forever
  163. if ($this->_cache->exists($cacheId, 0)) {
  164. $ob = unserialize($this->_cache->get($cacheId, 0));
  165. }
  166. }
  167. if (empty($ob) || !$ob) {
  168. $ob = new $class($rev);
  169. }
  170. $ob->setRepository($this->_rep);
  171. $ob->setFile($this);
  172. if (!is_null($rev) && !empty($this->_cache)) {
  173. $this->_cache->set($cacheId, serialize($ob));
  174. }
  175. return $ob;
  176. }
  177. /**
  178. * Returns a log object for the most recent log entry of this file.
  179. *
  180. * @return Horde_Vcs_QuickLog Log object of the last entry in the file.
  181. * @throws Horde_Vcs_Exception
  182. */
  183. abstract public function getLastLog();
  184. /**
  185. * Sort the list of Horde_Vcs_Log objects that this file contains.
  186. *
  187. * @param integer $how Horde_Vcs::SORT_REV (sort by revision),
  188. * Horde_Vcs::SORT_NAME (sort by author name), or
  189. * Horde_Vcs::SORT_AGE (sort by commit date).
  190. */
  191. public function applySort($how = Horde_Vcs::SORT_REV)
  192. {
  193. $this->_ensureInitialized();
  194. switch ($how) {
  195. case Horde_Vcs::SORT_NAME:
  196. $func = 'Name';
  197. break;
  198. case Horde_Vcs::SORT_AGE:
  199. $func = 'Age';
  200. break;
  201. case Horde_Vcs::SORT_REV:
  202. default:
  203. $func = 'Revision';
  204. break;
  205. }
  206. uasort($this->_logs, array($this, 'sortBy' . $func));
  207. return true;
  208. }
  209. /**
  210. * The sortBy*() functions are internally used by applySort.
  211. */
  212. public function sortByRevision($a, $b)
  213. {
  214. return $this->_rep->cmp($b->getRevision(), $a->getRevision());
  215. }
  216. public function sortByAge($a, $b)
  217. {
  218. return $b->getDate() - $a->getDate();
  219. }
  220. public function sortByName($a, $b)
  221. {
  222. return strcmp($a->getAuthor(), $b->getAuthor());
  223. }
  224. /**
  225. * Return the filename relative to its sourceroot.
  226. *
  227. * @return string Pathname relative to the sourceroot.
  228. */
  229. public function getSourcerootPath()
  230. {
  231. return ltrim($this->_dir . '/' . $this->_name, '/');
  232. }
  233. /**
  234. * Return the "base" filename (i.e. the filename needed by the various
  235. * command line utilities).
  236. *
  237. * @return string A filename.
  238. */
  239. public function getPath()
  240. {
  241. return $this->_rep->sourceroot . '/' . $this->getSourcerootPath();
  242. }
  243. /**
  244. * TODO
  245. */
  246. public function getBranches()
  247. {
  248. return array();
  249. }
  250. /**
  251. * TODO
  252. */
  253. public function getLog($rev = null)
  254. {
  255. $this->_ensureInitialized();
  256. return is_null($rev)
  257. ? $this->_logs
  258. : (isset($this->_logs[$rev]) ? $this->_logs[$rev] : null);
  259. }
  260. /**
  261. * TODO
  262. */
  263. public function revisionCount()
  264. {
  265. $this->_ensureInitialized();
  266. return count($this->_revs);
  267. }
  268. /**
  269. * TODO
  270. */
  271. public function getTags()
  272. {
  273. return array();
  274. }
  275. }