PerforceDriver.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Config;
  13. use Composer\IO\IOInterface;
  14. use Composer\Util\ProcessExecutor;
  15. use Composer\Util\Perforce;
  16. /**
  17. * @author Matt Whittom <Matt.Whittom@veteransunited.com>
  18. */
  19. class PerforceDriver extends VcsDriver
  20. {
  21. protected $depot;
  22. protected $branch;
  23. /** @var Perforce */
  24. protected $perforce;
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function initialize()
  29. {
  30. $this->depot = $this->repoConfig['depot'];
  31. $this->branch = '';
  32. if (!empty($this->repoConfig['branch'])) {
  33. $this->branch = $this->repoConfig['branch'];
  34. }
  35. $this->initPerforce($this->repoConfig);
  36. $this->perforce->p4Login();
  37. $this->perforce->checkStream();
  38. $this->perforce->writeP4ClientSpec();
  39. $this->perforce->connectClient();
  40. return true;
  41. }
  42. private function initPerforce($repoConfig)
  43. {
  44. if (!empty($this->perforce)) {
  45. return;
  46. }
  47. $repoDir = $this->config->get('cache-vcs-dir') . '/' . $this->depot;
  48. $this->perforce = Perforce::create($repoConfig, $this->getUrl(), $repoDir, $this->process, $this->io);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getFileContent($file, $identifier)
  54. {
  55. return $this->perforce->getFileContent($file, $identifier);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getChangeDate($identifier)
  61. {
  62. return null;
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getRootIdentifier()
  68. {
  69. return $this->branch;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function getBranches()
  75. {
  76. $branches = $this->perforce->getBranches();
  77. return $branches;
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public function getTags()
  83. {
  84. $tags = $this->perforce->getTags();
  85. return $tags;
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function getDist($identifier)
  91. {
  92. return null;
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function getSource($identifier)
  98. {
  99. $source = array(
  100. 'type' => 'perforce',
  101. 'url' => $this->repoConfig['url'],
  102. 'reference' => $identifier,
  103. 'p4user' => $this->perforce->getUser(),
  104. );
  105. return $source;
  106. }
  107. /**
  108. * {@inheritDoc}
  109. */
  110. public function getUrl()
  111. {
  112. return $this->url;
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. public function hasComposerFile($identifier)
  118. {
  119. $composerInfo = $this->perforce->getComposerInformation('//' . $this->depot . '/' . $identifier);
  120. $composerInfoIdentifier = $identifier;
  121. return !empty($composerInfo);
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. public function getContents($url)
  127. {
  128. return false;
  129. }
  130. /**
  131. * {@inheritDoc}
  132. */
  133. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  134. {
  135. if ($deep || preg_match('#\b(perforce|p4)\b#i', $url)) {
  136. return Perforce::checkServerExists($url, new ProcessExecutor($io));
  137. }
  138. return false;
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. public function cleanup()
  144. {
  145. $this->perforce->cleanupClientSpec();
  146. $this->perforce = null;
  147. }
  148. public function getDepot()
  149. {
  150. return $this->depot;
  151. }
  152. public function getBranch()
  153. {
  154. return $this->branch;
  155. }
  156. }