Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Composer.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use Closure;
6 use Illuminate\Filesystem\Filesystem;
7 use RuntimeException;
8 use Symfony\Component\Console\Output\OutputInterface;
9 use Symfony\Component\Process\Process;
10
11 class Composer
12 {
13 /**
14 * The filesystem instance.
15 *
16 * @var \Illuminate\Filesystem\Filesystem
17 */
18 protected $files;
19
20 /**
21 * The working path to regenerate from.
22 *
23 * @var string|null
24 */
25 protected $workingPath;
26
27 /**
28 * Create a new Composer manager instance.
29 *
30 * @param \Illuminate\Filesystem\Filesystem $files
31 * @param string|null $workingPath
32 */
33 public function __construct(Filesystem $files, $workingPath = null)
34 {
35 $this->files = $files;
36 $this->workingPath = $workingPath;
37 }
38
39 /**
40 * Determine if the given Composer package is installed.
41 *
42 * @param string $package
43 * @return bool
44 *
45 * @throws \RuntimeException
46 */
47 public function hasPackage($package)
48 {
49 $composer = json_decode(file_get_contents($this->findComposerFile()), true);
50
51 return array_key_exists($package, $composer['require'] ?? [])
52 || array_key_exists($package, $composer['require-dev'] ?? []);
53 }
54
55 /**
56 * Install the given Composer packages into the application.
57 *
58 * @param array<int, string> $packages
59 * @param bool $dev
60 * @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
61 * @param string|null $composerBinary
62 * @return bool
63 */
64 public function requirePackages(array $packages, bool $dev = false, Closure|OutputInterface|null $output = null, $composerBinary = null)
65 {
66 $command = (new Collection([
67 ...$this->findComposer($composerBinary),
68 'require',
69 ...$packages,
70 ]))
71 ->when($dev, function ($command) {
72 $command->push('--dev');
73 })->all();
74
75 return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1'])
76 ->run(
77 $output instanceof OutputInterface
78 ? function ($type, $line) use ($output) {
79 $output->write(' '.$line);
80 } : $output
81 );
82 }
83
84 /**
85 * Remove the given Composer packages from the application.
86 *
87 * @param array<int, string> $packages
88 * @param bool $dev
89 * @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
90 * @param string|null $composerBinary
91 * @return bool
92 */
93 public function removePackages(array $packages, bool $dev = false, Closure|OutputInterface|null $output = null, $composerBinary = null)
94 {
95 $command = (new Collection([
96 ...$this->findComposer($composerBinary),
97 'remove',
98 ...$packages,
99 ]))
100 ->when($dev, function ($command) {
101 $command->push('--dev');
102 })->all();
103
104 return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1'])
105 ->run(
106 $output instanceof OutputInterface
107 ? function ($type, $line) use ($output) {
108 $output->write(' '.$line);
109 } : $output
110 );
111 }
112
113 /**
114 * Modify the "composer.json" file contents using the given callback.
115 *
116 * @param callable(array):array $callback
117 * @return void
118 *
119 * @throws \RuntimeException
120 */
121 public function modify(callable $callback)
122 {
123 $composerFile = $this->findComposerFile();
124
125 $composer = json_decode(file_get_contents($composerFile), true, 512, JSON_THROW_ON_ERROR);
126
127 file_put_contents(
128 $composerFile,
129 json_encode(
130 call_user_func($callback, $composer),
131 JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
132 )
133 );
134 }
135
136 /**
137 * Regenerate the Composer autoloader files.
138 *
139 * @param string|array $extra
140 * @param string|null $composerBinary
141 * @return int
142 */
143 public function dumpAutoloads($extra = '', $composerBinary = null)
144 {
145 $extra = $extra ? (array) $extra : [];
146
147 $command = array_merge($this->findComposer($composerBinary), ['dump-autoload'], $extra);
148
149 return $this->getProcess($command)->run();
150 }
151
152 /**
153 * Regenerate the optimized Composer autoloader files.
154 *
155 * @param string|null $composerBinary
156 * @return int
157 */
158 public function dumpOptimized($composerBinary = null)
159 {
160 return $this->dumpAutoloads('--optimize', $composerBinary);
161 }
162
163 /**
164 * Get the Composer binary / command for the environment.
165 *
166 * @param string|null $composerBinary
167 * @return array
168 */
169 public function findComposer($composerBinary = null)
170 {
171 if (! is_null($composerBinary) && $this->files->exists($composerBinary)) {
172 return [$this->phpBinary(), $composerBinary];
173 } elseif ($this->files->exists($this->workingPath.'/composer.phar')) {
174 return [$this->phpBinary(), 'composer.phar'];
175 }
176
177 return ['composer'];
178 }
179
180 /**
181 * Get the path to the "composer.json" file.
182 *
183 * @return string
184 *
185 * @throws \RuntimeException
186 */
187 protected function findComposerFile()
188 {
189 $composerFile = "{$this->workingPath}/composer.json";
190
191 if (! file_exists($composerFile)) {
192 throw new RuntimeException("Unable to locate `composer.json` file at [{$this->workingPath}].");
193 }
194
195 return $composerFile;
196 }
197
198 /**
199 * Get the PHP binary.
200 *
201 * @return string
202 */
203 protected function phpBinary()
204 {
205 return php_binary();
206 }
207
208 /**
209 * Get a new Symfony process instance.
210 *
211 * @param array $command
212 * @param array $env
213 * @return \Symfony\Component\Process\Process
214 */
215 protected function getProcess(array $command, array $env = [])
216 {
217 return (new Process($command, $this->workingPath, $env))->setTimeout(null);
218 }
219
220 /**
221 * Set the working path used by the class.
222 *
223 * @param string $path
224 * @return $this
225 */
226 public function setWorkingPath($path)
227 {
228 $this->workingPath = realpath($path);
229
230 return $this;
231 }
232
233 /**
234 * Get the version of Composer.
235 *
236 * @return string|null
237 */
238 public function getVersion()
239 {
240 $command = array_merge($this->findComposer(), ['-V', '--no-ansi']);
241
242 $process = $this->getProcess($command);
243
244 $process->run();
245
246 $output = $process->getOutput();
247
248 if (preg_match('/(\d+(\.\d+){2})/', $output, $version)) {
249 return $version[1];
250 }
251
252 return explode(' ', $output)[2] ?? null;
253 }
254 }