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

Php Composer Reader Laravel Package

nadar/php-composer-reader

Small PHP library to read and manipulate composer.json. Load and validate readability/writability, dump full content, and work with typed section readers (e.g., require, autoload PSR-4) to iterate packages/namespaces, inspect constraints, and add sections.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment:

    • Expanded Runtime Flexibility: The new allow input from array feature (PR #16) enables programmatic composer.json manipulation without file I/O, critical for:
      • Dynamic dependency injection (e.g., runtime feature flags based on in-memory composer.json clones).
      • CI/CD pipelines generating synthetic composer.json snippets (e.g., for testing).
      • Laravel’s config() system extensions (e.g., config('custom.dependencies') sourced from parsed arrays).
    • CLI/Artisan Integration: New command output methods (PR #17) bridge gaps with Laravel’s Artisan commands, enabling:
      • Dependency analysis tools (e.g., php artisan dependency:audit).
      • Custom composer script replacements (e.g., post-update-cmd logic in PHP).
    • Laravel Synergy:
      • Service Container Integration: The array-input feature aligns with Laravel’s dependency injection (e.g., pass a composer.json array from a config file).
      • Event Listeners: Combine with Composer\EventDispatcher for post-install hooks (e.g., trigger composer.post-autoload-dump events dynamically).
  • Laravel-Specific Opportunities:

    • Dynamic Providers: Use array input to generate config('app.providers') at runtime (e.g., load providers only if specific packages are installed).
    • Environment-Aware Configs: Merge composer.json data with Laravel’s config() (e.g., config(['package_versions' => $reader->getVersions()])).
  • Alternatives Revisited:

    • spatie/laravel-package-tools: Still preferred for package development, but this package now offers low-level control for edge cases (e.g., modifying composer.json in-memory before writing to disk).
    • Native Composer Facade: Lacks the new array-input and CLI utilities; this package fills gaps for programmatic, non-file-based workflows.

Integration Feasibility

  • PHP 8.3/8.4 Support:

    • Pros: Future-proofs Laravel 10/11 projects (PHP 8.3+ required). No breaking changes reported.
    • Cons: If using PHP 8.2 or lower, pin to 2.0.x to avoid potential strict typing issues.
  • Array Input Feature:

    • Game-Changer: Eliminates need for file_get_contents() calls, reducing I/O overhead in:
      • Performance-Critical Paths: E.g., parsing composer.json in middleware or request pipelines.
      • Testing: Simulate composer.json without touching disk (e.g., unit tests with mock arrays).
    • Caveat: Ensure input arrays strictly adhere to Composer schema (e.g., nested require/replace structures).
  • Command Output Methods:

    • Laravel CLI Integration: Directly usable in Artisan commands (e.g., Artisan::call('composer:audit', ['--format' => 'json'])).
    • Output Formatting: Supports JSON/array outputs, aligning with Laravel’s JsonResponse or Artisan output helpers.
  • Dependency Conflicts:

    • None: Package remains dependency-free. PHP 8.3+ features (e.g., typed properties) are backward-compatible.

Technical Risk

  • Low-Moderate (Previously Low):

    • New Risks:
      • Schema Validation for Arrays: The array-input feature may accept malformed data (e.g., missing name field). Mitigation: Validate against Composer schema (e.g., Composer\Semver\VersionParser) before processing.
      • PHP 8.4 Strict Typing: Potential edge cases with new return types (e.g., array<string, mixed>). Mitigation: Test with PHP 8.4 in CI.
    • Existing Risks (Unchanged):
      • Schema Drift: Still requires proactive monitoring (e.g., Composer schema updates).
      • Edge Cases: Circular references or invalid types in composer.json arrays may cause silent failures. Mitigation: Wrap array input in a try-catch with schema validation.
  • Key Questions Updated:

    1. Array Input Safety:
      • How will you validate user-provided arrays against the Composer schema? Will you use json_schema or a custom validator?
    2. PHP Version Locking:
      • Should you pin to ^2.1 (PHP 8.3+) or ~2.0 (PHP 7.4+) for broader compatibility?
    3. CLI Integration:
      • Will you use the new command output methods for Artisan commands? If so, how will you handle output formatting (e.g., JSON vs. table)?
    4. Performance Impact:
      • With array input, will you cache parsed results in Laravel’s cache system (e.g., Cache::remember)?
    5. Security:
      • If accepting user-provided composer.json arrays (e.g., from API inputs), how will you prevent injection attacks (e.g., arbitrary code in autoload scripts)?

Integration Approach

Stack Fit

  • PHP/Laravel: Enhanced Fit with new features:

    • Array Input: Replaces json_decode(file_get_contents()) in all use cases (runtime, testing, CLI).
    • Command Output: Directly integrates with Laravel’s Artisan and JsonResponse.
    • PHP 8.3/8.4: Aligns with Laravel 10/11’s PHP requirements.
  • Use Cases Expanded:

    Feature Laravel Integration Example
    Array Input config(['dynamic_providers' => $reader->parse($arrayConfig)])
    Command Output Artisan::output($reader->getCommandOutput('json'))
    PHP 8.3+ Support Laravel 10+ projects without version conflicts.

Migration Path

  1. Phase 0: Prep Work (New)

    • Add PHP 8.3+ Support: Update php.ini or Laravel’s php-version config if needed.
    • Schema Validation: Implement a validator for array inputs (e.g., using webmozart/assert or json_schema).
  2. Phase 1: Replace File Parsing (Updated)

    • Old: json_decode(file_get_contents(base_path('composer.json')))
    • New: $reader->parse(file: false, array: $composerJsonArray)
    • Example:
      $reader = app(Nadar\ComposerReader\ComposerJsonReader::class);
      $config = $reader->parse(array: [
          'name' => 'app/package',
          'require' => ['laravel/framework' => '^10.0'],
      ]);
      
  3. Phase 2: CLI/Artisan Integration (New)

    • Add Command:
      use Nadar\ComposerReader\ComposerJsonReader;
      
      class AuditCommand extends Command {
          protected function handle() {
              $reader = new ComposerJsonReader();
              $this->output->write($reader->getCommandOutput('table'));
          }
      }
      
    • Register in AppServiceProvider:
      $this->app->singleton(ComposerJsonReader::class, fn() => new ComposerJsonReader());
      
  4. Phase 3: Dynamic Runtime Logic (Updated)

    • Example: Load providers conditionally:
      $reader = app(ComposerJsonReader::class);
      $packages = $reader->parse()->getPackages();
      if ($packages['monolog/monolog'] ?? false) {
          Config::set('app.providers', array_merge(config('app.providers'), [MonologServiceProvider::class]));
      }
      

Compatibility

  • Laravel Versions:
    • Recommended: Laravel 10+ (PHP 8.3+). For Laravel 9, pin to 2.0.x.
    • Lumen: Compatible if using PHP 8.3+.
  • Composer Versions:
    • Test against Composer 2.6+ (latest schema). Use composer validate in CI to catch drift.
  • PHP Extensions:
    • None. PHP 8.3+ features (e.g., array<string, mixed>) are optional.

Sequencing

  1. Phase 1: Replace all file_get_contents() calls with array input (lowest risk).
  2. Phase 2: Implement schema validation for array inputs (critical for security).
  3. Phase 3: Integrate command output methods into Artisan commands.
  4. Phase 4: Add caching for parsed arrays (e.g., `Cache::remember('com
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.
aimeos/prisma
besmartand-pro/php-quality-config
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views