VcsDriver.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Repository\Vcs;
  12. use Composer\Cache;
  13. use Composer\Downloader\TransportException;
  14. use Composer\Config;
  15. use Composer\Factory;
  16. use Composer\IO\IOInterface;
  17. use Composer\Json\JsonFile;
  18. use Composer\Util\ProcessExecutor;
  19. use Composer\Util\HttpDownloader;
  20. use Composer\Util\Filesystem;
  21. use Composer\Util\Http\Response;
  22. /**
  23. * A driver implementation for driver with authentication interaction.
  24. *
  25. * @author François Pluchino <francois.pluchino@opendisplay.com>
  26. */
  27. abstract class VcsDriver implements VcsDriverInterface
  28. {
  29. /** @var string */
  30. protected $url;
  31. /** @var string */
  32. protected $originUrl;
  33. /** @var array */
  34. protected $repoConfig;
  35. /** @var IOInterface */
  36. protected $io;
  37. /** @var Config */
  38. protected $config;
  39. /** @var ProcessExecutor */
  40. protected $process;
  41. /** @var HttpDownloader */
  42. protected $httpDownloader;
  43. /** @var array */
  44. protected $infoCache = array();
  45. /** @var Cache */
  46. protected $cache;
  47. /**
  48. * Constructor.
  49. *
  50. * @param array $repoConfig The repository configuration
  51. * @param IOInterface $io The IO instance
  52. * @param Config $config The composer configuration
  53. * @param HttpDownloader $httpDownloader Remote Filesystem, injectable for mocking
  54. * @param ProcessExecutor $process Process instance, injectable for mocking
  55. */
  56. final public function __construct(array $repoConfig, IOInterface $io, Config $config, HttpDownloader $httpDownloader, ProcessExecutor $process)
  57. {
  58. if (Filesystem::isLocalPath($repoConfig['url'])) {
  59. $repoConfig['url'] = Filesystem::getPlatformPath($repoConfig['url']);
  60. }
  61. $this->url = $repoConfig['url'];
  62. $this->originUrl = $repoConfig['url'];
  63. $this->repoConfig = $repoConfig;
  64. $this->io = $io;
  65. $this->config = $config;
  66. $this->httpDownloader = $httpDownloader;
  67. $this->process = $process;
  68. }
  69. /**
  70. * Returns whether or not the given $identifier should be cached or not.
  71. *
  72. * @param string $identifier
  73. * @return bool
  74. */
  75. protected function shouldCache($identifier)
  76. {
  77. return $this->cache && preg_match('{[a-f0-9]{40}}i', $identifier);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getComposerInformation($identifier)
  83. {
  84. if (!isset($this->infoCache[$identifier])) {
  85. if ($this->shouldCache($identifier) && $res = $this->cache->read($identifier)) {
  86. return $this->infoCache[$identifier] = JsonFile::parseJson($res);
  87. }
  88. $composer = $this->getBaseComposerInformation($identifier);
  89. if ($this->shouldCache($identifier)) {
  90. $this->cache->write($identifier, json_encode($composer));
  91. }
  92. $this->infoCache[$identifier] = $composer;
  93. }
  94. return $this->infoCache[$identifier];
  95. }
  96. protected function getBaseComposerInformation($identifier)
  97. {
  98. $composerFileContent = $this->getFileContent('composer.json', $identifier);
  99. if (!$composerFileContent) {
  100. return null;
  101. }
  102. $composer = JsonFile::parseJson($composerFileContent, $identifier . ':composer.json');
  103. if (empty($composer['time']) && $changeDate = $this->getChangeDate($identifier)) {
  104. $composer['time'] = $changeDate->format(DATE_RFC3339);
  105. }
  106. return $composer;
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public function hasComposerFile($identifier)
  112. {
  113. try {
  114. return (bool) $this->getComposerInformation($identifier);
  115. } catch (TransportException $e) {
  116. }
  117. return false;
  118. }
  119. /**
  120. * Get the https or http protocol depending on SSL support.
  121. *
  122. * Call this only if you know that the server supports both.
  123. *
  124. * @return string The correct type of protocol
  125. */
  126. protected function getScheme()
  127. {
  128. if (extension_loaded('openssl')) {
  129. return 'https';
  130. }
  131. return 'http';
  132. }
  133. /**
  134. * Get the remote content.
  135. *
  136. * @param string $url The URL of content
  137. *
  138. * @return Response
  139. */
  140. protected function getContents($url)
  141. {
  142. $options = isset($this->repoConfig['options']) ? $this->repoConfig['options'] : array();
  143. return $this->httpDownloader->get($url, $options);
  144. }
  145. /**
  146. * {@inheritDoc}
  147. */
  148. public function cleanup()
  149. {
  150. return;
  151. }
  152. }