pact-foundation/composer-downloads-plugin
This package, composer-downloads-plugin, is a lightweight Composer plugin designed to simplify tracking and managing package download statistics for PHP projects. It integrates seamlessly with Laravel projects to provide insights into package adoption without requiring manual instrumentation.
Installation:
Add the plugin to your project's composer.json under "extra":
{
"extra": {
"composer-downloads-plugin": {
"apiKey": "your_api_key_here", // Optional, if using a hosted service
"command-class": "App\\Composer\\CustomTrackCommand" // Optional override
}
}
}
Run composer require pact-foundation/composer-downloads-plugin.
First Use Case:
After installation, the plugin automatically tracks package downloads via Composer's post-update-cmd and post-install-cmd hooks. For Laravel projects, leverage this to:
PackageDownloaded) via Composer scripts:
{
"scripts": {
"post-update-cmd": [
"@composer-downloads-plugin track",
"php artisan event:dispatch PackageDownloaded"
]
}
}
Automatic Tracking with Symfony 8/Laravel:
The plugin now officially supports Symfony 8. Ensure your composer.json includes the plugin under "extra" (no conflicts with Symfony's built-in plugins). Example for Laravel:
# .github/workflows/ci.yml
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install --no-interaction
- run: composer downloads:track # Explicitly trigger tracking
PHP 8.4/8.5 Integration:
composer why-not pact-foundation/composer-downloads-plugin to verify constraints.Custom API Endpoints:
Extend the plugin to use self-hosted APIs by configuring endpoint and apiKey in composer.json:
{
"extra": {
"composer-downloads-plugin": {
"endpoint": "https://your-service.com/api/downloads",
"apiKey": "your_key"
}
}
}
composer.json to identify unused packages (now with Symfony 8 compatibility).macOS Command Resolution:
$(composer show -s pact-foundation/composer-downloads-plugin)/vendor/bin/composer downloads:track
PATH:
export PATH="$PATH:$HOME/.composer/vendor/bin"
Symfony 8 Plugin Conflicts:
composer.json:
{
"config": {
"platform-check": false,
"sort-packages": true
}
}
PHP Version Quirks:
composer validate to check for version conflicts.Verbose Mode:
Run Composer with -vvv to debug plugin execution:
composer install -vvv
Look for logs like [ComposerDownloadsPlugin] Tracking package: vendor/package.
Exit Code Handling:
The plugin now correctly returns exit codes (e.g., 1 for "command not found"). Use echo $? to verify.
Custom Events in Laravel:
Listen to the DownloadTracked event to extend functionality:
// app/Providers/ComposerDownloadsServiceProvider.php
namespace App\Providers;
use PactFoundation\ComposerDownloadsPlugin\Events\DownloadTracked;
use Illuminate\Support\ServiceProvider;
class ComposerDownloadsServiceProvider extends ServiceProvider
{
public function boot()
{
DownloadTracked::listen(function ($event) {
\Log::info('Package downloaded', ['package' => $event->package]);
});
}
}
Override Command Logic:
Extend the plugin by overriding the TrackCommand class:
{
"extra": {
"composer-downloads-plugin": {
"command-class": "App\\Composer\\CustomTrackCommand"
}
}
}
Self-Hosted API Extensions:
Fork the package and extend PactFoundation\ComposerDownloadsPlugin\Client\ApiClient for custom authentication or rate limiting.
How can I help you explore Laravel packages today?