VcsDriver.php 3.0 KB

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