PerforceDriver.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\Filesystem;
  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 $composer_info;
  25. protected $composer_info_identifier;
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function initialize()
  30. {
  31. $this->depot = $this->repoConfig['depot'];
  32. $this->branch = "";
  33. if (isset($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 (isset($this->perforce)) {
  46. return;
  47. }
  48. $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot";
  49. $this->perforce = Perforce::createPerforce($repoConfig, $this->getUrl(), $repoDir, $this->process);
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function getComposerInformation($identifier)
  55. {
  56. if (isset($this->composer_info_identifier)) {
  57. if (strcmp($identifier, $this->composer_info_identifier) === 0) {
  58. return $this->composer_info;
  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->composer_info = $this->perforce->getComposerInformation("//$this->depot/$identifier");
  120. $this->composer_info_identifier = $identifier;
  121. $result = false;
  122. if (isset($this->composer_info)) {
  123. $result = count($this->composer_info) > 0;
  124. }
  125. return $result;
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. public function getContents($url)
  131. {
  132. return false;
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. public static function supports(IOInterface $io, $url, $deep = false)
  138. {
  139. return Perforce::checkServerExists($url, new ProcessExecutor);
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public function cleanup()
  145. {
  146. $this->perforce->cleanupClientSpec();
  147. $this->perforce = null;
  148. }
  149. public function getDepot()
  150. {
  151. return $this->depot;
  152. }
  153. public function getBranch()
  154. {
  155. return $this->branch;
  156. }
  157. public function injectPerforce(Perforce $perforce)
  158. {
  159. $this->perforce = $perforce;
  160. }
  161. }