-
Notifications
You must be signed in to change notification settings - Fork 16
/
Composer.php
139 lines (114 loc) · 4.3 KB
/
Composer.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
<?php
namespace Castor\Import\Remote;
use Castor\Console\Application;
use Castor\Fingerprint\FingerprintHelper;
use Castor\GlobalHelper;
use Castor\Import\Exception\ComposerError;
use Castor\PathHelper;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Helper\ProgressIndicator;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
/** @internal */
class Composer
{
public const VENDOR_DIR = '/.castor/vendor/';
public const DEFAULT_COMPOSER_CONFIGURATION = [
'description' => 'This file is managed by Castor. Do not edit it manually.',
'config' => [
'sort-packages' => true,
],
'replace' => [
'castor/castor' => Application::VERSION,
],
];
public function __construct(
private readonly Filesystem $filesystem,
private readonly LoggerInterface $logger,
private readonly FingerprintHelper $fingerprintHelper,
/** @var array<string, mixed> */
private array $configuration = self::DEFAULT_COMPOSER_CONFIGURATION,
) {
}
/**
* @return array<string, mixed>
*/
public function getConfiguration(): array
{
return $this->configuration;
}
/**
* @param array<string, mixed> $configuration
*/
public function setConfiguration(array $configuration): void
{
$this->configuration = $configuration;
}
public function update(bool $force = false, bool $displayProgress = true): void
{
$composer = (new ExecutableFinder())->find('composer');
if (!$composer) {
throw new ComposerError('The "composer" executable was not found. In order to use remote import, please make sure that Composer is installed and available in your PATH.');
}
$dir = PathHelper::getRoot() . self::VENDOR_DIR;
$this->filesystem->mkdir($dir);
file_put_contents($dir . '.gitignore', "*\n");
$this->writeJsonFile($dir . 'composer.json', $this->configuration);
$ran = false;
$fingerprint = base64_encode(json_encode($this->configuration, \JSON_THROW_ON_ERROR));
if ($force || !$this->fingerprintHelper->verifyFingerprintFromHash($fingerprint)) {
$progressIndicator = null;
if ($displayProgress) {
$progressIndicator = new ProgressIndicator(GlobalHelper::getOutput(), null, 100, ['⠏', '⠛', '⠹', '⢸', '⣰', '⣤', '⣆', '⡇']);
$progressIndicator->start('<comment>Downloading remote packages</comment>');
}
$this->run(['update'], callback: function () use ($progressIndicator) {
if ($progressIndicator) {
$progressIndicator->advance();
}
});
if ($progressIndicator) {
$progressIndicator->finish('<info>Remote packages imported</info>');
}
$this->fingerprintHelper->postProcessFingerprintForHash($fingerprint);
$ran = true;
}
if (!$ran) {
$this->logger->debug('Packages were already required, no need to run Composer.');
}
}
public function remove(): void
{
$this->filesystem->remove(PathHelper::getRoot() . self::VENDOR_DIR);
}
/**
* @param string[] $args
*/
private function run(array $args, callable $callback): void
{
$this->logger->debug('Running Composer command.', [
'args' => implode(' ', $args),
]);
$dir = PathHelper::getRoot() . self::VENDOR_DIR;
$process = new Process(['composer', ...$args, '--working-dir', $dir]);
$process->setEnv([
'COMPOSER_VENDOR_DIR' => $dir,
]);
$process->run($callback);
if (!$process->isSuccessful()) {
throw new ComposerError('The Composer process failed: ' . $process->getErrorOutput());
}
$this->logger->debug('Composer command was successful.', [
'args' => implode(' ', $args),
'output' => $process->getOutput(),
]);
}
/**
* @param array<string, mixed> $json
*/
private function writeJsonFile(string $path, array $json): void
{
file_put_contents($path, json_encode($json, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR));
}
}