Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Composer Json Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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", ...]

Implementation Patterns

  • 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";
    

Gotchas and Tips

  • 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]);
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport