Resonance PHP Framework

repository·master·Indexed 42 days ago

https://github.com/distantmagic/resonance

An asynchronous PHP framework built on Swoole for high-performance interoperability, messaging, and AI/ML service integration. It features support for WebSocket JSON-RPC, GraphQL schemas via PHP attributes, asynchronous tasks using SwooleFuture, and integration with Rubix ML for training and serving machine learning models.

Tokens
100.3K
Snippets
272
Records
332
Agent score
52%

What's inside Resonance

  1. Overview of the Resonance PHP Framework

    master
    Resonance is an opinionated PHP framework designed for building IO-intensive web applications, such as AI copilots, conversational applications, and services requiring real-time messaging (WebSocket, gRPC). It leverages asynchronous PHP (via Swoole) and integrates with tools like llama.cpp for LLM capabilities and Rubix ML for machine learning model hosting. The framework focuses on predictable performance by using stateless, read-only internals to avoid memory leaks and garbage collector surprises.
  2. Common use cases for Resonance

    master

    Resonance is optimized for several development scenarios:

    • Rapid Prototyping: Quickly validating product hypotheses and features.
    • SaaS Development: Creating and scaling Software as a Service applications.
    • High-Performance PHP: Building applications that require high performance and memory usage optimization.
    • Service Orchestration: Managing cross-service communication and asynchronous task management.
    • Codebase Management: Streamlining refactoring through standardized and readable development patterns.
    • AI Integration: Integrating applications with open source, self-hosted Large Language Models (LLMs).
  3. Use OAuth 2.0 with Resonance

    master
    Resonance provides built-in support for OAuth 2.0 by integrating the thephpleague/oauth2-server library. You can configure OAuth 2.0 integrations using PHP attributes, which allows for a declarative setup of authentication and authorization features. Resonance includes sensible configuration defaults and integrates with other framework features to streamline the implementation process.
  4. How Connection Pools work in Resonance

    master
    Resonance uses connection pools to manage database interactions efficiently. When the application starts, a pool of connections is automatically created. When a query is executed, a connection is borrowed from the pool. The Connection Pool handles rotating and reestablishing connections automatically. If a connection closes, Swoole reopens it in the background to ensure the pool never becomes depleted. To avoid manual borrowing and returning of connections, Resonance provides high-level wrappers like database-queries and database-entities.
  5. Use Extractors to get structured data from fuzzy input

    master

    Extractors are a feature in Resonance designed to map a user's fuzzy, unstructured input into strictly formatted data or to pull specific data points out of a text block.

    Important Compatibility Note: All extractors are tested using Mistral Instruct 7B. If you use a different LLM—particularly smaller models like Phi-2—you may experience inconsistent results or failures in following the required schema.

  6. Security and performance of AssetFileRegistry

    master

    The AssetFileRegistry provides built-in protection against Path Traversal attacks and optimizes performance through the following mechanisms:

    • Path Traversal Mitigation: The registry indexes all files from the directory defined by the DM_PUBLIC_ROOT constant. It only serves files explicitly found in this internal registry. If a requested path is not in the registry, it returns a 404, preventing attackers from accessing files outside the public root.
    • Performance: Because the file list is kept in memory, Resonance avoids expensive filesystem checks during every request.
    • Optimized Transfer: Once a file is validated, Resonance uses Swoole's $response->sendfile(...) method to transfer the file efficiently.
  7. Understand Resonance's core principles and design philosophy

    master

    Resonance is designed around the principle of Asynchronous PHP, treating PHP not just as a website generator, but as a communication hub between infrastructure services.

    Key design goals include:

    • Consistent speed of development: Maintaining productivity regardless of project scale.
    • Ease of maintenance: Ensuring long-term project health.
    • Ease of integration: Facilitating connections with external services.
    • Service-oriented architecture: Allowing developers to prototype features in PHP and later replace them with specialized services (written in other languages) via API calls without re-architecting the entire system.

    Note that performance and security are treated as baseline requirements rather than distinct features.

  8. Use Route Symbols for type-safe internal linking

    master

    routeSymbol is a unique name assigned to a route via the #[RespondsToHttp] attribute. Instead of using raw strings for URLs, Resonance encourages using an enum that implements HttpRouteSymbolInterface.

    This approach provides:

    1. Consistency validation: Prevents typos in internal links.
    2. Bootstrap checks: Allows the framework to verify that all referenced route symbols actually exist during the server bootstrap phase.

    Example implementation of a Route Symbol enum:

    namespace App;
    
    use Distantmagic//... (imports)
    
    enum HttpRouteSymbol implements HttpRouteSymbolInterface
    {
        use CastableEnumTrait;
        use NameableEnumTrait;
    
        case Homepage;
    }
    enum HttpRouteSymbol implements HttpRouteSymbolInterface
    {
        use CastableEnumTrait;
        use NameableEnumTrait;
    
        case Homepage;
    }