PerforceDownloader.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Downloader;
  12. use Composer\Package\PackageInterface;
  13. use Composer\Repository\VcsRepository;
  14. use Composer\Util\Perforce;
  15. /**
  16. * @author Matt Whittom <Matt.Whittom@veteransunited.com>
  17. */
  18. class PerforceDownloader extends VcsDownloader
  19. {
  20. /** @var Perforce */
  21. protected $perforce;
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function doDownload(PackageInterface $package, $path, $url)
  26. {
  27. $ref = $package->getSourceReference();
  28. $label = $this->getLabelFromSourceReference($ref);
  29. $this->io->writeError(' Cloning ' . $ref);
  30. $this->initPerforce($package, $path, $url);
  31. $this->perforce->setStream($ref);
  32. $this->perforce->p4Login();
  33. $this->perforce->writeP4ClientSpec();
  34. $this->perforce->connectClient();
  35. $this->perforce->syncCodeBase($label);
  36. $this->perforce->cleanupClientSpec();
  37. }
  38. private function getLabelFromSourceReference($ref)
  39. {
  40. $pos = strpos($ref, '@');
  41. if (false !== $pos) {
  42. return substr($ref, $pos + 1);
  43. }
  44. return null;
  45. }
  46. public function initPerforce(PackageInterface $package, $path, $url)
  47. {
  48. if (!empty($this->perforce)) {
  49. $this->perforce->initializePath($path);
  50. return;
  51. }
  52. $repository = $package->getRepository();
  53. $repoConfig = null;
  54. if ($repository instanceof VcsRepository) {
  55. $repoConfig = $this->getRepoConfig($repository);
  56. }
  57. $this->perforce = Perforce::create($repoConfig, $url, $path, $this->process, $this->io);
  58. }
  59. private function getRepoConfig(VcsRepository $repository)
  60. {
  61. return $repository->getRepoConfig();
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url)
  67. {
  68. $this->doDownload($target, $path, $url);
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function getLocalChanges(PackageInterface $package, $path)
  74. {
  75. $this->io->writeError('Perforce driver does not check for local changes before overriding', true);
  76. return;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. protected function getCommitLogs($fromReference, $toReference, $path)
  82. {
  83. $commitLogs = $this->perforce->getCommitLogs($fromReference, $toReference);
  84. return $commitLogs;
  85. }
  86. public function setPerforce($perforce)
  87. {
  88. $this->perforce = $perforce;
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. protected function hasMetadataRepository($path)
  94. {
  95. return true;
  96. }
  97. }