PerforceDriver.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. protected $perforce;
  24. protected $composerInfo;
  25. protected $composerInfoIdentifier;
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function initialize()
  30. {
  31. $this->depot = $this->repoConfig['depot'];
  32. $this->branch = '';
  33. if (!empty($this->repoConfig['branch'])) {
  34. $this->branch = $this->repoConfig['branch'];
  35. }
  36. $this->initPerforce($this->repoConfig);
  37. $this->perforce->p4Login($this->io);
  38. $this->perforce->checkStream($this->depot);
  39. $this->perforce->writeP4ClientSpec();
  40. $this->perforce->connectClient();
  41. return true;
  42. }
  43. private function initPerforce($repoConfig)
  44. {
  45. if (!empty($this->perforce)) {
  46. return;
  47. }
  48. $repoDir = $this->config->get('cache-vcs-dir') . '/' . $this->depot;
  49. $this->perforce = Perforce::create($repoConfig, $this->getUrl(), $repoDir, $this->process, $this->io);
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function getComposerInformation($identifier)
  55. {
  56. if (!empty($this->composerInfoIdentifier)) {
  57. if (strcmp($identifier, $this->composerInfoIdentifier) === 0) {
  58. return $this->composerInfo;
  59. }
  60. }
  61. $composer_info = $this->perforce->getComposerInformation($identifier);
  62. return $composer_info;
  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. $this->composerInfo = $this->perforce->getComposerInformation('//' . $this->depot . '/' . $identifier);
  120. $this->composerInfoIdentifier = $identifier;
  121. $result = false;
  122. return !empty($this->composerInfo);
  123. if (!empty($this->composerInfo)) {
  124. $result = count($this->composerInfo) > 0;
  125. }
  126. return $result;
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. public function getContents($url)
  132. {
  133. return false;
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  139. {
  140. if ($deep || preg_match('#\b(perforce|p4)\b#i', $url)) {
  141. return Perforce::checkServerExists($url, new ProcessExecutor($io));
  142. }
  143. return false;
  144. }
  145. /**
  146. * {@inheritDoc}
  147. */
  148. public function cleanup()
  149. {
  150. $this->perforce->cleanupClientSpec();
  151. $this->perforce = null;
  152. }
  153. public function getDepot()
  154. {
  155. return $this->depot;
  156. }
  157. public function getBranch()
  158. {
  159. return $this->branch;
  160. }
  161. public function setPerforce(Perforce $perforce)
  162. {
  163. $this->perforce = $perforce;
  164. }
  165. }