PerforceDownloader.php 2.7 KB

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