1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-05 16:40:05 +01:00
Files
laravel-valet/cli/Valet/Composer.php
Matt Stauffer dd8e15edf5 Implement valet fetch-share-url when working with Expose (#1349)
* Flesh out Expose currentTunnelUrl method

* Apply fixes from StyleCI

* Prep for requiring a certain version of Expose

* Don't call installed() before installedVersion() in Composer

Co-authored-by: StyleCI Bot <bot@styleci.io>
2023-01-24 20:53:05 -05:00

59 lines
1.6 KiB
PHP

<?php
namespace Valet;
use DomainException;
class Composer
{
public function __construct(public CommandLine $cli)
{
}
public function installed(string $namespacedPackage): bool
{
$result = $this->cli->runAsUser("composer global show --format json -- $namespacedPackage");
if (str_contains($result, 'InvalidArgumentException') && str_contains($result, 'not found')) {
return false;
}
if (starts_with($result, 'Changed current')) {
$result = strstr($result, '{');
}
$details = json_decode($result, true);
return ! empty($details);
}
public function installOrFail(string $namespacedPackage): void
{
info('['.$namespacedPackage.'] is not installed, installing it now via Composer...</info> 🎼');
$this->cli->runAsUser(('composer global require '.$namespacedPackage), function ($exitCode, $errorOutput) use ($namespacedPackage) {
output($errorOutput);
throw new DomainException('Composer was unable to install ['.$namespacedPackage.'].');
});
}
public function installedVersion(string $namespacedPackage): ?string
{
$result = $this->cli->runAsUser("composer global show --format json -- $namespacedPackage");
if (str_contains($result, 'InvalidArgumentException') && str_contains($result, 'not found')) {
return null;
}
if (starts_with($result, 'Changed current')) {
$result = strstr($result, '{');
}
$details = json_decode($result, true);
$versions = $details['versions'];
return reset($versions);
}
}