phar-io/composer-distributor
Library to build Composer plugins that install and update PHAR-based tools instead of source code. Ideal for dev utilities like PHPUnit/PHPStan/Psalm: keep installs via Composer while avoiding dependency conflicts by distributing signed PHAR releases.
composer require phar-io/composer-distributor
PharIo\ComposerDistributor\ComposerDistributor.use PharIo\ComposerDistributor\ComposerDistributor;
$distributor = new ComposerDistributor();
$composerPhar = $distributor->getComposerPhar('2.5.8');
echo $composerPhar->getPath(); // Path to the verified PHAR
$HOME/.cache/composer-distributor (override via constructor).$distributor = new ComposerDistributor('/path/to/cache');
$composer = $distributor->getComposerPhar('2.5.8'); // Always uses 2.5.8
Composer\Factory for automated dependency resolution:
$composer = $distributor->getComposerPhar('2.5.8');
$factory = new Composer\Factory();
$composer = $factory->create($composer->getPath(), '/path/to/project');
$composer->run();
getLatestStableComposerPhar() for non-pinned setups.PharIo\ComposerDistributor\Verifier\ComposerPharVerifier for additional checks.false to setVerify() to skip verification (not recommended for security).namespace App\Console\Commands;
use PharIo\ComposerDistributor\ComposerDistributor;
class UpdateComposer extends Command {
protected $signature = 'composer:update {version}';
public function handle(ComposerDistributor $distributor) {
$phar = $distributor->getComposerPhar($this->argument('version'));
$this->info("Updated to: {$phar->getVersion()}");
}
}
$this->app->singleton(ComposerDistributor::class, function ($app) {
return new ComposerDistributor($app['config']['composer.cache']);
});
try-catch:
try {
$phar = $distributor->getComposerPhar('2.5.8');
} catch (\PharIo\ComposerDistributor\Exception\ComposerPharException $e) {
$this->error("Composer PHAR verification failed: {$e->getMessage()}");
}
chmod -R 755 ~/.cache/composer-distributor).setDebug(true) to log download/verification steps.composer self-update --check after distribution.PharIo\ComposerDistributor\Downloader\ComposerPharDownloaderInterface for private mirrors.PharIo\ComposerDistributor\Verifier\ComposerPharVerifier to use custom keys.PharIo\ComposerDistributor\ComposerPhar to add metadata or modify the PHAR.setVerify(false)) bypasses all integrity checks—use cautiously in production./opt/composer-cache) for team-wide reuse.VOLUME ["/path/to/cache"]
How can I help you explore Laravel packages today?