Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Client.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace GitList\Git;
  3. use Gitter\Client as BaseClient;
  4. class Client extends BaseClient
  5. {
  6. protected $defaultBranch;
  7. protected $hidden;
  8. public function __construct($options = null)
  9. {
  10. parent::__construct($options['path']);
  11. $this->setDefaultBranch($options['default_branch']);
  12. $this->setHidden($options['hidden']);
  13. }
  14. public function getRepositoryFromName($paths, $repo)
  15. {
  16. $repositories = $this->getRepositories($paths);
  17. $path = $repositories[$repo]['path'];
  18. return $this->getRepository($path);
  19. }
  20. /**
  21. * Searches for valid repositories on the specified path
  22. *
  23. * @param array $paths Array of paths where repositories will be searched
  24. * @return array Found repositories, containing their name, path and description sorted
  25. * by repository name
  26. */
  27. public function getRepositories($paths)
  28. {
  29. $allRepositories = array();
  30. foreach ($paths as $path) {
  31. $repositories = $this->recurseDirectory($path);
  32. if (empty($repositories)) {
  33. throw new \RuntimeException('There are no GIT repositories in ' . $path);
  34. }
  35. $allRepositories = array_merge($allRepositories, $repositories);
  36. }
  37. $allRepositories = array_unique($allRepositories, SORT_REGULAR);
  38. uksort($allRepositories, function($k1, $k2) {
  39. return strtolower($k2)<strtolower($k1);
  40. });
  41. return $allRepositories;
  42. }
  43. private function recurseDirectory($path, $topLevel = true)
  44. {
  45. $dir = new \DirectoryIterator($path);
  46. $repositories = array();
  47. foreach ($dir as $file) {
  48. if ($file->isDot()) {
  49. continue;
  50. }
  51. if (strrpos($file->getFilename(), '.') === 0) {
  52. continue;
  53. }
  54. if (!$file->isReadable()) {
  55. continue;
  56. }
  57. if ($file->isDir()) {
  58. $isBare = file_exists($file->getPathname() . '/HEAD');
  59. $isRepository = file_exists($file->getPathname() . '/.git/HEAD');
  60. if ($isRepository || $isBare) {
  61. if (in_array($file->getPathname(), $this->getHidden())) {
  62. continue;
  63. }
  64. if ($isBare) {
  65. $description = $file->getPathname() . '/description';
  66. } else {
  67. $description = $file->getPathname() . '/.git/description';
  68. }
  69. if (file_exists($description)) {
  70. $description = file_get_contents($description);
  71. } else {
  72. $description = null;
  73. }
  74. if (!$topLevel) {
  75. $repoName = $file->getPathInfo()->getFilename() . '/' . $file->getFilename();
  76. } else {
  77. $repoName = $file->getFilename();
  78. }
  79. $repositories[$repoName] = array(
  80. 'name' => $repoName,
  81. 'path' => $file->getPathname(),
  82. 'description' => $description
  83. );
  84. continue;
  85. } else {
  86. $repositories = array_merge($repositories, $this->recurseDirectory($file->getPathname(), false));
  87. }
  88. }
  89. }
  90. return $repositories;
  91. }
  92. /**
  93. * Set default branch as a string.
  94. *
  95. * @param string $branch Name of branch to use when repo's HEAD is detached.
  96. */
  97. protected function setDefaultBranch($branch)
  98. {
  99. $this->defaultBranch = $branch;
  100. return $this;
  101. }
  102. /**
  103. * Return name of default branch as a string.
  104. */
  105. public function getDefaultBranch()
  106. {
  107. return $this->defaultBranch;
  108. }
  109. /**
  110. * Get hidden repository list
  111. *
  112. * @return array List of repositories to hide
  113. */
  114. protected function getHidden()
  115. {
  116. return $this->hidden;
  117. }
  118. /**
  119. * Set the hidden repository list
  120. *
  121. * @param array $hidden List of repositories to hide
  122. */
  123. protected function setHidden($hidden)
  124. {
  125. $this->hidden = $hidden;
  126. return $this;
  127. }
  128. /**
  129. * Overloads the parent::createRepository method for the correct Repository class instance
  130. *
  131. * {@inheritdoc}
  132. */
  133. public function createRepository($path, $bare = null)
  134. {
  135. if (file_exists($path . '/.git/HEAD') && !file_exists($path . '/HEAD')) {
  136. throw new \RuntimeException('A GIT repository already exists at ' . $path);
  137. }
  138. $repository = new Repository($path, $this);
  139. return $repository->create($bare);
  140. }
  141. /**
  142. * Overloads the parent::getRepository method for the correct Repository class instance
  143. *
  144. * {@inheritdoc}
  145. */
  146. public function getRepository($path)
  147. {
  148. if (!file_exists($path) || !file_exists($path . '/.git/HEAD') && !file_exists($path . '/HEAD')) {
  149. throw new \RuntimeException('There is no GIT repository at ' . $path);
  150. }
  151. return new Repository($path, $this);
  152. }
  153. }