composer/spdx-licenses
SPDX licenses list and validation library extracted from Composer. Look up licenses and exceptions by identifier or name, check OSI approval and deprecation, and validate SPDX license expressions using official SPDX License List data.
Install via Composer:
composer require composer/spdx-licenses
First use case: Validate a license in composer.json or a package submission.
use Composer\Spdx\SpdxLicenses;
$validator = new SpdxLicenses();
// Basic validation (returns bool)
$isValid = $validator->validate('MIT'); // true
$isValid = $validator->validate('PROPRIETARY'); // false
// Get license details
$license = $validator->getLicenseByIdentifier('MIT');
echo $license['name']; // "MIT License"
Where to look first:
SpdxLicenses::validate() for quick checks.SpdxLicenses::getLicenses() to iterate all licenses programmatically.Workflow: Enforce SPDX compliance during dependency resolution.
// In a Composer plugin or script
$validator = new SpdxLicenses();
$requiredLicenses = ['MIT', 'Apache-2.0', 'GPL-3.0-only'];
foreach ($requiredLicenses as $license) {
if (!$validator->validate($license)) {
throw new \RuntimeException("Invalid SPDX license: {$license}");
}
}
Integration Tip:
post-autoload-dump or post-install-cmd scripts to validate composer.json licenses.Use Case: Block non-OSI-approved licenses in open-source projects.
$validator = new SpdxLicenses();
$license = 'MIT';
if ($validator->isOsiApprovedByIdentifier($license)) {
// Allow open-source distribution
} else {
// Trigger compliance workflow (e.g., GitHub issue, CI failure)
}
Pattern:
$osiApproved = $validator->isOsiApprovedByIdentifier('AGPL-3.0-only'); // true
Use Case: Display license metadata in admin panels or API responses.
// In a Laravel service or controller
public function getLicenseDetails(string $spdxId): array
{
$validator = new SpdxLicenses();
$license = $validator->getLicenseByIdentifier($spdxId);
return [
'id' => $spdxId,
'name' => $license['name'] ?? 'Unknown',
'url' => $license['licenseUrl'] ?? null,
'osi_approved' => $validator->isOsiApprovedByIdentifier($spdxId),
];
}
Integration Tip:
SpdxLicenses to Laravel’s IoC container:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(SpdxLicenses::class);
}
Use Case: Validate licenses in bulk (e.g., Packagist submissions).
$validator = new SpdxLicenses();
$licenses = ['MIT', 'GPL-2.0', 'INVALID-LICENSE'];
$results = array_map(
fn($id) => [
'id' => $id,
'valid' => $validator->validate($id),
'details' => $validator->getLicenseByIdentifier($id),
],
$licenses
);
Performance Tip:
SpdxLicenses::getLicenses() to preload all licenses into memory if validating many IDs.Use Case: Validate complex SPDX expressions (e.g., MIT AND Apache-2.0).
$validator = new SpdxLicenses();
$expression = 'MIT AND (Apache-2.0 OR GPL-3.0)';
if ($validator->validate($expression)) {
// Parse expression further (e.g., with `spdx-expression` package)
}
Extension Point:
spdx-expression for advanced parsing.Case Sensitivity:
MIT = mit), but names are case-sensitive.getIdentifierByName('MIT License') ≠ getIdentifierByName('mit license').Deprecated Licenses:
BSD-2-Clause-FreeBSD) are deprecated but still valid.isDeprecatedByIdentifier() to handle gracefully:
if ($validator->isDeprecatedByIdentifier('BSD-2-Clause-FreeBSD')) {
logWarning("Deprecated license detected");
}
SPDX Expression Limits:
validate() method supports SPDX 3.0+ expressions, but complex nested expressions may fail.$validator->validate('(MIT AND GPL-3.0) OR (Apache-2.0 WITH LLVM-exception)');
PHP Version:
License Exceptions:
GPL-2.0 WITH Classpath-exception-2.0) are not auto-validated by default.getExceptionByIdentifier() to check exceptions explicitly.List All Licenses:
$allLicenses = $validator->getLicenses();
print_r(array_keys($allLicenses)); // Debug available IDs
Validate Against SPDX Spec:
Update License Data:
SpdxLicensesUpdater (advanced use).Performance:
SpdxLicenses once and reuse it.Custom License Rules:
SpdxLicenses:
class CustomSpdxValidator extends SpdxLicenses {
public function isAllowedInProject(string $licenseId): bool {
$allowed = ['MIT', 'Apache-2.0', 'BSD-3-Clause'];
return in_array($licenseId, $allowed, true);
}
}
SPDX-RDF Generation:
spdx-tools to generate SPDX documents for compliance reporting.Laravel Artisan Command:
spdx:audit command to scan dependencies:
// app/Console/Commands/AuditLicenses.php
public function handle() {
$validator = new SpdxLicenses();
$composer = include base_path('composer.json');
$license = $composer['license'] ?? 'UNLICENSED';
if (!$validator->validate($license)) {
$this->error("Invalid SPDX license: {$license}");
exit(1);
}
}
GitHub Actions Integration:
# .github/workflows/license-check.yml
jobs:
check-license:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer require composer/spdx-licenses
- run: |
php -r "
require 'vendor/autoload.php';
\$validator = new \Composer\Spdx\SpdxLicenses();
\$license = json_decode(file_get_contents('composer.json'), true)['license'];
if (!$validator->validate(\$license)) exit(1);
"
SpdxLicenses instance or its data.laravel-spdx) for easier adoption.getIdentifierByName()).composer.json (e.g., suggest alternatives).How can I help you explore Laravel packages today?