Skip to content

Commit

Permalink
[10.x] Add commonly reusable Composer related commands from 1st party…
Browse files Browse the repository at this point in the history
… packages (#48096)

* Add commonly reusable Composer related commands from 1st party packages.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* formatting

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
crynobone and taylorotwell committed Aug 17, 2023
1 parent 85ad68c commit d3a5f3e
Showing 1 changed file with 94 additions and 0 deletions.
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

0 comments on commit d3a5f3e

Please sign in to comment.