imanghafoori/composer-json
Read and query data from any composer.json with a simple API. Provide the absolute path, create an instance, and access common fields via convenient methods—useful for tooling, package inspection, and build scripts.
Install the package via Composer:
composer require imanghafoori/composer-json
Then instantiate the reader in your PHP code by passing a directory (not a file path) where composer.json resides:
use ImanGhafoori\ComposerJson\ComposerJson;
$composer = ComposerJson::make(base_path()); // 👈 common for Laravel apps
Start with the most useful methods:
$name = $composer->getName(); // "vendor/project"
$version = $composer->getVersion(); // "1.5.2"
$requires = $composer->getRequires(); // ["php": "^8.1", "guzzlehttp/guzzle": "^7.0", ...]
Self-diagnosing CLI tools: Embed version and dependency info in artisan commands or console scripts:
$info = ComposerJson::make(base_path());
$this->line(sprintf('🚀 %s v%s', $info->getName(), $info->getVersion()));
Dependency audits: Scan for risky or outdated packages at build time:
$requires = ComposerJson::make(base_path())->getRequires();
if (isset($requires['laravel/framework'])) {
// Check if framework is pinned to a stable version
}
Dynamic configuration: Branch logic based on project type or minimum PHP version:
$minPhp = $composer->getMinimumPhpVersion(); // e.g., "8.1"
if (PHP_VERSION_ID < $minPhp) {
throw new RuntimeException("Requires PHP ≥ {$minPhp}");
}
CI/CD integration: Pass metadata to deploy scripts, Docker builds, or release notes:
$composer = ComposerJson::make(base_path());
echo "BUILD_VERSION={$composer->getVersion()}\n";
echo "BUILD_PACKAGE={$composer->getName()}\n";
Path is critical: make() must receive a directory path (e.g., base_path()), not a file path. Passing __DIR__ . '/composer.json' will fail silently or throw.
Read-only by design: This package only reads composer.json. To modify it, use Composer’s Platform utilities, or tools like symfony/process with jq/composer CLI.
Case sensitivity: Keys in composer.json are normalized internally to lowercase (e.g., getRequires() returns ['php', 'monolog/monolog']), but package names retain case (monolog/monolog, not Monolog/monolog).
Missing fields: Methods like getAuthors(), getExtra(), or getautoload() return null if absent — handle gracefully:
$autoload = $composer->getAutoload() ?? [];
$psr4 = $autoload['psr-4'] ?? [];
No environment fallback: Unlike .env, composer.json is static. Don’t rely on this package for runtime configuration (e.g., DB credentials). Use Laravel’s config system instead.
Extensibility: Since the class is thin, extend via helper methods or traits:
trait ComposerExt {
public function isDevDep(string $pkg): bool {
return isset(ComposerJson::make(base_path())->getDevRequires()[$pkg]);
}
}
How can I help you explore Laravel packages today?