]> BookStack Code Mirror - system-cli/blob - scripts/Services/ComposerLocator.php
Added update command
[system-cli] / scripts / Services / ComposerLocator.php
1 <?php
2
3 namespace Cli\Services;
4
5 use Exception;
6
7 class ComposerLocator
8 {
9     public function __construct(
10         protected string $appDir
11     ) {
12     }
13
14     public function getProgram(): ProgramRunner
15     {
16         return (new ProgramRunner('composer', '/usr/local/bin/composer'))
17             ->withTimeout(300)
18             ->withIdleTimeout(15)
19             ->withAdditionalPathLocation($this->appDir);
20     }
21
22     /**
23      * @throws Exception
24      */
25     public function download(): void
26     {
27         $setupPath = $this->appDir . DIRECTORY_SEPARATOR . 'composer-setup.php';
28         $signature = file_get_contents('https://composer.github.io/installer.sig');
29         copy('https://getcomposer.org/installer', $setupPath);
30         $checksum = hash_file('sha384', $setupPath);
31
32         if ($signature !== $checksum) {
33             unlink($setupPath);
34             throw new Exception("Could not install composer, checksum validation failed.");
35         }
36
37         $status = (new ProgramRunner('php', '/usr/bin/php'))
38             ->runWithoutOutputCallbacks([
39                 $setupPath, '--quiet',
40                 "--install-dir={$this->appDir}",
41                 "--filename=composer",
42             ]);
43
44         unlink($setupPath);
45
46         if ($status !== 0) {
47             throw new Exception("Could not install composer, composer-setup script run failed.");
48         }
49     }
50 }