VcsDriver.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Downloader\TransportException;
  13. use Composer\Config;
  14. use Composer\Factory;
  15. use Composer\IO\IOInterface;
  16. use Composer\Util\ProcessExecutor;
  17. use Composer\Util\RemoteFilesystem;
  18. /**
  19. * A driver implementation for driver with authentication interaction.
  20. *
  21. * @author François Pluchino <francois.pluchino@opendisplay.com>
  22. */
  23. abstract class VcsDriver implements VcsDriverInterface
  24. {
  25. protected $url;
  26. protected $originUrl;
  27. protected $repoConfig;
  28. protected $io;
  29. protected $config;
  30. protected $process;
  31. protected $remoteFilesystem;
  32. /**
  33. * Constructor.
  34. *
  35. * @param array $repoConfig The repository configuration
  36. * @param IOInterface $io The IO instance
  37. * @param Config $config The composer configuration
  38. * @param ProcessExecutor $process Process instance, injectable for mocking
  39. * @param RemoteFilesystem $remoteFilesystem Remote Filesystem, injectable for mocking
  40. */
  41. final public function __construct(array $repoConfig, IOInterface $io, Config $config, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null)
  42. {
  43. if (self::isLocalUrl($repoConfig['url'])) {
  44. $repoConfig['url'] = realpath(
  45. preg_replace('/^file:\/\//', '', $repoConfig['url'])
  46. );
  47. }
  48. $this->url = $repoConfig['url'];
  49. $this->originUrl = $repoConfig['url'];
  50. $this->repoConfig = $repoConfig;
  51. $this->io = $io;
  52. $this->config = $config;
  53. $this->process = $process ?: new ProcessExecutor($io);
  54. $this->remoteFilesystem = $remoteFilesystem ?: Factory::createRemoteFilesystem($this->io, $config);
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function hasComposerFile($identifier)
  60. {
  61. try {
  62. return (bool) $this->getComposerInformation($identifier);
  63. } catch (TransportException $e) {
  64. }
  65. return false;
  66. }
  67. /**
  68. * Get the https or http protocol depending on SSL support.
  69. *
  70. * Call this only if you know that the server supports both.
  71. *
  72. * @return string The correct type of protocol
  73. */
  74. protected function getScheme()
  75. {
  76. if (extension_loaded('openssl')) {
  77. return 'https';
  78. }
  79. return 'http';
  80. }
  81. /**
  82. * Get the remote content.
  83. *
  84. * @param string $url The URL of content
  85. *
  86. * @return mixed The result
  87. */
  88. protected function getContents($url)
  89. {
  90. return $this->remoteFilesystem->getContents($this->originUrl, $url, false);
  91. }
  92. /**
  93. * Return if current repository url is local
  94. *
  95. * @param string $url
  96. * @return boolean Repository url is local
  97. */
  98. protected static function isLocalUrl($url)
  99. {
  100. return (bool) preg_match('{^(file://|/|[a-z]:[\\\\/])}i', $url);
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public function cleanup()
  106. {
  107. return;
  108. }
  109. }