VcsDriver.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 authorization 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. $this->url = $repoConfig['url'];
  43. $this->originUrl = $repoConfig['url'];
  44. $this->repoConfig = $repoConfig;
  45. $this->io = $io;
  46. $this->config = $config;
  47. $this->process = $process ?: new ProcessExecutor;
  48. $this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io);
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function hasComposerFile($identifier)
  54. {
  55. try {
  56. return (bool) $this->getComposerInformation($identifier);
  57. } catch (TransportException $e) {
  58. }
  59. return false;
  60. }
  61. /**
  62. * Get the https or http protocol depending on SSL support.
  63. *
  64. * Call this only if you know that the server supports both.
  65. *
  66. * @return string The correct type of protocol
  67. */
  68. protected function getScheme()
  69. {
  70. if (extension_loaded('openssl')) {
  71. return 'https';
  72. }
  73. return 'http';
  74. }
  75. /**
  76. * Get the remote content.
  77. *
  78. * @param string $url The URL of content
  79. *
  80. * @return mixed The result
  81. */
  82. protected function getContents($url)
  83. {
  84. return $this->remoteFilesystem->getContents($this->originUrl, $url, false);
  85. }
  86. protected static function isLocalUrl($url)
  87. {
  88. return (bool) preg_match('{^(file://|/|[a-z]:[\\\\/])}i', $url);
  89. }
  90. }