Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add commonly reusable Composer related commands from 1st party packages #48096

Merged
merged 2 commits into from Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/Illuminate/Support/Composer.php
Expand Up @@ -2,7 +2,10 @@

namespace Illuminate\Support;

use Closure;
use Illuminate\Filesystem\Filesystem;
use RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -35,6 +38,97 @@ public function __construct(Filesystem $files, $workingPath = null)
$this->workingPath = $workingPath;
}

/**
* Install the given Composer packages into the application.
*
* @param array<int, string> $packages
* @param bool $dev
* @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
* @return bool
*/
protected function requirePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
{
$composer = $this->findComposer();

$command = explode(' ', $composer);

array_push($command, 'require');

$command = array_merge(
$command,
$packages,
$dev ? ['--dev'] : [],
);

return 0 === (new Process($command, cwd: $this->workingPath, env: ['COMPOSER_MEMORY_LIMIT' => '-1']))
->setTimeout(null)
->run(
$output instanceof OutputInterface
? function ($type, $line) use ($output) {
$output->write(' '.$line);
} : $output
);
}

/**
* Remove the given Composer packages from the application.
*
* @param array<int, string> $packages
* @param bool $dev
* @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output
* @return bool
*/
protected function removePackages(array $packages, bool $dev = false, Closure|OutputInterface $output = null)
{
$composer = $this->findComposer();

$command = explode(' ', $composer);

array_push($command, 'remove');

$command = array_merge(
$command,
$packages,
$dev ? ['--dev'] : [],
);

return 0 === (new Process($command, cwd: $this->workingPath, env: ['COMPOSER_MEMORY_LIMIT' => '-1']))
->setTimeout(null)
->run(
$output instanceof OutputInterface
? function ($type, $line) use ($output) {
$output->write(' '.$line);
} : $output
);
}

/**
* Modify the "composer.json" file contents using the given callback.
*
* @param callable(array):array $callback
* @return void
*
* @throw \RuntimeException
*/
public function modify(callable $callback)
{
$composerFile = "{$this->workingPath}/composer.json";

if (! file_exists($composerFile)) {
throw new RuntimeException("Unable to locate `composer.json` file at [{$this->workingPath}].");
}

$composer = json_decode(file_get_contents($composerFile), true, 512, JSON_THROW_ON_ERROR);

file_put_contents(
$composerFile,
json_encode(
call_user_func($callback, $composer),
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
)
);
}

/**
* Regenerate the Composer autoloader files.
*
Expand Down