VcsDriver.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 $io;
  26. protected $config;
  27. protected $process;
  28. protected $remoteFilesystem;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $url The URL
  33. * @param IOInterface $io The IO instance
  34. * @param Config $config The composer configuration
  35. * @param ProcessExecutor $process Process instance, injectable for mocking
  36. * @param callable $remoteFilesystem Remote Filesystem, injectable for mocking
  37. */
  38. final public function __construct($url, IOInterface $io, Config $config, ProcessExecutor $process = null, $remoteFilesystem = null)
  39. {
  40. $this->url = $url;
  41. $this->io = $io;
  42. $this->config = $config;
  43. $this->process = $process ?: new ProcessExecutor;
  44. $this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io);
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function hasComposerFile($identifier)
  50. {
  51. try {
  52. return (Boolean) $this->getComposerInformation($identifier);
  53. } catch (TransportException $e) {
  54. }
  55. return false;
  56. }
  57. /**
  58. * Get the https or http protocol depending on SSL support.
  59. *
  60. * Call this only if you know that the server supports both.
  61. *
  62. * @return string The correct type of protocol
  63. */
  64. protected function getScheme()
  65. {
  66. if (extension_loaded('openssl')) {
  67. return 'https';
  68. }
  69. return 'http';
  70. }
  71. /**
  72. * Get the remote content.
  73. *
  74. * @param string $url The URL of content
  75. *
  76. * @return mixed The result
  77. */
  78. protected function getContents($url)
  79. {
  80. return $this->remoteFilesystem->getContents($this->url, $url, false);
  81. }
  82. protected static function isLocalUrl($url)
  83. {
  84. return (Boolean) preg_match('{^(file://|/|[a-z]:[\\\\/])}i', $url);
  85. }
  86. }