# sirmerdas/sparkle architecture

> Architecture diagram of the GitHub repository sirmerdas/sparkle, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sirmerdas/sparkle
- Repository: https://github.com/sirmerdas/sparkle

## Overview

##### Project Type and Purpose
- The project is a library (or potentially a framework component) that provides database management functionalities along with a query builder. This is evident from the “src/Database” structure containing both Manager and QueryBuilder components.
- It also defines Enums (for operators, join types, etc.), Exceptions, and Traits suggesting careful organization around error handling and reusable query logic.
- Although the README is minimal (# sparkle), the organization of code and composer configuration indicate a mature PHP tool intended for interacting with databases in a structured fashion.

##### File Structure
- Top-Level Components:
- .github directory: Contains workflows (for automated testing) indicating CI/CD integration.
- composer.json: Dependency management and autoloading for PHP.
- rector.php and phpunit.xml: Tools for refactoring and testing respectively.
- src Directory:
- Database directory: Contains Manager classes (e.g., Manager.php, PdoManager.php) that likely handle connection setup, running queries, etc.
- QueryBuilder subdirectory: Contains classes like Builder.php and QueryResult.php, showing that the project builds SQL queries and processes results.
- Enums: Contains predefined constants (ComparisonOperator, Database, JoinType) for standardized usage in database interactions.
- Exceptions: Custom exceptions (ConnectionException, SqlExecuteException) which will be thrown when database operations fail.
- Traits: Provides shareable behaviors for constructing and validating query components.
- tests Directory:
- Contains test cases targeting the QueryBuilder functionality, ensuring that generated queries meet the required conditions.

a. Main Components to Represent:
- Database Management Layer:
- Manager classes (Manager.php, PdoManager.php) for initiating and managing the database connections.
- Sparkle.php might be the main entry point or a utility that stitches the components together.
- Query Builder:
- The Builder class to construct SQL queries.
- QueryResult used for handling database responses.
- Enums and Traits:
- Enums to standardize operator types and join types.
- Traits that provide common query components and validation functions.
- Exceptions:
- ConnectionException and SqlExecuteException to highlight error handling mechanisms.
- Testing/CI:

b. Relationships and Interactions:
- How the Database Manager interacts with the Query Builder to construct and execute SQL queries.
- That the Query Builder relies on Enums and Traits to construct syntactically correct queries.
- A module for tests, emphasizing that the QueryBuilder’s functionality is validated through the tests.

c. Important Architectural Patterns/Design:
- Modular and layered architecture: Separate concerns into connection management, query building, error handling, and data processing.
- Use of Traits for reusable code and Enums for type safety.
- Emphasis on well-defined exceptions to provide clear error propagation.
- Automated testing integration (CI/CD pipelines via GitHub Actions).

d. Technologies, Frameworks, and Libraries:
- PHP as the primary language, leveraging Composer for dependency management.
- PHPUnit for testing and Rector for refactoring hints.
- GitHub Actions for workflow automation.

##### Elements
- Clear labels for each component (e.g., Database Manager, Query Builder, Enums, Traits, Exceptions, Testing).
– From the main “sparkle” entry point to the Database Manager.
– From the Manager to QueryBuilder.
– From QueryBuilder to QueryResult.
– From each component to the relevant Enum or Trait if applicable.
– Rectangles for major modules/services.
– Ovals or circles for utility components (like Enums and Traits).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Database Management Layer
    subgraph "Database Management Layer"
        Sparkle["Sparkle"]:::layer
        Manager["Manager"]:::layer
        PdoManager["PdoManager"]:::layer
    end

    %% Query Builder
    subgraph "Query Builder"
        Builder["Builder"]:::layer
        QueryResult["QueryResult"]:::layer
    end

    %% Utilities
    subgraph "Utilities"
        subgraph "Enums"
            ComparisonOperator["ComparisonOperator"]:::utility
            DatabaseEnum["Database"]:::utility
            JoinType["JoinType"]:::utility
        end
        subgraph "Traits"
            QueryComponents["QueryComponents"]:::utility
            QueryValidators["QueryValidators"]:::utility
        end
        subgraph "Exceptions"
            ConnectionException["ConnectionException"]:::error
            SqlExecuteException["SqlExecuteException"]:::error
        end
    end

    %% Testing/CI Integration
    subgraph "Testing/CI Integration"
        TestWorkflow["Test Workflow"]:::ci
        PHPUnitConfig["PHPUnit Config"]:::ci
        QueryComponentsTest["QueryComponentsTest"]:::ci
    end

    %% Connections between layers
    Sparkle -->|"initiates"| Manager
    Manager -->|"manages"| PdoManager
    Manager -->|"executesQuery"| Builder
    Builder -->|"buildsQuery"| QueryResult
    Builder -->|"usesEnum"| ComparisonOperator
    Builder -->|"usesEnum"| DatabaseEnum
    Builder -->|"usesEnum"| JoinType
    Builder -->|"usesTrait"| QueryComponents
    Builder -->|"usesTrait"| QueryValidators
    Manager -.->|"propagatesError"| ConnectionException
    Manager -.->|"propagatesError"| SqlExecuteException
    Builder -->|"validatedBy"| QueryComponentsTest
    TestWorkflow -->|"runs"| PHPUnitConfig
    PHPUnitConfig -->|"executes"| QueryComponentsTest

    %% Click Events
    click Sparkle "https://github.com/sirmerdas/sparkle/blob/master/src/Database/Sparkle.php"
    click Manager "https://github.com/sirmerdas/sparkle/blob/master/src/Database/Manager.php"
    click PdoManager "https://github.com/sirmerdas/sparkle/blob/master/src/Database/PdoManager.php"
    click Builder "https://github.com/sirmerdas/sparkle/blob/master/src/Database/QueryBuilder/Builder.php"
    click QueryResult "https://github.com/sirmerdas/sparkle/blob/master/src/Database/QueryBuilder/QueryResult.php"
    click ComparisonOperator "https://github.com/sirmerdas/sparkle/blob/master/src/Enums/ComparisonOperator.php"
    click DatabaseEnum "https://github.com/sirmerdas/sparkle/blob/master/src/Enums/Database.php"
    click JoinType "https://github.com/sirmerdas/sparkle/blob/master/src/Enums/JoinType.php"
    click ConnectionException "https://github.com/sirmerdas/sparkle/blob/master/src/Exceptions/ConnectionException.php"
    click SqlExecuteException "https://github.com/sirmerdas/sparkle/blob/master/src/Exceptions/SqlExecuteException.php"
    click QueryComponents "https://github.com/sirmerdas/sparkle/blob/master/src/Traits/QueryComponents.php"
    click QueryValidators "https://github.com/sirmerdas/sparkle/blob/master/src/Traits/QueryValidators.php"
    click TestWorkflow "https://github.com/sirmerdas/sparkle/blob/master/.github/workflows/test.yml"
    click PHPUnitConfig "https://github.com/sirmerdas/sparkle/blob/master/phpunit.xml"
    click QueryComponentsTest "https://github.com/sirmerdas/sparkle/blob/master/tests/QueryBuilder/QueryComponentsTest.php"

    %% Styles
    classDef layer fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef utility fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef error fill:#FFB6C1,stroke:#000,stroke-width:1px;
    classDef ci fill:#FFD700,stroke:#000,stroke-width:1px;
```
