/
PathRepository.php
240 lines (209 loc) · 7.79 KB
/
PathRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Repository;
use Composer\Config;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Package\CompleteAliasPackage;
use Composer\Package\CompletePackage;
use Composer\Package\Loader\ArrayLoader;
use Composer\Package\Version\VersionGuesser;
use Composer\Package\Version\VersionParser;
use Composer\Pcre\Preg;
use Composer\Util\Platform;
use Composer\Util\ProcessExecutor;
use Composer\Util\Filesystem;
use Composer\Util\Url;
use Composer\Util\Git as GitUtil;
/**
* This repository allows installing local packages that are not necessarily under their own VCS.
*
* The local packages will be symlinked when possible, else they will be copied.
*
* @code
* "require": {
* "<vendor>/<local-package>": "*"
* },
* "repositories": [
* {
* "type": "path",
* "url": "../../relative/path/to/package/"
* },
* {
* "type": "path",
* "url": "/absolute/path/to/package/"
* },
* {
* "type": "path",
* "url": "/absolute/path/to/several/packages/*"
* },
* {
* "type": "path",
* "url": "../../relative/path/to/package/",
* "options": {
* "symlink": false
* }
* },
* ]
* @endcode
*
* @author Samuel Roze <samuel.roze@gmail.com>
* @author Johann Reinke <johann.reinke@gmail.com>
*/
class PathRepository extends ArrayRepository implements ConfigurableRepositoryInterface
{
/**
* @var ArrayLoader
*/
private $loader;
/**
* @var VersionGuesser
*/
private $versionGuesser;
/**
* @var string
*/
private $url;
/**
* @var mixed[]
* @phpstan-var array{url: string, options?: array{symlink?: bool, relative?: bool, versions?: array<string, string>}}
*/
private $repoConfig;
/**
* @var ProcessExecutor
*/
private $process;
/**
* @var array{symlink?: bool, relative?: bool, versions?: array<string, string>}
*/
private $options;
/**
* Initializes path repository.
*
* @param array{url?: string, options?: array{symlink?: bool, relative?: bool, versions?: array<string, string>}} $repoConfig
* @param IOInterface $io
* @param Config $config
*/
public function __construct(array $repoConfig, IOInterface $io, Config $config)
{
if (!isset($repoConfig['url'])) {
throw new \RuntimeException('You must specify the `url` configuration for the path repository');
}
$this->loader = new ArrayLoader(null, true);
$this->url = Platform::expandPath($repoConfig['url']);
$this->process = new ProcessExecutor($io);
$this->versionGuesser = new VersionGuesser($config, $this->process, new VersionParser());
$this->repoConfig = $repoConfig;
$this->options = isset($repoConfig['options']) ? $repoConfig['options'] : array();
if (!isset($this->options['relative'])) {
$filesystem = new Filesystem();
$this->options['relative'] = !$filesystem->isAbsolutePath($this->url);
}
parent::__construct();
}
public function getRepoName()
{
return 'path repo ('.Url::sanitize($this->repoConfig['url']).')';
}
public function getRepoConfig()
{
return $this->repoConfig;
}
/**
* Initializes path repository.
*
* This method will basically read the folder and add the found package.
*/
protected function initialize()
{
parent::initialize();
$urlMatches = $this->getUrlMatches();
if (empty($urlMatches)) {
if (Preg::isMatch('{[*{}]}', $this->url)) {
$url = $this->url;
while (Preg::isMatch('{[*{}]}', $url)) {
$url = dirname($url);
}
// the parent directory before any wildcard exists, so we assume it is correctly configured but simply empty
if (is_dir($url)) {
return;
}
}
throw new \RuntimeException('The `url` supplied for the path (' . $this->url . ') repository does not exist');
}
foreach ($urlMatches as $url) {
$path = realpath($url) . DIRECTORY_SEPARATOR;
$composerFilePath = $path.'composer.json';
if (!file_exists($composerFilePath)) {
continue;
}
$json = file_get_contents($composerFilePath);
$package = JsonFile::parseJson($json, $composerFilePath);
$package['dist'] = array(
'type' => 'path',
'url' => $url,
'reference' => sha1($json . serialize($this->options)),
);
// copy symlink/relative options to transport options
$package['transport-options'] = array_intersect_key($this->options, array('symlink' => true, 'relative' => true));
// use the version provided as option if available
if (isset($package['name'], $this->options['versions'][$package['name']])) {
$package['version'] = $this->options['versions'][$package['name']];
}
// carry over the root package version if this path repo is in the same git repository as root package
if (!isset($package['version']) && ($rootVersion = Platform::getEnv('COMPOSER_ROOT_VERSION'))) {
if (
0 === $this->process->execute('git rev-parse HEAD', $ref1, $path)
&& 0 === $this->process->execute('git rev-parse HEAD', $ref2)
&& $ref1 === $ref2
) {
$package['version'] = $rootVersion;
}
}
$output = '';
if (is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process->execute('git log -n1 --pretty=%H'.GitUtil::getNoShowSignatureFlag($this->process), $output, $path)) {
$package['dist']['reference'] = trim($output);
}
if (!isset($package['version'])) {
$versionData = $this->versionGuesser->guessVersion($package, $path);
if (is_array($versionData) && $versionData['pretty_version']) {
// if there is a feature branch detected, we add a second packages with the feature branch version
if (!empty($versionData['feature_pretty_version'])) {
$package['version'] = $versionData['feature_pretty_version'];
$this->addPackage($this->loader->load($package));
}
$package['version'] = $versionData['pretty_version'];
} else {
$package['version'] = 'dev-main';
}
}
$this->addPackage($this->loader->load($package));
}
}
/**
* Get a list of all (possibly relative) path names matching given url (supports globbing).
*
* @return string[]
*/
private function getUrlMatches()
{
$flags = GLOB_MARK | GLOB_ONLYDIR;
if (defined('GLOB_BRACE')) {
$flags |= GLOB_BRACE;
} elseif (strpos($this->url, '{') !== false || strpos($this->url, '}') !== false) {
throw new \RuntimeException('The operating system does not support GLOB_BRACE which is required for the url '. $this->url);
}
// Ensure environment-specific path separators are normalized to URL separators
return array_map(function ($val) {
return rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $val), '/');
}, glob($this->url, $flags));
}
}