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.

112 lines
4.1KB

  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\Repository\Vcs;
  11. use Composer\Cache;
  12. use Composer\Json\JsonFile;
  13. use Composer\Repository\Vcs\VcsDriverInterface;
  14. /**
  15. * Helper for bitbucket VCS driver.
  16. *
  17. * @author François Pluchino <francois.pluchino@gmail.com>
  18. */
  19. class BitbucketUtil
  20. {
  21. /**
  22. * Get composer information.
  23. *
  24. * @param Cache $cache The cache
  25. * @param array $infoCache The code cache
  26. * @param string $scheme The scheme
  27. * @param array $repoConfig The repository config
  28. * @param string $identifier The identifier
  29. * @param string $owner The owner of repository
  30. * @param string $repository The repository name
  31. * @param VcsDriverInterface $driver The vcs driver
  32. * @param string $method The method of vcs driver for get contents
  33. *
  34. * @return array The composer
  35. */
  36. public static function getComposerInformation(Cache $cache, array &$infoCache, $scheme,
  37. array $repoConfig, $identifier, $owner, $repository, VcsDriverInterface $driver, $method = 'getContents')
  38. {
  39. $infoCache[$identifier] = Util::readCache($infoCache, $cache, $repoConfig['asset-type'], $identifier);
  40. if (!isset($infoCache[$identifier])) {
  41. $resource = $scheme.'://bitbucket.org/'.$owner.'/'.$repository.'/raw/'.$identifier.'/'.$repoConfig['filename'];
  42. $composer = static::getComposerContent($resource, $identifier, $scheme, $owner, $repository, $driver, $method);
  43. Util::writeCache($cache, $repoConfig['asset-type'], $identifier, $composer);
  44. $infoCache[$identifier] = $composer;
  45. }
  46. return $infoCache[$identifier];
  47. }
  48. /**
  49. * Gets content of composer information.
  50. *
  51. * @param string $resource The resource
  52. * @param string $identifier The identifier
  53. * @param string $scheme The scheme
  54. * @param string $owner The owner
  55. * @param string $repository The repository
  56. * @param VcsDriverInterface $driver The vcs driver
  57. * @param string $method The method for get content
  58. *
  59. * @return array
  60. */
  61. protected static function getComposerContent($resource, $identifier, $scheme, $owner, $repository, $driver, $method)
  62. {
  63. try {
  64. $ref = new \ReflectionClass($driver);
  65. $meth = $ref->getMethod($method);
  66. $meth->setAccessible(true);
  67. $composer = $meth->invoke($driver, $resource);
  68. } catch (\Exception $e) {
  69. $composer = false;
  70. }
  71. if ($composer) {
  72. $composer = (array) JsonFile::parseJson((string) $composer, $resource);
  73. $composer = static::formatComposerContent($composer, $identifier, $scheme, $owner, $repository, $driver, $method);
  74. return $composer;
  75. }
  76. return array('_nonexistent_package' => true);
  77. }
  78. /**
  79. * Format composer content.
  80. *
  81. * @param array $composer The composer
  82. * @param string $identifier The identifier
  83. * @param string $scheme The scheme
  84. * @param string $owner The owner
  85. * @param string $repository The repository
  86. * @param VcsDriverInterface $driver The vcs driver
  87. * @param string $method The method for get content
  88. *
  89. * @return array
  90. */
  91. protected static function formatComposerContent(array $composer, $identifier, $scheme, $owner, $repository, $driver, $method)
  92. {
  93. $resource = $scheme.'://api.bitbucket.org/1.0/repositories/'.$owner.'/'.$repository.'/changesets/'.$identifier;
  94. $composer = Util::addComposerTime($composer, 'timestamp', $resource, $driver, $method);
  95. return $composer;
  96. }
  97. }