VOOZH about

URL: https://deepwiki.com/hypervel/components/1.2-hyperf-integration-and-swoole-foundation

⇱ Hyperf Integration and Swoole Foundation | hypervel/components | DeepWiki


Loading...
Last indexed: 7 March 2026 (96fbab)
Menu

Hyperf Integration and Swoole Foundation

This document explains how Hypervel integrates with the Hyperf framework and leverages Swoole's coroutine capabilities. It covers the dual-provider architecture that combines Hyperf's ConfigProvider pattern with Laravel-style ServiceProvider classes, and describes coroutine safety considerations when building applications on this foundation.

For information about the application lifecycle and bootstrapping process, see Application Container and Lifecycle. For details about service provider registration, see Service Providers and Bootstrapping.


Architecture Overview

Hypervel builds on Hyperf, leveraging Swoole's coroutine runtime while providing Laravel-compatible APIs. The framework operates through a layered architecture where each Hypervel component provides both a ConfigProvider (Hyperf integration) and optional ServiceProvider (application-level registration).

Framework Stack and Component Integration


Sources: src/foundation/src/Application.php25-90 composer.json238-279 composer.json99-145


Container and Dependency Injection

Hypervel's Application class extends a custom Container implementation that wraps Hyperf's dependency injection container. This provides Laravel-compatible container methods while utilizing Hyperf's powerful DI capabilities.

Container Hierarchy and Initialization


The Application constructor at src/foundation/src/Application.php82-90 receives a base path and initializes the DI container through DefinitionSourceFactory:


Sources: src/foundation/src/Application.php82-95 src/container/composer.json22-26 composer.json238-270

Service Resolution and Aliasing

The Application registers extensive container aliases mapping PSR interfaces and Hyperf classes to Hypervel implementations. This enables developers to resolve services using multiple interface names:

Primary InterfaceAliasesImplementation
Psr\Container\ContainerInterfaceapp, Hyperf\Di\Container, Hypervel\Container\Container, Hypervel\Foundation\ApplicationApplication singleton
Hyperf\Contract\ConfigInterfaceconfig, Hypervel\Config\Contracts\RepositoryConfiguration repository
Psr\EventDispatcher\EventDispatcherInterfaceevents, Hypervel\Event\Contracts\DispatcherEvent dispatcher
Psr\Http\Message\ServerRequestInterfacerequest, Hyperf\HttpServer\Request, Hypervel\Http\Contracts\RequestContractCurrent request

Sources: src/foundation/src/Application.php546-664


The Dual-Provider System

Hypervel implements a dual-provider architecture that combines two registration patterns:

  1. ConfigProviders - Hyperf's native service registration mechanism
  2. ServiceProviders - Laravel-style service providers for application-level concerns

ConfigProvider Registration Flow


ConfigProviders are discovered from composer.json238-270:


Sources: composer.json238-270 src/foundation/composer.json60-67

ConfigProvider Structure

Each Hypervel component package includes a ConfigProvider class that returns configuration arrays:


Sources: src/foundation/composer.json60-67 src/router/composer.json46-49 src/cache/composer.json45-52

ServiceProvider Registration Flow


ServiceProviders are discovered from composer.json271-279:


The registration logic at src/foundation/src/Application.php309-345 follows a two-phase lifecycle with register() and boot() methods.

Sources: composer.json271-279 src/foundation/src/Application.php309-345 src/foundation/src/Application.php398-430

Integration Points

ConcernConfigProviderServiceProvider
Registration PhaseApplication constructionBootstrap phase
PurposeFramework-level service registrationApplication-level logic and bindings
Discoveryextra.hyperf.config in composer.jsonextra.hypervel.providers in composer.json
LifecycleLoaded once at container buildRegistered during bootstrap, booted after
DI IntegrationDirect definitions in containerUses container after construction
ExamplesCacheManager, Router, EventDispatcherNotificationServiceProvider, ViewServiceProvider

Sources: src/foundation/composer.json60-72 src/notifications/composer.json50-54 src/view/composer.json


Swoole Coroutine Foundation

Swoole provides a coroutine-based concurrency model that allows thousands of concurrent connections without traditional threading overhead. Hypervel builds on this foundation while maintaining Laravel-compatible APIs.

Coroutine Execution Model


Each worker process runs a Swoole event loop that spawns coroutines for incoming requests. The Hyperf\Context\Context class provides coroutine-local storage keyed by coroutine ID, ensuring each request's data remains isolated.

Sources: composer.json119-122 src/http/src/Request.php971-984

Context Isolation Pattern

Hyperf's Context class provides coroutine-local storage, preventing data leakage between concurrent requests in the same worker process. The Request class demonstrates proper usage.


Key Context Keys Used:

Context KeyPurposeSet/Accessed At
ServerRequestInterface::classStores PSR-7 requestsrc/http/src/Request.php974
'http.request.parsedData'Cached parsed datasrc/http/src/Request.php339-340
HyperfRequest::class . '.properties.requestUri'Cached request URIHyperfRequest parent
HyperfRequest::class . '.properties.pathInfo'Cached path infoHyperfRequest parent

Sources: src/http/src/Request.php971-984 src/http/src/Request.php334-358 tests/Http/RequestTest.php36-43

Request Context Retrieval with Error Handling

The Request::getRequest() method at src/http/src/Request.php971-984 demonstrates coroutine-safe context retrieval with proper error handling:


This pattern ensures that:

  1. The request is always retrieved from the current coroutine's context
  2. Missing context is caught with a descriptive error message
  3. Other TypeError exceptions are properly propagated

The merge() method at src/http/src/Request.php334-343 shows how to update context safely:


Sources: src/http/src/Request.php971-984 src/http/src/Request.php334-358


HTTP Layer Integration

Hypervel's HTTP layer extends Hyperf's HTTP server components while providing Laravel-compatible APIs.

Request Extension Pattern


The Request class adds Laravel-style convenience methods while maintaining PSR-7 compatibility:

Sources: src/http/src/Request.php31-48 src/http/src/Contracts/RequestContract.php18-442

Request Method Extensions

Method CategoryExamplesPurpose
Type Castingboolean(), integer(), float(), enum()Type-safe input retrieval
Collectionscollect(), only(), except()Fluent data manipulation
Validationfilled(), anyFilled(), whenFilled()Input presence checking
Routingroute(), routeIs(), fullUrlIs()Route information access
Content NegotiationwantsJson(), expectsJson(), accepts()Response type determination

Sources: src/http/src/Request.php74-196 src/http/src/Request.php446-547 src/http/src/Request.php694-735


Coroutine Safety Considerations

When building applications on Hypervel, developers must be aware of coroutine-specific patterns and potential pitfalls.

Coroutine Safety Patterns


Safe Patterns:

PatternImplementationExample Location
Context storageContext::set() / Context::get()src/http/src/Request.php974
Context overrideContext::override() with closuresrc/http/src/Request.php338-341
Container resolutionApplicationContext::getContainer()->get()src/http/src/Request.php871-872
Immutable dataValue objects passed by valueThroughout framework

Unsafe Patterns:

PatternWhy UnsafeImpact
Global variablesShared across coroutinesData leakage between requests
Static propertiesWorker-level singletonState persists across requests
Thread-local assumptionsPHP doesn't have thread-localCoroutines != threads
Mutable singletonsState shared in workerRace conditions

Sources: src/http/src/Request.php338-341 src/http/src/Request.php871-882 src/http/src/Request.php971-984

Context-Aware Request Resolution

The framework provides multiple ways to access the current request, all resolving through the coroutine-local context:


The Request class resolves its data from context at src/http/src/Request.php971-984:


The facade accessor is defined at src/support/src/Facades/Request.php146-150:


Sources: src/support/src/Facades/Request.php144-150 src/http/src/Request.php971-984 src/foundation/src/Application.php597-602


Performance Considerations

Swoole's coroutine model provides significant performance advantages over traditional PHP execution:

Concurrency Model Comparison

AspectTraditional PHP-FPMSwoole Coroutines
Process ModelProcess per requestShared worker processes
ConcurrencyLimited by process countThousands per worker
MemoryHigh (process overhead)Low (shared memory)
Startup CostBootstrap per requestBootstrap once per worker
I/O HandlingBlockingNon-blocking async
State IsolationProcess isolationCoroutine context isolation

Worker Process Architecture

Hyperf's server configuration determines worker process count and lifecycle:


Each worker process:

  • Bootstraps the application once at startup
  • Maintains the container and service registrations
  • Handles thousands of concurrent coroutines
  • Isolates state using context managers

Sources: composer.json120-122 composer.json42


Package Dependencies

Hypervel's dependency structure shows the integration layers:

Core Hyperf Dependencies

PackageVersionPurpose
hyperf/framework~3.1.0Core framework foundation
hyperf/di~3.1.0Dependency injection container
hyperf/http-server~3.1.0HTTP server implementation
hyperf/config~3.1.0Configuration management
hyperf/dispatcher~3.1.0Middleware dispatcher
hyperf/engine^2.10Swoole engine abstraction
hyperf/context~3.1.0Coroutine context management

Hypervel Layer Dependencies

PackagePurpose
hypervel/containerContainer extensions and contracts
hypervel/supportHelper functions and utilities
hypervel/httpHTTP layer extensions
hypervel/routerRouting and URL generation
hypervel/cacheCaching with Swoole support

Sources: composer.json99-145 src/foundation/composer.json22-48


Summary

Hypervel's integration with Hyperf and Swoole provides:

  1. Laravel-Compatible APIs - Familiar interfaces and patterns for Laravel developers
  2. High Performance - Swoole's coroutine-based concurrency for thousands of concurrent requests
  3. Dual-Provider System - Combining Hyperf's ConfigProviders with Laravel's ServiceProviders
  4. Coroutine Safety - Context-based isolation preventing state leakage between requests
  5. PSR Standards - Full PSR-7, PSR-11, and PSR-14 compliance
  6. Extensibility - Modular package architecture with 33+ replaceable components

The framework achieves compatibility and performance by carefully layering Laravel-style APIs over Hyperf's coroutine-safe infrastructure, using context managers to maintain request isolation while enabling massive concurrency within shared worker processes.

Sources: src/foundation/src/Application.php composer.json src/http/src/Request.php src/container/composer.json

Refresh this wiki

On this page