Repository navigation
Expand file tree
/
Copy pathComposer.php
More file actions
173 lines (149 loc) · 5.56 KB
/
Copy pathComposer.php
File metadata and controls
173 lines (149 loc) · 5.56 KB
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
<?php
declare(strict_types=1);
/*
* This file is part of the Composer package "cpsit/project-builder".
*
* Copyright (C) 2022 Elias Häußler <e.haeussler@familie-redlich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace CPSIT\ProjectBuilder\Resource\Local;
use Composer\Autoload;
use Composer\Factory;
use Composer\InstalledVersions;
use Composer\IO;
use Composer\Package;
use CPSIT\ProjectBuilder\Exception;
use CPSIT\ProjectBuilder\Helper;
use CPSIT\ProjectBuilder\Template;
use Symfony\Component\Console as SymfonyConsole;
use Symfony\Component\Filesystem;
use Symfony\Component\Process;
use function array_filter;
use function basename;
use function dirname;
use function in_array;
use function is_array;
use function is_executable;
use function is_file;
use function is_string;
/**
* Composer.
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-3.0-or-later
*/
final readonly class Composer
{
public function __construct(
private Filesystem\Filesystem $filesystem,
private Process\ExecutableFinder $executableFinder = new Process\ExecutableFinder(),
) {}
/**
* @template T of SymfonyConsole\Output\OutputInterface|null
*
* @param T $output
*
* @param-out (T is null ? SymfonyConsole\Output\BufferedOutput : T) $output
*/
public function install(
string $composerJson,
bool $includeDevDependencies = false,
?SymfonyConsole\Output\OutputInterface &$output = null,
): int {
if (!$this->filesystem->exists($composerJson)) {
throw Exception\IOException::forMissingFile($composerJson);
}
$output ??= new SymfonyConsole\Output\BufferedOutput();
$command = [
...$this->resolveComposerBinary(),
'update',
'--prefer-dist',
'--no-interaction',
];
if (!$includeDevDependencies) {
$command[] = '--no-dev';
}
// Run Composer in a dedicated process. Running it in-process would
// load the template's dependencies (autoload "files", plugins, ...)
// into this very process, which can clash with dependencies already
// loaded for the project builder itself (e.g. duplicate global
// function declarations of a shared dependency).
$process = new Process\Process(
$command,
dirname($composerJson),
['COMPOSER' => basename($composerJson)],
null,
null,
);
$process->disableOutput();
$process->run(static function (string $type, string $line) use ($output): void {
$output->write($line);
});
return (int) $process->getExitCode();
}
/**
* @return non-empty-list<string>
*/
private function resolveComposerBinary(): array
{
$binary = $this->executableFinder->find('composer');
if (null !== $binary) {
return [$binary];
}
// Fall back to the binary/script that runs the current process,
// e.g. when Composer was invoked as a local "composer.phar" that
// is not registered on the system's PATH.
$argv = $_SERVER['argv'] ?? null;
$fallback = is_array($argv) ? $argv[0] ?? null : null;
if (is_string($fallback) && is_file($fallback)) {
return is_executable($fallback) ? [$fallback] : [PHP_BINARY, $fallback];
}
return ['composer'];
}
/**
* @internal
*/
public static function createClassLoader(?string $rootPath = null): Autoload\ClassLoader
{
$rootPath ??= Helper\FilesystemHelper::getPackageDirectory();
$composer = self::createComposer($rootPath);
// Get all packages of type "project-builder-template"
$repository = $composer->getRepositoryManager()->getLocalRepository();
$templatePackages = InstalledVersions::getInstalledPackagesByType(Template\Provider\ProviderInterface::PACKAGE_TYPE);
$packages = array_filter(
$repository->getPackages(),
fn (Package\BasePackage $package): bool => in_array($package->getName(), $templatePackages, true),
);
$packages[] = $composer->getPackage();
// Parse autoloads of template packages
$autoloadGenerator = $composer->getAutoloadGenerator();
$packageMap = $autoloadGenerator->buildPackageMap($composer->getInstallationManager(), $composer->getPackage(), $packages);
$autoloads = $autoloadGenerator->parseAutoloads($packageMap, $composer->getPackage());
// Fetch vendor directory
$vendorDir = $composer->getConfig()->get('vendor-dir');
return $autoloadGenerator->createLoader($autoloads, $vendorDir);
}
/**
* @internal
*/
public static function createComposer(string $rootPath): \Composer\Composer
{
$factory = new Factory();
return $factory->createComposer(
new IO\NullIO(),
Filesystem\Path::join($rootPath, 'composer.json'),
);
}
}