forked from composer/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VcsDriver.php
186 lines (161 loc) · 5.08 KB
/
VcsDriver.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
<?php declare(strict_types=1);
/*
* 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\Vcs;
use Composer\Cache;
use Composer\Downloader\TransportException;
use Composer\Config;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Pcre\Preg;
use Composer\Util\ProcessExecutor;
use Composer\Util\HttpDownloader;
use Composer\Util\Filesystem;
use Composer\Util\Http\Response;
/**
* A driver implementation for driver with authentication interaction.
*
* @author François Pluchino <francois.pluchino@opendisplay.com>
*/
abstract class VcsDriver implements VcsDriverInterface
{
/** @var string */
protected $url;
/** @var string */
protected $originUrl;
/** @var array<string, mixed> */
protected $repoConfig;
/** @var IOInterface */
protected $io;
/** @var Config */
protected $config;
/** @var ProcessExecutor */
protected $process;
/** @var HttpDownloader */
protected $httpDownloader;
/** @var array<int|string, mixed> */
protected $infoCache = array();
/** @var ?Cache */
protected $cache;
/**
* Constructor.
*
* @param array{url: string}&array<string, mixed> $repoConfig The repository configuration
* @param IOInterface $io The IO instance
* @param Config $config The composer configuration
* @param HttpDownloader $httpDownloader Remote Filesystem, injectable for mocking
* @param ProcessExecutor $process Process instance, injectable for mocking
*/
final public function __construct(array $repoConfig, IOInterface $io, Config $config, HttpDownloader $httpDownloader, ProcessExecutor $process)
{
if (Filesystem::isLocalPath($repoConfig['url'])) {
$repoConfig['url'] = Filesystem::getPlatformPath($repoConfig['url']);
}
$this->url = $repoConfig['url'];
$this->originUrl = $repoConfig['url'];
$this->repoConfig = $repoConfig;
$this->io = $io;
$this->config = $config;
$this->httpDownloader = $httpDownloader;
$this->process = $process;
}
/**
* Returns whether or not the given $identifier should be cached or not.
*
* @param string $identifier
* @return bool
*/
protected function shouldCache(string $identifier): bool
{
return $this->cache && Preg::isMatch('{^[a-f0-9]{40}$}iD', $identifier);
}
/**
* @inheritDoc
*/
public function getComposerInformation(string $identifier): ?array
{
if (!isset($this->infoCache[$identifier])) {
if ($this->shouldCache($identifier) && $res = $this->cache->read($identifier)) {
return $this->infoCache[$identifier] = JsonFile::parseJson($res);
}
$composer = $this->getBaseComposerInformation($identifier);
if ($this->shouldCache($identifier)) {
$this->cache->write($identifier, JsonFile::encode($composer, 0));
}
$this->infoCache[$identifier] = $composer;
}
return $this->infoCache[$identifier];
}
/**
* @param string $identifier
*
* @return array<mixed>|null
*/
protected function getBaseComposerInformation(string $identifier): ?array
{
$composerFileContent = $this->getFileContent('composer.json', $identifier);
if (!$composerFileContent) {
return null;
}
$composer = JsonFile::parseJson($composerFileContent, $identifier . ':composer.json');
if ([] === $composer || !is_array($composer)) {
return null;
}
if (empty($composer['time']) && null !== ($changeDate = $this->getChangeDate($identifier))) {
$composer['time'] = $changeDate->format(DATE_RFC3339);
}
return $composer;
}
/**
* @inheritDoc
*/
public function hasComposerFile(string $identifier): bool
{
try {
return null !== $this->getComposerInformation($identifier);
} catch (TransportException $e) {
}
return false;
}
/**
* Get the https or http protocol depending on SSL support.
*
* Call this only if you know that the server supports both.
*
* @return string The correct type of protocol
*/
protected function getScheme(): string
{
if (extension_loaded('openssl')) {
return 'https';
}
return 'http';
}
/**
* Get the remote content.
*
* @param string $url The URL of content
*
* @return Response
* @throws TransportException
*/
protected function getContents(string $url): Response
{
$options = $this->repoConfig['options'] ?? array();
return $this->httpDownloader->get($url, $options);
}
/**
* @inheritDoc
*/
public function cleanup(): void
{
return;
}
}