PerforceDriver.php 4.0 KB

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