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

Contract Laravel Package

hyperf/contract

Core contracts for Hyperf: a set of lightweight PHP interfaces that define common behaviors across the framework (DI, events, middleware, serialization, etc.). Helps decouple components, improve testability, and keep implementations swappable.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Modular Monolith or Microservices Architecture: Leverage Hyperf contracts to define service boundaries in a Laravel-based system, enabling independent scaling of components (e.g., payment processing, notifications) while maintaining a unified codebase. Aligns with Domain-Driven Design (DDD) by encapsulating domain logic behind interfaces.

  • Strategic Tech Stack Evolution:

    • Hybrid Laravel/Hyperf Systems: Justify adopting Hyperf contracts as a bridge for gradual migration to Hyperf microservices or high-performance modules (e.g., real-time APIs, heavy compute tasks).
    • Multi-Framework Integration: Use contracts to decouple Laravel frontend services from Hyperf backend services (e.g., via gRPC or HTTP APIs), reducing coupling between teams.
  • API-First Development: Define Hyperf contracts as API specifications for external services (e.g., third-party integrations, mobile apps). Example: Hyperf\Contract\AuthInterface could serve as the basis for an OAuth2 API contract shared across platforms.

  • Vendor Abstraction Layer: Build framework-agnostic abstractions over third-party services (e.g., Hyperf\Contract\PaymentGatewayInterface) to switch providers (e.g., Stripe → PayPal) without changing business logic. Critical for compliance-heavy industries (e.g., fintech) where provider lock-in is risky.

  • Performance Optimization:

    • Offload Heavy Workloads: Use Hyperf contracts to delegate CPU-intensive tasks (e.g., image processing, ML inference) to Hyperf microservices while keeping Laravel for HTTP-facing logic.
    • Caching Strategies: Standardize caching interfaces (Hyperf\Contract\CacheInterface) to optimize data layer performance across services.
  • Build vs. Buy:

    • Buy: Adopt Hyperf contracts to avoid reinventing interface definitions for common services (e.g., queues, caching).
    • Build: Implement custom logic behind contracts to differentiate (e.g., a proprietary Hyperf\Contract\RecommendationEngineInterface).
  • Use Cases:

    • Legacy System Modernization: Gradually introduce Hyperf contracts to decouple monolithic Laravel apps by wrapping legacy code behind interfaces.
    • Plugin/Extension Architecture: Define contracts for third-party plugins (e.g., Hyperf\Contract\PluginInterface) to ensure consistency and security.
    • Event-Driven Systems: Use Hyperf\Contract\EventDispatcherInterface to standardize event handling across services.

When to Consider This Package

  • Adopt if:

    • Your Laravel project is tightly coupled and needs modularity (e.g., preparing for microservices or plugin architecture).
    • You’re integrating with Hyperf services (e.g., Laravel frontend + Hyperf backend) and need shared contracts for service-to-service communication.
    • You prioritize testability and want to mock dependencies (e.g., payment gateways, external APIs) using interfaces.
    • Your team is migrating from Laravel to Hyperf or adopting a hybrid architecture and needs a smooth transition path.
    • You’re building a library or SDK that must work with both Laravel and Hyperf environments.
    • You need standardized interfaces for cross-cutting concerns (e.g., logging, caching, auth) to reduce technical debt.
  • Look elsewhere if:

    • Your project is small-scale with no modularity needs (e.g., a simple CRUD app).
    • You’re not using Hyperf and have no plans to integrate with it (the package is Hyperf-specific).
    • Your team lacks experience with interface-driven design (may require training or lead to over-engineering).
    • You need full implementations (this package only provides interfaces; you’ll need to provide concrete classes).
    • Laravel’s built-in contracts (Illuminate\Contracts) fully meet your needs (e.g., for basic caching or queue workers).
    • You’re in a highly regulated environment where vendor lock-in to Hyperf’s ecosystem is a risk (e.g., if Hyperf’s roadmap is unstable).

How to Pitch It (Stakeholders)

For Executives:

*"This package lets us future-proof our Laravel system by adopting Hyperf’s standardized interfaces—like a ‘universal adapter’ for services, APIs, and integrations. Here’s why it matters:

  • Reduces Risk: Swap out third-party services (e.g., payment processors) without rewriting core logic, saving $X in vendor lock-in costs.
  • Speeds Up Development: Teams can work in parallel on independent modules (e.g., notifications, auth) behind shared contracts, cutting time-to-market by Y%.
  • Lowers Costs: Avoids reinventing interface definitions for caching, queues, or auth—we reuse Hyperf’s battle-tested contracts.
  • Future-Proofs Migration: If we later adopt Hyperf for high-performance modules (e.g., real-time APIs), this gives us a smooth transition path without rewriting the entire system. Think of it as architectural insurance—a small upfront investment to avoid costly refactoring later. ROI: Faster development, lower risk, and flexibility to scale."*

For Engineering:

*"hyperf/contract gives us interfaces for everything Hyperf-related, so we can:

  • Decouple Laravel services from Hyperf backends (e.g., gRPC/RPC calls) using shared contracts.
  • Mock dependencies easily: Replace real services with interfaces for unit/integration testing (e.g., PaymentGatewayInterface).
  • Standardize cross-cutting concerns: Use CacheInterface, QueueInterface, etc., to avoid duplicate abstractions.
  • Prepare for migration: If we later move parts of the system to Hyperf, these contracts reduce refactoring effort.

When to Use It: ✅ New modular services (e.g., a Hyperf-based recommendation engine). ✅ Third-party integrations where you need to swap implementations. ✅ Performance-critical paths offloaded to Hyperf.

When to Avoid It: ❌ If you’re not using Hyperf and have no plans to. ❌ For simple projects where interfaces add unnecessary complexity. ❌ If Laravel’s contracts (Illuminate\Contracts) already cover your needs.

Getting Started:

  1. Install: composer require hyperf/contract.
  2. Use interfaces like Hyperf\Contract\CacheInterface in your Laravel services.
  3. Bind custom implementations via Laravel’s DI container.
  4. Start small: Define 1–2 critical contracts (e.g., AuthInterface, QueueWorkerInterface) and expand as needed.

Pro Tip: Pair with Hyperf’s RPC or gRPC for seamless service-to-service communication if migrating to microservices."*


For Architects/Tech Leads:

*"This package is a double-edged sword—it’s powerful for modularity and future-proofing, but adds complexity if misused. Here’s how to leverage it strategically:

  • Use for:
    • Service boundaries: Define contracts between Laravel and Hyperf services (e.g., OrderServiceInterface).
    • Plugin architecture: Enforce contracts for third-party plugins (e.g., Hyperf\Contract\PluginInterface).
    • Testing: Mock interfaces for critical paths (e.g., payment processing, external APIs).
  • Avoid for:
    • Simple CRUD apps: Overkill unless you’re planning for scale.
    • Tightly coupled monoliths: May introduce unnecessary abstraction layers.
  • Key Risks:
    • DI conflicts: Laravel’s container won’t natively resolve Hyperf contracts—you’ll need custom bindings.
    • Maintenance overhead: Dual contracts (Laravel + Hyperf) may require syncing updates.
  • Recommendation:
    • Start with 1–2 high-impact contracts (e.g., AuthInterface, CacheInterface).
    • Document the contract strategy (e.g., ‘Do we extend Laravel’s contracts or use Hyperf’s?’).
    • Assign ownership: Decide who maintains contract compatibility between frameworks.

Example Workflow:

  1. Define a contract (e.g., Hyperf\Contract\RecommendationInterface).
  2. Implement it in both Laravel and Hyperf (or delegate to Hyperf).
  3. Use Laravel’s DI to bind the Hyperf implementation:
    $container->bind(
        Hyperf\Contract\RecommendationInterface::class,
        HyperfRecommendationService::class
    );
    
  4. Test rigorously: Verify mocking and runtime behavior across frameworks."*
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
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