# performingdigital/twig-components architecture

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

- Interactive diagram: https://gitdiagram.com/performingdigital/twig-components
- Repository: https://github.com/performingdigital/twig-components

## Overview

##### Main Components
- Configuration Module
- Represented by files like Configuration.php and Setup.php. This module is responsible for initializing the extension into the Twig environment. It includes methods for defining the templates path, extension names, and enabling features such as custom tags or global context.
- Extension Module
- Located in the src/Extension folder (e.g., ComponentExtension.php), this module registers the custom components with Twig. It acts as a bridge between the user’s Twig setup and the custom token and lexer rules.
- Token Parsing System
- In the src/TokenParser directory, there are separate token parsers for components (ComponentTokenParser.php), slots (SlotTokenParser.php), and properties/attributes (PropsTokenParser.php). These modules interpret the custom syntax (e.g., {% x:button %}…{% endx %} or <x-button … >).
- Lexer Functionality
- Found in the src/Lexer folder, the files (ComponentLexer.php and ComponentTagCompiler.php) are responsible for converting the raw Twig source into token streams and then further into syntax nodes tailored for component rendering.
- Node Representation
- Defined in the src/Node directory, Node classes such as ComponentNode.php and SlotNode.php represent the components and slots in the parsed syntax tree. They serve as intermediate representations before Twig compiles the template into executable code.
- View and Rendering Layer
- In the src/View directory, classes like Component, ComponentAttributeBag, and ComponentSlot encapsulate the logic for rendering the Twig component. This layer manages merging attributes, rendering slots, and dynamic adjustments to the component output.
- Testing and Examples
- The tests directory (with many .twig examples and PHP test classes) illustrates how components are used and ensures that the lexer, token parsers, and nodes are working as expected. Although not part of the runtime architecture, these files validate the system behavior.

##### Relationships and Data Flow
- At startup, the application (or framework, like Craft, Symfony, or October CMS) calls the Configuration module’s setup method passing in the Twig environment.
- The Configuration module adds the custom Extension to the Twig environment.
- Once set up, whenever a Twig template is compiled that contains component syntax, the custom Token Parsers (for components, slots, and properties) intercept the template tags.
- The Token Parsers work in coordination with the Lexer to convert these tags into specialized Node objects.
- These Node objects (e.g., ComponentNode and SlotNode) are then processed during Twig’s compilation phase.
- Finally, the View classes (Component, ComponentAttributeBag, ComponentSlot) take over during the rendering phase to output the final HTML, merging any provided attributes and processing slots.

##### Architectural Patterns
- Separation of Concerns: Each module (configuration, extension, token parsing, lexing, node creation, and rendering) has a clearly defined responsibility.
- Decorator/Extension Pattern: The package “decorates” the standard Twig environment by injecting additional functionality through the extension module.
- Pipeline/Data Flow: The sequential processing steps (configuration → token parsing → lexing → node conversion → rendering) define a clear pipeline for processing Twig components.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Main Components
    A["Configuration Module"]:::config
    B["Extension Module"]:::extension
    C["Twig Environment"]:::env

    %% Token Parsing System Subgraph
    subgraph "Token Parsing System"
        D["ComponentTokenParser"]:::token
        E["SlotTokenParser"]:::token
        F["PropsTokenParser"]:::token
    end

    %% Lexer Functionality Subgraph
    subgraph "Lexer Functionality"
        G["ComponentLexer"]:::lexer
        H["ComponentTagCompiler"]:::lexer
    end

    %% Node Representation Subgraph
    subgraph "Node Representation"
        I["ComponentNode"]:::node
        J["SlotNode"]:::node
    end

    %% View & Rendering Layer Subgraph
    subgraph "View & Rendering Layer"
        K["Component"]:::view
        L["ComponentAttributeBag"]:::view
        M["ComponentSlot"]:::view
    end

    %% Testing Component (Optional)
    N["Testing"]:::testing

    %% Connections
    A -->|"Setup&Registration"| B
    B -->|"RegisterWith"| C
    C -->|"ParseCustomTags"| D
    C -->|"ParseCustomTags"| E
    C -->|"ParseCustomTags"| F
    D -->|"TransformTokens"| G
    E -->|"TransformTokens"| G
    F -->|"TransformTokens"| G
    G -->|"GenerateNodes"| I
    H -->|"GenerateNodes"| J
    I -->|"RenderComponent"| K
    J -->|"RenderComponent"| K
    %% Testing validations (dashed connection)
    N ---|"Validates"| D
    N ---|"Validates"| I

    %% Click Events
    click A "https://github.com/performingdigital/twig-components/blob/master/src/Configuration.php"
    click A "https://github.com/performingdigital/twig-components/blob/master/src/Setup.php"
    click B "https://github.com/performingdigital/twig-components/blob/master/src/Extension/ComponentExtension.php"
    click D "https://github.com/performingdigital/twig-components/blob/master/src/TokenParser/ComponentTokenParser.php"
    click E "https://github.com/performingdigital/twig-components/blob/master/src/TokenParser/SlotTokenParser.php"
    click F "https://github.com/performingdigital/twig-components/blob/master/src/TokenParser/PropsTokenParser.php"
    click G "https://github.com/performingdigital/twig-components/blob/master/src/Lexer/ComponentLexer.php"
    click H "https://github.com/performingdigital/twig-components/blob/master/src/Lexer/ComponentTagCompiler.php"
    click I "https://github.com/performingdigital/twig-components/blob/master/src/Node/ComponentNode.php"
    click J "https://github.com/performingdigital/twig-components/blob/master/src/Node/SlotNode.php"
    click K "https://github.com/performingdigital/twig-components/blob/master/src/View/Component.php"
    click L "https://github.com/performingdigital/twig-components/blob/master/src/View/ComponentAttributeBag.php"
    click M "https://github.com/performingdigital/twig-components/blob/master/src/View/ComponentSlot.php"
    click N "https://github.com/performingdigital/twig-components/tree/master/tests"

    %% Styles
    classDef config fill:#ffadad,stroke:#333,stroke-width:2px
    classDef extension fill:#ffd6a5,stroke:#333,stroke-width:2px
    classDef env fill:#fdffb6,stroke:#333,stroke-width:2px
    classDef token fill:#caffbf,stroke:#333,stroke-width:2px
    classDef lexer fill:#9bf6ff,stroke:#333,stroke-width:2px
    classDef node fill:#a0c4ff,stroke:#333,stroke-width:2px
    classDef view fill:#bdb2ff,stroke:#333,stroke-width:2px
    classDef testing fill:#ffc6ff,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5
```
