-
Notifications
You must be signed in to change notification settings - Fork 16
/
Importer.php
157 lines (129 loc) 路 5.02 KB
/
Importer.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
<?php
namespace Castor\Remote;
use Castor\Console\Application;
use Castor\GlobalHelper;
use Castor\PathHelper;
use Castor\Remote\Exception\ComposerError;
use Castor\Remote\Exception\ImportError;
use Castor\Remote\Exception\InvalidImportFormat;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\ExecutableFinder;
/** @internal */
class Importer
{
public function __construct(
private readonly LoggerInterface $logger,
private readonly Composer $composer,
private readonly Filesystem $filesystem,
) {
}
/**
* @param ?array{
* url?: string,
* type?: "git" | "svn",
* reference?: string,
* } $source
*/
public function importFunctionsFrom(string $scheme, string $package, ?string $version = null, ?string $vcs = null, ?array $source = null): string
{
if (!$this->allowsRemote()) {
throw new ImportError(sprintf('Remote imports are disabled, skipping import of "%s".', $package));
}
$composer = (new ExecutableFinder())->find('composer');
if (!$composer) {
throw new ImportError('The "composer" executable was not found. Please make sure that Composer is installed and available in your PATH.');
}
if (!preg_match('#^(?<organization>[^/]+)/(?<repository>[^/]+)$#', $package)) {
throw new InvalidImportFormat(sprintf('The import path must be formatted like this: "%s://<organization>/<repository>".', $scheme));
}
if ('composer' === $scheme) {
if (null !== $source) {
throw new InvalidImportFormat('The "source" argument is not supported for Composer/Packagist packages.');
}
return $this->importPackageWithComposer($package, version: $version ?? '*', repositoryUrl: $vcs);
}
if ('package' === $scheme) {
if (null !== $version || null !== $vcs) {
throw new InvalidImportFormat('The "source" and "vcs" arguments are not supported for non-Composer packages.');
}
if (null === $source) {
throw new InvalidImportFormat('The "source" argument is required for non-Composer packages.');
}
return $this->importPackageWithComposer($package, version: 'v1', source: $source);
}
throw new InvalidImportFormat(sprintf('The import scheme "%s" is not supported.', $scheme));
}
public function reset(): void
{
$this->composer->setConfiguration([
'description' => 'This file is managed by Castor. Do not edit it manually.',
'config' => [
'sort-packages' => true,
],
'replace' => [
'castor/castor' => Application::VERSION,
],
]);
}
public function cleanIfNoRemotes(): void
{
$json = $this->composer->getConfiguration();
if (\count($json['require'] ?? []) > 0) {
return;
}
$this->filesystem->remove(PathHelper::getRoot() . Composer::VENDOR_DIR);
}
/**
* @param ?array{
* url?: string,
* type?: "git" | "svn",
* reference?: string,
* } $source
*/
private function importPackageWithComposer(string $package, string $version, ?string $repositoryUrl = null, ?array $source = null): string
{
$this->logger->notice('Importing remote package with Composer.', [
'package' => $package,
'version' => $version,
]);
$json = $this->composer->getConfiguration();
$json['require'][$package] = $version;
if ($repositoryUrl) {
$json['repositories'][] = [
'type' => 'vcs',
'url' => $repositoryUrl,
];
}
if ($source) {
if (!isset($source['url'], $source['type'], $source['reference'])) {
throw new ImportError('The "source" argument must contain "url", "type" and "reference" keys.');
}
$json['repositories'][] = [
'type' => 'package',
'package' => [
'name' => $package,
'version' => $version,
'source' => $source,
],
];
}
$this->composer->setConfiguration($json);
try {
$this->composer->require($package, $version);
} catch (ComposerError $e) {
throw new ImportError(sprintf('Failed to import the package with Composer: %s.', $e->getMessage()), previous: $e);
}
return PathHelper::getRoot() . Composer::VENDOR_DIR . $package;
}
private function allowsRemote(): bool
{
if ($_SERVER['CASTOR_NO_REMOTE'] ?? false) {
return false;
}
$input = GlobalHelper::getApplication()->getInput();
// Need to look for the raw options as the input is not yet parsed
$noRemote = $input->getParameterOption('--no-remote', true);
return true === $noRemote;
}
}