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

Package Versions Laravel Package

ocramius/package-versions

Fast, zero-I/O access to installed Composer package versions from composer.lock. Get dependency versions at runtime via PackageVersions\Versions::getVersion(), with versions compiled during install/update—ideal for building assets or artifacts based on dependency versions.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Zero-I/O Runtime Access: The package pre-compiles version data from composer.lock during composer install/update, enabling O(1) version lookups at runtime. This aligns perfectly with Laravel’s need for low-latency dependency checks (e.g., feature flags, conditional logic, or compliance checks).
  • Stateless Design: No external dependencies or runtime filesystem access, reducing cold-start overhead in serverless/Laravel Forge deployments.
  • Composer Lock Dependency: Tight coupling to composer.lock ensures consistency but requires adherence to Laravel’s standard Composer workflow (e.g., no dynamic require-dev packages in production).

Integration Feasibility

  • Laravel Compatibility:
    • Service Provider Integration: Can be bootstrapped via Laravel’s register() method to expose versions as a singleton service (e.g., app('package-versions')).
    • Artisan Command: Extend Laravel’s CLI with a php artisan package:versions command for debugging.
    • Config Cache: Leverage Laravel’s config caching to memoize version data if accessed frequently.
  • Caching Layer: Pair with Laravel’s cache driver (Redis/Memcached) to avoid recomputing versions across requests in high-traffic apps.
  • Testing: Mock PackageVersions\Versions in PHPUnit tests using Laravel’s mocking utilities (e.g., createMock()).

Technical Risk

  • Composer Lock Volatility:
    • Risk: Version data staleness if composer.lock is regenerated mid-deployment (e.g., via composer install --no-scripts).
    • Mitigation: Use Laravel’s deployment hooks (e.g., post-install-cmd) to trigger version recompilation.
  • Autoloader Conflicts:
    • Risk: Classmap collisions if multiple plugins generate classes with similar names (e.g., Versions_composer_tmpX).
    • Mitigation: Explicitly configure Laravel’s classmap to exclude auto-generated PackageVersions classes or use a custom namespace.
  • PHP Version Support:
    • Risk: Dropped PHP 8.2/8.3 support in v2.12.0 may conflict with legacy Laravel apps (e.g., LTS 8.x).
    • Mitigation: Pin to ~2.11.0 if using PHP 8.4+ or upgrade Laravel to PHP 8.5+.

Key Questions

  1. Use Cases:
    • Will versions be used for runtime logic (e.g., feature toggles) or static metadata (e.g., build artifacts)?
    • Are there SLA requirements for version accuracy (e.g., real-time vs. stale-acceptable)?
  2. Deployment Workflow:
    • How is composer.lock managed? (Manual updates, CI/CD, or dynamic composer install?)
    • Can version recompilation be atomic during deployments (e.g., no partial updates)?
  3. Scaling:
    • Will version data be shared across microservices (e.g., via API) or isolated per app?
  4. Monitoring:
    • Should version mismatches (e.g., composer.lock vs. runtime) trigger alerts (e.g., Laravel Horizon)?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Service Container: Register PackageVersions\Versions as a bound service with a custom resolver:
      $this->app->bind('package-versions', function () {
          return new PackageVersions\Versions();
      });
      
    • Facade: Create a PackageVersions facade for fluent access:
      use Illuminate\Support\Facades\Facade;
      
      class PackageVersions extends Facade {
          protected static function getFacadeAccessor() { return 'package-versions'; }
      }
      
    • Blade Directives: Extend Blade to embed versions in templates:
      Blade::directive('packageVersion', function ($package) {
          return "<?php echo PackageVersions::getVersion('{$package}'); ?>";
      });
      
      Usage: @packageVersion('laravel/framework')
  • Artisan Commands:
    • Add a package:versions command to dump all versions to a file or cache:
      php artisan package:versions:dump --format=json --output=versions.json
      
  • Event Listeners:
    • Listen to Laravel’s composer.installed event to recompile versions post-install:
      Event::listen('composer.installed', function () {
          $this->app['package-versions']->recompile();
      });
      

Migration Path

  1. Phase 1: Proof of Concept
    • Install the package in a staging environment with optimize-autoloader: true.
    • Test version retrieval in a Laravel controller and Blade template.
    • Verify performance impact (e.g., microtime before/after getVersion()).
  2. Phase 2: Integration
    • Register the service in AppServiceProvider.
    • Add a facade for consistent API access.
    • Implement a cache layer (e.g., Redis) for high-traffic endpoints.
  3. Phase 3: Deployment
    • Update composer.json to include post-install-cmd for version recompilation:
      "scripts": {
          "post-install-cmd": [
              "Illuminate\\Foundation\\ComposerScripts::postInstall",
              "@php artisan package:versions:recompile"
          ]
      }
      
    • Test rollbacks to ensure version data persists across failed deployments.

Compatibility

  • Laravel Versions:
    • LTS (8.x/9.x/10.x): Fully compatible with PHP 8.1+ (pin to ~2.11.0 for PHP 8.4+).
    • Legacy (7.x): Requires downgrading to ~1.10.1 (PHP 7.4+) but may miss optimizations.
  • Composer Config:
    • Ensure optimize-autoloader: true is set in composer.json.
    • Avoid composer install --no-scripts in production (use --scripts or post-install-cmd).
  • Caching:
    • Laravel’s config cache (php artisan config:cache) can memoize version data if accessed via service container.

Sequencing

  1. Pre-requisite: Ensure composer.lock is version-controlled and regenerated in CI/CD.
  2. Installation: Add to composer.json and run composer install --optimize.
  3. Bootstrap: Register the service in AppServiceProvider before any version-dependent logic.
  4. Testing: Validate versions in unit tests (mock Versions) and integration tests (real composer.lock).
  5. Monitoring: Add health checks for version data consistency (e.g., compare composer.lock hashes).

Operational Impact

Maintenance

  • Dependency Updates:
    • Monitor ocramius/package-versions for PHP version drops (e.g., v2.12.0 drops 8.2/8.3).
    • Use Renovate or Dependabot to auto-update minor/patch versions.
  • Composer Lock Drift:
    • Risk: Manual composer update may invalidate version data.
    • Mitigation: Enforce CI/CD-composer-lock workflows (e.g., GitHub Actions with composer.lock checks).
  • Debugging:
    • Add a php artisan package:versions:debug command to log raw composer.lock parsing.

Support

  • Common Issues:
    • Stale Versions: Clear Laravel’s config cache (php artisan config:clear) if versions appear outdated.
    • Class Not Found: Ensure composer dump-autoload --optimize is run post-install.
    • Permission Errors: Verify composer.lock is writable (rare, but possible in shared hosting).
  • Documentation:
    • Add a README section in the Laravel app explaining version data sources and caching.
    • Include troubleshooting steps for deployment failures (e.g., partial composer.lock updates).

Scaling

  • Horizontal Scaling:
    • Version data is stateless after initial load, so no additional scaling needed for multi-server setups.
    • Cache Invalidation: Use Laravel’s tagged caching (e.g., cache()->forget('package-versions')) during deployments.
  • Performance:
    • Benchmark: Compare getVersion() latency with/without caching (target <1ms for critical paths).
    • Cold Starts: Pre-warm versions in Laravel’s global after middleware:
      app('package-versions')->getVersion('laravel/framework'); // Pre-load
      
  • Memory:

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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata