/git/src/TQ/Git/StreamWrapper/PathInformation.php

https://github.com/WPsites/WPide · PHP · 235 lines · 79 code · 22 blank · 134 comment · 3 complexity · 2b13a55583e45288bfb157c771430a1a MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright (C) 2011 by TEQneers GmbH & Co. KG
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. /**
  24. * Git Streamwrapper for PHP
  25. *
  26. * @category TQ
  27. * @package TQ_Git
  28. * @subpackage StreamWrapper
  29. * @copyright Copyright (C) 2011 by TEQneers GmbH & Co. KG
  30. */
  31. /**
  32. * @namespace
  33. */
  34. namespace TQ\Git\StreamWrapper;
  35. use TQ\Git\Repository\Repository;
  36. use TQ\Git\Cli\Binary;
  37. /**
  38. * Handles decomposition of a given Git streamwrapper path
  39. *
  40. * @author Stefan Gehrig <gehrigteqneers.de>
  41. * @category TQ
  42. * @package TQ_Git
  43. * @subpackage StreamWrapper
  44. * @copyright Copyright (C) 2011 by TEQneers GmbH & Co. KG
  45. */
  46. class PathInformation
  47. {
  48. /**
  49. * The Git repository
  50. *
  51. * @var Repository
  52. */
  53. protected $repository;
  54. /**
  55. * The Git URL
  56. *
  57. * @var string
  58. */
  59. protected $url;
  60. /**
  61. * The absolute path to the resource
  62. *
  63. * @var string
  64. */
  65. protected $fullPath;
  66. /**
  67. * The relative path to the reosurce based on the repository path
  68. *
  69. * @var string
  70. */
  71. protected $localPath;
  72. /**
  73. * The version ref
  74. *
  75. * @var string
  76. */
  77. protected $ref;
  78. /**
  79. * Additional arguments
  80. *
  81. * @var array
  82. */
  83. protected $arguments;
  84. /**
  85. * Returns path information for a given stream URL
  86. *
  87. * @param string $url The URL
  88. * @param string $protocol The protocol registered
  89. * @return array An array containing information about the path
  90. */
  91. public static function parseUrl($url, $protocol)
  92. {
  93. // normalize directory separators
  94. $path = str_replace(DIRECTORY_SEPARATOR, '/', $url);
  95. $path = ltrim(substr($path, strlen($protocol) + 3), '/');
  96. //fix path if fragment has been munged into the path (e.g. when using the RecursiveIterator)
  97. $path = preg_replace('~^(.+?)(#[^/]+)(.*)$~', '$1$3$2', $path);
  98. $url = parse_url($protocol.'://'.$path);
  99. if (preg_match('~^\w:.+~', $path)) {
  100. $url['path'] = $url['host'].':'.$url['path'];
  101. } else {
  102. $url['path'] = '/'.$url['host'].$url['path'];
  103. }
  104. unset($url['host']);
  105. return $url;
  106. }
  107. /**
  108. * Creates a new path information instance from a given URL
  109. *
  110. * @param string $url The URL
  111. * @param string $protocol The protocol registered
  112. * @param Binary $binary The Git binary
  113. */
  114. public function __construct($url, $protocol, Binary $binary)
  115. {
  116. $url = self::parseUrl($url, $protocol);
  117. $this->fullPath = $url['path'];
  118. $this->repository = Repository::open($this->fullPath, $binary, false);
  119. $this->localPath = $this->repository->resolveLocalPath($this->fullPath);
  120. $this->ref = (array_key_exists('fragment', $url)) ? $url['fragment'] : 'HEAD';
  121. $arguments = array();
  122. if (array_key_exists('query', $url)) {
  123. parse_str($url['query'], $arguments);
  124. }
  125. $this->arguments = $arguments;
  126. $this->url = $protocol.'://'.$this->fullPath
  127. .'#'.$this->ref
  128. .'?'.http_build_query($this->arguments);
  129. }
  130. /**
  131. * Returns the Git URL
  132. *
  133. * @return string
  134. */
  135. public function getUrl()
  136. {
  137. return $this->url;
  138. }
  139. /**
  140. * Returns the Git repository instance
  141. *
  142. * @return Repository
  143. */
  144. public function getRepository()
  145. {
  146. return $this->repository;
  147. }
  148. /**
  149. * Returns the absolute repository path
  150. *
  151. * @return string
  152. */
  153. public function getRepositoryPath()
  154. {
  155. return $this->getRepository()->getRepositoryPath();
  156. }
  157. /**
  158. * Returns the absolute path to the resource
  159. *
  160. * @return string
  161. */
  162. public function getFullPath()
  163. {
  164. return $this->fullPath;
  165. }
  166. /**
  167. * Returns the relative path to the resource based on the repository path
  168. *
  169. * @return string
  170. */
  171. public function getLocalPath()
  172. {
  173. return $this->localPath;
  174. }
  175. /**
  176. * Returns the version ref
  177. *
  178. * @return string
  179. */
  180. public function getRef()
  181. {
  182. return $this->ref;
  183. }
  184. /**
  185. * Returns the additional arguments given
  186. *
  187. * @return array
  188. */
  189. public function getArguments()
  190. {
  191. return $this->arguments;
  192. }
  193. /**
  194. * Checks if the given argument exists
  195. *
  196. * @param string $argument The argument name
  197. * @return boolean
  198. */
  199. public function hasArgument($argument)
  200. {
  201. return array_key_exists($argument, $this->arguments);
  202. }
  203. /**
  204. * Returns the given argument from the argument collection
  205. *
  206. * @param string $argument The argument name
  207. * @return string|null The argument value or NULL if the argument does not exist
  208. */
  209. public function getArgument($argument)
  210. {
  211. return ($this->hasArgument($argument)) ? $this->arguments[$argument] : null;
  212. }
  213. }