PathRepository.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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;
  12. use Composer\Config;
  13. use Composer\IO\IOInterface;
  14. use Composer\Json\JsonFile;
  15. use Composer\Package\Loader\ArrayLoader;
  16. use Composer\Package\Version\VersionGuesser;
  17. use Composer\Package\Version\VersionParser;
  18. use Composer\Util\Platform;
  19. use Composer\Util\ProcessExecutor;
  20. /**
  21. * This repository allows installing local packages that are not necessarily under their own VCS.
  22. *
  23. * The local packages will be symlinked when possible, else they will be copied.
  24. *
  25. * @code
  26. * "require": {
  27. * "<vendor>/<local-package>": "*"
  28. * },
  29. * "repositories": [
  30. * {
  31. * "type": "path",
  32. * "url": "../../relative/path/to/package/"
  33. * },
  34. * {
  35. * "type": "path",
  36. * "url": "/absolute/path/to/package/"
  37. * },
  38. * {
  39. * "type": "path",
  40. * "url": "/absolute/path/to/several/packages/*"
  41. * },
  42. * {
  43. * "type": "path",
  44. * "url": "../../relative/path/to/package/",
  45. * "options": {
  46. * "symlink": false
  47. * }
  48. * },
  49. * ]
  50. * @endcode
  51. *
  52. * @author Samuel Roze <samuel.roze@gmail.com>
  53. * @author Johann Reinke <johann.reinke@gmail.com>
  54. */
  55. class PathRepository extends ArrayRepository implements ConfigurableRepositoryInterface
  56. {
  57. /**
  58. * @var ArrayLoader
  59. */
  60. private $loader;
  61. /**
  62. * @var VersionGuesser
  63. */
  64. private $versionGuesser;
  65. /**
  66. * @var string
  67. */
  68. private $url;
  69. /**
  70. * @var array
  71. */
  72. private $repoConfig;
  73. /**
  74. * @var ProcessExecutor
  75. */
  76. private $process;
  77. /**
  78. * @var array
  79. */
  80. private $options;
  81. /**
  82. * Initializes path repository.
  83. *
  84. * @param array $repoConfig
  85. * @param IOInterface $io
  86. * @param Config $config
  87. */
  88. public function __construct(array $repoConfig, IOInterface $io, Config $config)
  89. {
  90. if (!isset($repoConfig['url'])) {
  91. throw new \RuntimeException('You must specify the `url` configuration for the path repository');
  92. }
  93. $this->loader = new ArrayLoader(null, true);
  94. $this->url = Platform::expandPath($repoConfig['url']);
  95. $this->process = new ProcessExecutor($io);
  96. $this->versionGuesser = new VersionGuesser($config, $this->process, new VersionParser());
  97. $this->repoConfig = $repoConfig;
  98. $this->options = isset($repoConfig['options']) ? $repoConfig['options'] : array();
  99. parent::__construct();
  100. }
  101. public function getRepoConfig()
  102. {
  103. return $this->repoConfig;
  104. }
  105. /**
  106. * Initializes path repository.
  107. *
  108. * This method will basically read the folder and add the found package.
  109. */
  110. protected function initialize()
  111. {
  112. parent::initialize();
  113. $urlMatches = $this->getUrlMatches();
  114. if (empty($urlMatches)) {
  115. throw new \RuntimeException('The `url` supplied for the path (' . $this->url . ') repository does not exist');
  116. }
  117. foreach ($urlMatches as $url) {
  118. $path = realpath($url) . DIRECTORY_SEPARATOR;
  119. $composerFilePath = $path.'composer.json';
  120. if (!file_exists($composerFilePath)) {
  121. continue;
  122. }
  123. $json = file_get_contents($composerFilePath);
  124. $package = JsonFile::parseJson($json, $composerFilePath);
  125. $package['dist'] = array(
  126. 'type' => 'path',
  127. 'url' => $url,
  128. 'reference' => sha1($json . serialize($this->options)),
  129. );
  130. $package['transport-options'] = $this->options;
  131. // carry over the root package version if this path repo is in the same git repository as root package
  132. if (!isset($package['version']) && ($rootVersion = getenv('COMPOSER_ROOT_VERSION'))) {
  133. if (
  134. 0 === $this->process->execute('git rev-parse HEAD', $ref1, $path)
  135. && 0 === $this->process->execute('git rev-parse HEAD', $ref2)
  136. && $ref1 === $ref2
  137. ) {
  138. $package['version'] = $rootVersion;
  139. }
  140. }
  141. if (!isset($package['version'])) {
  142. $versionData = $this->versionGuesser->guessVersion($package, $path);
  143. if (is_array($versionData) && $versionData['pretty_version']) {
  144. $package['version'] = $versionData['pretty_version'];
  145. } else {
  146. $package['version'] = 'dev-master';
  147. }
  148. }
  149. $output = '';
  150. if (is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process->execute('git log -n1 --pretty=%H', $output, $path)) {
  151. $package['dist']['reference'] = trim($output);
  152. }
  153. $package = $this->loader->load($package);
  154. $this->addPackage($package);
  155. }
  156. }
  157. /**
  158. * Get a list of all (possibly relative) path names matching given url (supports globbing).
  159. *
  160. * @return string[]
  161. */
  162. private function getUrlMatches()
  163. {
  164. $flags = GLOB_MARK | GLOB_ONLYDIR;
  165. if (defined('GLOB_BRACE')) {
  166. $flags |= GLOB_BRACE;
  167. } elseif (strpos($this->url, '{') !== false || strpos($this->url, '}') !== false) {
  168. throw new \RuntimeException('The operating system does not support GLOB_BRACE which is required for the url '. $this->url);
  169. }
  170. // Ensure environment-specific path separators are normalized to URL separators
  171. return array_map(function ($val) {
  172. return rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $val), '/');
  173. }, glob($this->url, $flags));
  174. }
  175. }