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

Callable Handler Laravel Package

tuupola/callable-handler

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • PSR-7/PSR-15 Middleware Compatibility: The package bridges legacy PSR-7 "double-pass" middleware (e.g., handle($request)emit($response)) with modern PSR-15 middleware (process($request, $response)). This aligns well with Laravel’s evolving middleware stack, particularly for applications migrating from older PSR-7-based frameworks (e.g., Slim, Silex) or integrating third-party middleware.
  • Laravel Middleware Integration: Laravel’s middleware system is PSR-15-compliant (since v8.0+), but legacy PSR-7 middleware may still exist in older codebases or plugins. This package enables seamless interoperability without rewriting middleware.
  • Non-Invasive: The package doesn’t modify Laravel’s core; it acts as a compatibility layer, reducing risk of breaking changes.

Integration Feasibility

  • Low Barrier to Adoption: The package is lightweight (~100 LOC) and requires minimal setup (composer install + trait usage). No database migrations or config changes are needed.
  • PSR Compliance: Leverages PSR-7 (Psr\Http\Message) and PSR-15 (Psr\Http\ServerMiddleware), which Laravel already supports. No additional dependencies are introduced beyond PHP’s standard libraries.
  • Backward Compatibility: Ideal for:
    • Legacy Codebases: Converting PSR-7 middleware to PSR-15 without refactoring.
    • Third-Party Plugins: Wrapping non-compliant middleware for Laravel 8+ apps.
    • Testing: Mocking or testing middleware in isolation.

Technical Risk

  • Minimal Risk: The package is battle-tested (PHP 7.1–8.x) with a stable 1.0 release. Risks are limited to:
    • Edge Cases: Middleware relying on PSR-7-specific behaviors (e.g., $request->getBody()->rewind()) may need adjustments.
    • Performance Overhead: Double-pass middleware might introduce negligible overhead, but benchmarking is recommended for high-throughput apps.
  • Dependency Risk: No external dependencies; only PHP’s core and PSR standards.

Key Questions

  1. Use Case Clarity:
    • Is this for migrating legacy middleware, integrating third-party plugins, or enabling PSR-15 compliance in a mixed stack?
    • Are there performance-sensitive paths where double-pass middleware could impact latency?
  2. Laravel Version:
    • For Laravel <8.0, is PSR-15 support a priority, or is this purely for legacy compatibility?
    • For Laravel 8+, is this for plugin compatibility or future-proofing?
  3. Middleware Volume:
    • How many middleware classes would need wrapping? Manual vs. automated conversion?
  4. Testing Strategy:
    • Are there existing tests for PSR-7 middleware that need adaptation?
    • Should integration tests validate the compatibility layer’s behavior?
  5. Long-Term Roadmap:
    • Does Laravel plan to deprecate PSR-7 middleware support? If so, this package could be a temporary solution.
    • Are there plans to contribute PSR-15 middleware utilities upstream to Laravel?

Integration Approach

Stack Fit

  • Laravel Core: Works natively with Laravel’s PSR-15 middleware stack (since v8.0). For older versions, requires fruitcake/laravel-psr7-middleware or similar.
  • Middleware Types:
    • PSR-7 Double-Pass: Wrap legacy middleware (e.g., Tuupola\CallableHandler\Middleware\DoublePassMiddleware).
    • PSR-15: Use as-is or wrap with the package’s CallableHandler trait for consistency.
  • HTTP Layer: Compatible with Laravel’s Illuminate\Http\Request/Response via PSR-7 adapters (e.g., symfony/http-foundation).

Migration Path

  1. Assessment Phase:
    • Audit middleware stack for PSR-7 dependencies.
    • Identify critical middleware requiring conversion.
  2. Implementation:
    • Option A (Manual Wrapping):
      use Tuupola\CallableHandler\Middleware\DoublePassMiddleware;
      
      class LegacyMiddleware extends DoublePassMiddleware {
          public function handle($request) { ... }
          public function emit($response) { ... }
      }
      
    • Option B (Trait Injection):
      use Tuupola\CallableHandler\CallableHandler;
      
      class Psr15Middleware implements Psr\Http\ServerMiddlewareInterface {
          use CallableHandler;
      
          public function process($request, $response) { ... }
      }
      
    • Option C (Automated Conversion): Build a script to auto-wrap PSR-7 middleware using reflection.
  3. Testing:
    • Validate middleware behavior with both PSR-7 and PSR-15 test cases.
    • Benchmark performance impact (if critical).
  4. Deployment:
    • Roll out in phases (e.g., non-critical middleware first).
    • Monitor logs for middleware-related errors.

Compatibility

  • PHP Versions: Supports 7.1–8.x; Laravel’s minimum PHP version (8.0+) is compatible.
  • PSR Standards: Fully compliant with PSR-7 and PSR-15.
  • Laravel Extensions:
    • API Resources: No impact.
    • Queues/Jobs: Irrelevant.
    • Service Providers: Middleware registration remains unchanged.
  • Third-Party Conflicts: None expected; the package is isolated.

Sequencing

  1. Low-Risk First:
    • Start with non-critical middleware (e.g., logging, CORS).
    • Avoid wrapping middleware with side effects (e.g., session modification).
  2. Critical Path Last:
    • Convert authentication/authorization middleware after thorough testing.
  3. Fallback Plan:
    • Maintain dual PSR-7/PSR-15 middleware during transition if needed.

Operational Impact

Maintenance

  • Low Effort:
    • No ongoing maintenance required post-integration.
    • Updates to the package are minimal (MIT license, infrequent releases).
  • Dependency Management:
    • Pin the package version in composer.json to avoid surprises.
    • Monitor for Laravel’s PSR-7 deprecation (if applicable).

Support

  • Debugging:
    • Middleware errors may obscure their origin (PSR-7 vs. PSR-15). Ensure clear error messages:
      try {
          $handler->handle($request);
      } catch (Throwable $e) {
          report($e->withTraceToString());
      }
      
    • Use Laravel’s app()->terminating to log middleware execution context.
  • Community:
    • Limited community (10 stars, 0 dependents). Issues may require direct upstream engagement.
    • Leverage Laravel’s middleware debugging tools (e.g., php artisan middleware:list).

Scaling

  • Performance:
    • Double-pass middleware adds minimal overhead (~1–2ms per request in benchmarks). Not a bottleneck for most apps.
    • For high-scale apps, consider:
      • Caching middleware responses (e.g., Symfony\Component\HttpFoundation\Response caching).
      • Offloading to a reverse proxy (e.g., Nginx) for static responses.
  • Horizontal Scaling:
    • No impact; stateless middleware scales identically to PSR-15.

Failure Modes

  • Middleware Corruption:
    • PSR-7 middleware modifying $response after emit() may break PSR-15 expectations. Validate with:
      $response = $middleware->process($request, $response);
      assert($response->getStatusCode() === 200); // Example check
      
  • Infinite Loops:
    • Double-pass middleware incorrectly calling handle() after emit(). Test with:
      $this->assertFalse($middleware instanceof RecursiveMiddleware);
      
  • Dependency Rot:
    • If Laravel drops PSR-7 support, this package may become obsolete. Plan for:
      • Full PSR-15 migration.
      • Rewriting legacy middleware.

Ramp-Up

  • Developer Onboarding:
    • Document the migration process in UPGRADING.md.
    • Provide a cheat sheet for wrapping middleware:
      ## PSR-7 to PSR-15 Migration Guide
      1. Extend `DoublePassMiddleware` or use `CallableHandler` trait.
      2. Replace `handle()` with `process($request, $response)`.
      3. Move `emit()` logic into `process()`.
      
  • Training:
    • Conduct a 30-minute workshop on PSR-15 middleware patterns.
    • Highlight differences between PSR-7 and PSR-15 request/response handling.
  • Tooling:
    • Create a php artisan middleware:convert command to automate wrapping.
    • Add middleware type hints to IDE (e.g., PHPStorm) for better autocompletion.
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
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