PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/drush/composer/vendor/composer/composer/src/Composer/Repository/PathRepository.php

https://bitbucket.org/kbasarab/kbasarab_vim
PHP | 170 lines | 70 code | 20 blank | 80 comment | 5 complexity | 6a68ec99c6142e1e6338f1bd37e9d184 MD5 | raw file
Possible License(s): MIT
  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\ProcessExecutor;
  19. /**
  20. * This repository allows installing local packages that are not necessarily under their own VCS.
  21. *
  22. * The local packages will be symlinked when possible, else they will be copied.
  23. *
  24. * @code
  25. * "require": {
  26. * "<vendor>/<local-package>": "*"
  27. * },
  28. * "repositories": [
  29. * {
  30. * "type": "path",
  31. * "url": "../../relative/path/to/package/"
  32. * },
  33. * {
  34. * "type": "path",
  35. * "url": "/absolute/path/to/package/"
  36. * },
  37. * {
  38. * "type": "path",
  39. * "url": "/absolute/path/to/several/packages/*"
  40. * },
  41. * {
  42. * "type": "path",
  43. * "url": "../../relative/path/to/package/",
  44. * "options": {
  45. * "symlink": false
  46. * }
  47. * },
  48. * ]
  49. * @endcode
  50. *
  51. * @author Samuel Roze <samuel.roze@gmail.com>
  52. * @author Johann Reinke <johann.reinke@gmail.com>
  53. */
  54. class PathRepository extends ArrayRepository implements ConfigurableRepositoryInterface
  55. {
  56. /**
  57. * @var ArrayLoader
  58. */
  59. private $loader;
  60. /**
  61. * @var VersionGuesser
  62. */
  63. private $versionGuesser;
  64. /**
  65. * @var string
  66. */
  67. private $url;
  68. /**
  69. * @var array
  70. */
  71. private $repoConfig;
  72. /**
  73. * @var ProcessExecutor
  74. */
  75. private $process;
  76. /**
  77. * @var array
  78. */
  79. private $options;
  80. /**
  81. * Initializes path repository.
  82. *
  83. * @param array $repoConfig
  84. * @param IOInterface $io
  85. * @param Config $config
  86. */
  87. public function __construct(array $repoConfig, IOInterface $io, Config $config)
  88. {
  89. if (!isset($repoConfig['url'])) {
  90. throw new \RuntimeException('You must specify the `url` configuration for the path repository');
  91. }
  92. $this->loader = new ArrayLoader(null, true);
  93. $this->url = $repoConfig['url'];
  94. $this->process = new ProcessExecutor($io);
  95. $this->versionGuesser = new VersionGuesser($config, $this->process, new VersionParser());
  96. $this->repoConfig = $repoConfig;
  97. $this->options = isset($repoConfig['options']) ? $repoConfig['options'] : array();
  98. parent::__construct();
  99. }
  100. public function getRepoConfig()
  101. {
  102. return $this->repoConfig;
  103. }
  104. /**
  105. * Initializes path repository.
  106. *
  107. * This method will basically read the folder and add the found package.
  108. */
  109. protected function initialize()
  110. {
  111. parent::initialize();
  112. foreach ($this->getUrlMatches() as $url) {
  113. $path = realpath($url) . DIRECTORY_SEPARATOR;
  114. $composerFilePath = $path.'composer.json';
  115. if (!file_exists($composerFilePath)) {
  116. continue;
  117. }
  118. $json = file_get_contents($composerFilePath);
  119. $package = JsonFile::parseJson($json, $composerFilePath);
  120. $package['dist'] = array(
  121. 'type' => 'path',
  122. 'url' => $url,
  123. 'reference' => sha1($json . serialize($this->options)),
  124. );
  125. $package['transport-options'] = $this->options;
  126. if (!isset($package['version'])) {
  127. $versionData = $this->versionGuesser->guessVersion($package, $path);
  128. $package['version'] = $versionData['version'] ?: 'dev-master';
  129. }
  130. $output = '';
  131. if (is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process->execute('git log -n1 --pretty=%H', $output, $path)) {
  132. $package['dist']['reference'] = trim($output);
  133. }
  134. $package = $this->loader->load($package);
  135. $this->addPackage($package);
  136. }
  137. }
  138. /**
  139. * Get a list of all (possibly relative) path names matching given url (supports globbing).
  140. *
  141. * @return string[]
  142. */
  143. private function getUrlMatches()
  144. {
  145. // Ensure environment-specific path separators are normalized to URL separators
  146. return array_map(function ($val) {
  147. return str_replace(DIRECTORY_SEPARATOR, '/', $val);
  148. }, glob($this->url, GLOB_MARK | GLOB_ONLYDIR));
  149. }
  150. }