You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.8KB

  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Fxp\Composer\AssetPlugin\Util;
  11. use Composer\IO\IOInterface;
  12. use Composer\Util\Perforce as BasePerforce;
  13. use Composer\Util\ProcessExecutor;
  14. /**
  15. * Helper for perforce driver.
  16. *
  17. * @author François Pluchino <francois.pluchino@gmail.com>
  18. */
  19. class Perforce extends BasePerforce
  20. {
  21. /**
  22. * @var string
  23. */
  24. protected $filename;
  25. /**
  26. * @param array $repoConfig
  27. */
  28. public function initialize($repoConfig)
  29. {
  30. parent::initialize($repoConfig);
  31. $this->filename = (string) $repoConfig['filename'];
  32. }
  33. /**
  34. * @param string $identifier
  35. *
  36. * @return array|string
  37. */
  38. public function getComposerInformation($identifier)
  39. {
  40. $index = strpos($identifier, '@');
  41. if ($index === false) {
  42. $composerJson = $identifier.'/'.$this->filename;
  43. return $this->getComposerInformationFromPath($composerJson);
  44. }
  45. return $this->getComposerInformationFromLabel($identifier, $index);
  46. }
  47. /**
  48. * @param string $identifier
  49. * @param string $index
  50. *
  51. * @return array|string
  52. */
  53. public function getComposerInformationFromLabel($identifier, $index)
  54. {
  55. $composerJsonPath = substr($identifier, 0, $index).'/'.$this->filename.substr($identifier, $index);
  56. $command = $this->generateP4Command(' files '.$composerJsonPath, false);
  57. $this->executeCommand($command);
  58. $result = $this->commandResult;
  59. $index2 = strpos($result, 'no such file(s).');
  60. if ($index2 === false) {
  61. $index3 = strpos($result, 'change');
  62. if (!($index3 === false)) {
  63. $phrase = trim(substr($result, $index3));
  64. $fields = explode(' ', $phrase);
  65. $id = $fields[1];
  66. $composerJson = substr($identifier, 0, $index).'/'.$this->filename.'@'.$id;
  67. return $this->getComposerInformationFromPath($composerJson);
  68. }
  69. }
  70. return '';
  71. }
  72. /**
  73. * Create perforce helper.
  74. *
  75. * @param array $repoConfig
  76. * @param int|string $port
  77. * @param string $path
  78. * @param ProcessExecutor $process
  79. * @param IOInterface $io
  80. *
  81. * @return Perforce
  82. */
  83. public static function create($repoConfig, $port, $path, ProcessExecutor $process, IOInterface $io)
  84. {
  85. $isWindows = defined('PHP_WINDOWS_VERSION_BUILD');
  86. $perforce = new self($repoConfig, $port, $path, $process, $isWindows, $io);
  87. return $perforce;
  88. }
  89. }