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.
## Technical Evaluation
### Architecture Fit
- **Misalignment with Laravel’s Native Stack**: This package is **Hyperf-specific**, while Laravel has its own mature contract system (`Illuminate\Contracts`). Introducing Hyperf contracts into a Laravel codebase risks **architectural fragmentation** unless there’s a **strategic reason** (e.g., Hyperf migration, microservices integration, or plugin architecture).
- **Decoupling Potential**: If the goal is **modularity** (e.g., swappable auth, caching, or payment gateways), Hyperf’s contracts *could* provide **alternative abstractions**, but Laravel’s contracts already fulfill this role for most use cases.
- **Overlap with Laravel’s Contracts**: Many Hyperf contracts (e.g., `CacheInterface`, `QueueWorkerInterface`) **duplicate Laravel’s existing interfaces**, leading to **redundancy** unless there’s a **clear technical or strategic advantage** (e.g., Hyperf-specific optimizations).
### Integration Feasibility
- **Low Feasibility for Pure Laravel**: Without Hyperf in the stack, this package offers **minimal value** beyond what Laravel already provides. Integration would require **adapter layers** or **dual-contract implementations**, increasing complexity.
- **Feasible for Hybrid Architectures**: If the system **integrates with Hyperf microservices** or uses **Hyperf for specific high-performance modules**, the contracts could define **cross-framework boundaries**.
- **Dependency Isolation**: Since the package is **interfaces-only**, it avoids pulling in Hyperf’s heavy implementations, but **concrete implementations would still need to be provided** (either via Hyperf or custom Laravel classes).
### Technical Risk
- **Breaking Changes**: Hyperf’s contracts may evolve independently of Laravel’s, requiring **version pinning** and **backward-compatibility checks** to avoid runtime failures.
- **DI Container Conflicts**: Laravel’s **service container** and Hyperf’s **container** are incompatible. Resolving Hyperf contracts in Laravel would require **custom binding logic** or a **container adapter**, adding complexity.
- **Testing Overhead**: Mocking Hyperf contracts in Laravel tests may require **custom test doubles** or **framework-specific test utilities**, increasing maintenance burden.
- **Performance Indirection**: If used to wrap Laravel services, the **additional abstraction layer** could introduce **minor runtime overhead** (e.g., reflection, proxy calls).
### Key Questions
1. **Strategic Justification**
- Why adopt Hyperf contracts instead of Laravel’s existing contracts? What **specific problem** do they solve that Laravel’s contracts don’t?
- Is this part of a **larger Hyperf migration** or **microservices strategy**?
2. **Contract Overlap**
- Which Hyperf contracts **duplicate** Laravel’s contracts (e.g., `CacheInterface`, `QueueWorkerInterface`)? Should we **deprecate one** or **merge them**?
- Are there **Hyperf-specific contracts** (e.g., `OnRequest`, `CoroutineInterface`) that Laravel lacks and are **critical** to the project?
3. **Implementation Strategy**
- Will we **extend Laravel’s contracts** to match Hyperf’s, or **wrap Hyperf contracts** in Laravel-compatible facades?
- How will we handle **DI conflicts** between Laravel’s and Hyperf’s containers?
4. **Long-Term Maintenance**
- Who will **sync updates** between Hyperf’s contracts and Laravel’s implementations?
- What’s the **deprecation policy** if Hyperf contracts diverge from Laravel’s?
5. **Performance and Scalability**
- Are there **critical paths** where Hyperf’s contracts could introduce **unnecessary indirection**?
- How will this affect **cold starts** (e.g., in serverless environments)?
6. **Team Readiness**
- Does the team have experience with **interface-driven design** and **Hyperf’s patterns** (e.g., coroutines, RPC)?
- Will this require **additional training** or **documentation**?
---
## Integration Approach
### Stack Fit
- **Best Fit**:
- **Hybrid Laravel/Hyperf architectures** (e.g., Laravel monolith calling Hyperf microservices).
- **Plugin/extension systems** where Hyperf’s contracts provide **standardized interfaces** for third-party integrations.
- **Projects migrating from Laravel to Hyperf** (gradual adoption of Hyperf contracts).
- **Partial Fit**:
- **Modular Laravel monoliths** where contracts are used to define **internal module boundaries** (e.g., payment gateways, auth providers).
- **Poor Fit**:
- **Vanilla Laravel applications** without Hyperf integration, as it adds **unnecessary complexity** and **duplicates existing contracts**.
### Migration Path
1. **Assessment and Alignment**
- Audit existing Laravel contracts (`Illuminate\Contracts`) and identify **gaps** where Hyperf’s contracts could add value.
- Map Hyperf contracts to Laravel’s **1:1 where possible**; define **adapter interfaces** for mismatches.
- Decide on a **contract ownership strategy** (e.g., "We’ll use Hyperf’s `CacheInterface` for new projects but deprecate Laravel’s").
2. **Pilot Integration (Low-Risk Module)**
- Start with a **non-critical module** (e.g., logging, caching, or a plugin system).
- Implement **dual-contract support** (e.g., a class implements both `Hyperf\Contract\CacheInterface` and `Illuminate\Contracts\Cache\Store`).
- Use **feature flags** to toggle between old and new contract implementations.
3. **DI Container Integration**
- Create a **custom Laravel service provider** to bind Hyperf contracts to Laravel-compatible implementations.
- Example:
```php
$this->app->bind(
Hyperf\Contract\CacheInterface::class,
function ($app) {
return $app->make(Illuminate\Contracts\Cache\Store::class);
}
);
```
- Handle **conflicting dependencies** (e.g., PSR-15 middleware) with **priority resolvers**.
4. **Testing and Validation**
- Write **contract-specific tests** to ensure mocking and DI work as expected.
- Test **cross-framework interactions** (e.g., a Laravel service calling a Hyperf microservice via contracts).
- Validate **performance impact** in staging.
5. **Full Adoption (Phased Rollout)**
- Gradually replace **Laravel-specific implementations** with Hyperf-compatible ones where beneficial.
- Deprecate **redundant contracts** (e.g., keep only Hyperf’s `CacheInterface` if migrating to Hyperf).
- Update **documentation** and **developer guidelines** to reflect the new contract system.
### Compatibility
- **Dependency Conflicts**:
- Ensure no **version clashes** between Hyperf’s dependencies (e.g., `psr/*`, `react/*`) and Laravel’s.
- Use **Composer’s `conflict` or `replace` directives** to manage overlaps.
- **DI Container Bridge**:
- Laravel’s container **does not natively support Hyperf’s annotations** (e.g., `@Inject`). Use **manual binding** or a **custom container adapter**.
- For **Hyperf’s coroutine-based services**, consider a **synchronous wrapper** for Laravel compatibility.
- **Event System**:
- Hyperf’s event system differs from Laravel’s. If using event contracts (e.g., `Hyperf\Contract\OnMessage`), implement a **cross-framework event dispatcher**.
- **Middleware and Filters**:
- Hyperf’s `OnRequest`/`OnResponse` interfaces may not align with Laravel’s middleware stack. Use **adapters** to bridge them.
### Sequencing
| Phase | Task | Dependencies | Risks |
|--------|--------------------------------|----------------|--------|
| 1 | **Contract Audit** | Identify overlapping/unique contracts. | Misalignment with business needs. |
| 2 | **Adapter Design** | Define mapping between Hyperf and Laravel contracts. | Complexity in DI resolution. |
| 3 | **Pilot Module** | Integrate in a low-risk module (e.g., logging). | Limited impact if module is trivial. |
| 4 | **DI Integration** | Configure Laravel’s container to resolve Hyperf contracts. | Breaking changes in DI. |
| 5 | **Testing Framework** | Implement contract-specific tests and CI checks. | Incomplete test coverage. |
| 6 | **Documentation** | Update architecture docs and developer guides. | Outdated or unclear documentation. |
| 7 | **Phased Adoption** | Roll out to critical modules (e.g., auth, payments). | Disruption in high-traffic areas. |
---
## Operational Impact
### Maintenance
- **Pros**:
- **Reduced redundancy**: Avoid redefining contracts like `CacheInterface` or `QueueWorkerInterface`.
- **Easier refactoring**: Swapping implementations (e.g., switching from Laravel’s cache to Hyperf’s) becomes **contract-driven**.
- **Consistency**: Standardized interfaces across **Laravel and Hyperf modules**.
- **Cons**:
- **Dual maintenance**: If contracts diverge, **syncing changes** between Hyperf and Laravel becomes a burden.
- **Additional abstraction layers**: Debugging may require **jumping between frameworks**, complicating stack traces.
- **Tooling gaps**: IDE support (e.g., PHPStorm) may not recognize Hyperf contracts without **custom configurations**.
### Support
How can I help you explore Laravel packages today?