PerforceDriver.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * Contributor: matt-whittom
  9. * Date: 7/17/13
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. namespace Composer\Repository\Vcs;
  15. #use Composer\Downloader\TransportException;
  16. #use Composer\Json\JsonFile;
  17. #use Composer\Cache;
  18. use Composer\IO\IOInterface;
  19. use Composer\Util\Filesystem;
  20. use Composer\Util\Perforce;
  21. #use Composer\Util\RemoteFilesystem;
  22. #use Composer\Util\GitHub;
  23. /**
  24. * @author matt-whittom <>
  25. */
  26. class PerforceDriver extends VcsDriver
  27. {
  28. // protected $cache;
  29. // protected $owner;
  30. // protected $repository;
  31. // protected $tags;
  32. // protected $branches;
  33. protected $rootIdentifier = 'mainline';
  34. protected $repoDir;
  35. // protected $hasIssues;
  36. // protected $infoCache = array();
  37. // protected $isPrivate = false;
  38. protected $depot;
  39. protected $p4client;
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function initialize()
  44. {
  45. print ("PerforceDriver:initialize\n");
  46. $this->depot = $this->repoConfig['depot'];
  47. $this->p4client = "composer_perforce_$this->depot";
  48. $this->repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot";
  49. $clientSpec = $this->config->get('cache-dir') . "/perforce/$this->p4client.p4.spec";
  50. $this->p4Login();
  51. $fs = new Filesystem();
  52. $fs->ensureDirectoryExists($this->repoDir);
  53. $stream = "//$this->depot/$this->rootIdentifier";
  54. $perforce = new Perforce();
  55. $perforce->writeP4ClientSpec($clientSpec, $this->repoDir, $this->p4client, $stream);
  56. $perforce->syncCodeBase($clientSpec, $this->repoDir, $this->p4client);
  57. return true;
  58. }
  59. protected function p4Login(){
  60. $password = trim(shell_exec('echo $P4PASSWD'));
  61. $command = "echo $password | p4 login -a ";
  62. shell_exec($command);
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getComposerInformation($identifier)
  68. {
  69. print ("PerforceDriver:getComposerInformation: $identifier\n");
  70. $command = "p4 print $identifier/composer.json";
  71. $result = shell_exec($command);
  72. $index = strpos($result, "{");
  73. if ($index === false){
  74. return;
  75. }
  76. if ($index >=0){
  77. $rawData = substr($result, $index);
  78. $composer_info = json_decode($rawData, true);
  79. print ("ComposerInfo is:".var_export($composer_info, true) . "\n");
  80. return $composer_info;
  81. }
  82. // Basically, read the composer.json file from the project.
  83. //
  84. // Git stuff:
  85. // ..getComposerInfo is: array (
  86. // 'support' =>
  87. // array (
  88. // 'source' => 'http://github.com/composer/packagist',
  89. // ),
  90. // 'time' => '2012-09-10',
  91. // )
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function getRootIdentifier()
  97. {
  98. print ("PerforceDriver:getRootIdentifier\n");
  99. return $this->rootIdentifier;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function getBranches()
  105. {
  106. //return $branch->$identifier
  107. //getComposer($identifier)
  108. //validate($branch)
  109. print ("PerforceDriver:getBranches\n");
  110. $command = "p4 streams //$this->depot/...";
  111. $result = shell_exec($command);
  112. $resArray = explode("\n", $result);
  113. $branches = array();
  114. foreach ($resArray as $line){
  115. $resBits = explode(" ", $line);
  116. if (count($resBits) > 4){
  117. $branch = substr($resBits[4], 1, strlen($resBits[4])-2);
  118. $branches[$branch] = $resBits[1];
  119. }
  120. }
  121. $branches['master'] = $branches['mainline'];
  122. print ("PerforceDriver:getBranches - returning branches:".var_export($branches, true)."\n");
  123. return $branches;
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public function getTags()
  129. {
  130. print ("PerforceDriver:getTags\n");
  131. return array();
  132. }
  133. /**
  134. * {@inheritDoc}
  135. */
  136. public function getDist($identifier)
  137. {
  138. print ("PerforceDriver:getDist: $identifier\n");
  139. return null;
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public function getSource($identifier)
  145. {
  146. print ("PerforceDriver:getSource: $identifier\n");
  147. $source = array (
  148. 'type' => 'perforce',
  149. 'url' => $this->repoConfig['url'],
  150. 'reference' => $identifier
  151. );
  152. return $source;
  153. }
  154. /**
  155. * {@inheritDoc}
  156. */
  157. public function getUrl()
  158. {
  159. print ("PerforceDriver:getUrl\n");
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public function hasComposerFile($identifier)
  165. {
  166. print ("PerforceDriver:hasComposerFile: $identifier\n");
  167. //Does the project have a composer file?
  168. return true;
  169. }
  170. /**
  171. * {@inheritDoc}
  172. */
  173. public function getContents($url)
  174. {
  175. print("PerforceDriver:getContents - url: $url");
  176. }
  177. /**
  178. * {@inheritDoc}
  179. */
  180. public static function supports(IOInterface $io, $url, $deep = false)
  181. {
  182. print ("PerforceDriver:supports\n");
  183. print ("\nChecking url for support: $url\n\n");
  184. if (preg_match('#(^perforce)#', $url)) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. }