# muxinc/mux-php architecture

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

- Interactive diagram: https://gitdiagram.com/muxinc/mux-php
- Repository: https://github.com/muxinc/mux-php

## Overview

##### Project type and purpose
- This is an Open-Source PHP client library (SDK) that wraps the Mux REST APIs (Video & Data).
- It is generated by OpenAPI Generator and published to Packagist for use in PHP applications.

##### File structure
- Top-level “MuxPhp” directory: contains the runtime client code
- Api/ – one class per API endpoint group (e.g., AssetsApi, LiveStreamsApi)
- Models/ – data model classes (requests, responses, domain objects)
- Configuration.php, HeaderSelector.php, ObjectSerializer.php – core plumbing
- gen/ – OpenAPI Generator templates and config; used to (re)generate the SDK
- docs/ – Markdown documentation of each Api and Model
- examples/ – usage samples invoking the client against the real Mux service
- composer.json – declares dependencies (e.g., GuzzleHttp) and PSR-4 autoloading
- CI / tooling files (phpunit.xml, .github/workflows/ci.yaml, php-cs-fixer)

##### README for additional insights
- Confirms that the library issues HTTP requests via Guzzle, serializes models to/from JSON “data” envelopes, and requires user-supplied credentials.
- Recommends managing tokens in environment and initializing Api classes via Configuration.
- Points out that the code is generated from an OpenAPI spec (gen folder).

##### Main components and their interactions
a. Application (user code)
- Imports vendor/autoload.php, sets up Configuration (token, secret)
- Instantiates Api classes (e.g., new AssetsApi(new GuzzleHttp\Client(), $config))
- Builds request model objects (MuxPhp\Models\CreateAssetRequest)
- Calls API methods (→ MuxPhp\Api\*)
b. MuxPhp SDK
- Api layer – translates method calls into HTTP requests
- Model layer – request/response classes
- ObjectSerializer – converts between PHP objects and JSON payloads
- HeaderSelector – picks Accept/Content-Type headers
- Configuration – holds base URL, credentials, timeouts
- GuzzleHttp client – executes HTTP requests and returns responses
c. External Service
- Mux REST API endpoints (https://api.mux.com/video/v1, /data/v1, /system/v1)
d. Code-generation toolchain (gen folder)
- OpenAPI Generator templates → regenerate Api, Models, documentation

##### Architectural patterns and design principles
- Layered architecture: user application → SDK Api layer → HTTP client → external service
- Separation of concerns:
- Api classes handle endpoint grouping and URIs
- Serializer handles (de)serialization
- Code generation for consistency and easy updating against new API versions
- PSR-4 autoloading, Composer-based dependency management

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Application
    UserApp["User Application<br/>(PHP App)"]:::app
    Examples["Examples<br/>Video & Data"]:::app

    %% SDK Main Box
    subgraph "Mux PHP SDK"
        direction TB
        subgraph "API Layer"
            AssetsApi["AssetsApi"]:::sdk
            LiveStreamsApi["LiveStreamsApi"]:::sdk
            ApiException["ApiException"]:::sdk
        end
        subgraph "Model Layer"
            Models["Models"]:::sdk
        end
        subgraph "Core / Infrastructure"
            Configuration["Configuration"]:::sdk
            HeaderSelector["HeaderSelector"]:::sdk
            ObjectSerializer["ObjectSerializer"]:::sdk
        end
    end

    %% HTTP Client and External Service
    Guzzle["GuzzleHttp Client"]:::external
    MuxAPI["Mux REST API<br/>(/video/v1, /data/v1)"]:::service

    %% Code Generation Toolchain
    subgraph "Code Generation"
        direction TB
        GenTemplates["OpenAPI Generator Templates"]:::tool
        OpenAPIToolsConfig["openapitools.json"]:::tool
        GeneratorConfig["generator-config.json"]:::tool
        FilesList[".openapi-generator/FILES"]:::tool
        Versioning[".openapi-generator/VERSION"]:::tool
    end

    %% Documentation & Config
    DocsApi["Docs: API Reference"]:::docs
    DocsModel["Docs: Model Reference"]:::docs
    Composer["composer.json"]:::config
    CI["CI Workflow"]:::config
    PHPUnit["phpunit.xml.dist"]:::config

    %% Data Flow
    Examples --> UserApp
    UserApp -->|method call| AssetsApi
    AssetsApi -->|"serialize PHP object → JSON"| ObjectSerializer
    ObjectSerializer -->|build HTTP request| Guzzle
    Guzzle -->|HTTPS request| MuxAPI
    MuxAPI -->|JSON response| Guzzle
    Guzzle -->|deserialize JSON → PHP| ObjectSerializer
    ObjectSerializer -->|return models| AssetsApi
    AssetsApi -->|return data| UserApp

    %% Code Generation Flow
    GenTemplates --> AssetsApi
    GenTemplates --> Models
    GenTemplates --> Configuration

    OpenAPIToolsConfig --> GenTemplates
    GeneratorConfig --> GenTemplates
    FilesList --> GenTemplates
    Versioning --> GenTemplates

    %% Documentation Links
    DocsApi --> AssetsApi
    DocsModel --> Models

    %% Config Files Reference
    Composer --- UserApp
    CI --- UserApp
    PHPUnit --- UserApp

    %% Click Events
    click AssetsApi "https://github.com/muxinc/mux-php/blob/master/MuxPhp/Api/AssetsApi.php"
    click LiveStreamsApi "https://github.com/muxinc/mux-php/tree/master/MuxPhp/Api/"
    click Models "https://github.com/muxinc/mux-php/tree/master/MuxPhp/Models/"
    click Configuration "https://github.com/muxinc/mux-php/blob/master/MuxPhp/Configuration.php"
    click HeaderSelector "https://github.com/muxinc/mux-php/blob/master/MuxPhp/HeaderSelector.php"
    click ObjectSerializer "https://github.com/muxinc/mux-php/blob/master/MuxPhp/ObjectSerializer.php"
    click ApiException "https://github.com/muxinc/mux-php/blob/master/MuxPhp/ApiException.php"
    click GenTemplates "https://github.com/muxinc/mux-php/tree/master/gen/templates/"
    click OpenAPIToolsConfig "https://github.com/muxinc/mux-php/blob/master/gen/openapitools.json"
    click GeneratorConfig "https://github.com/muxinc/mux-php/blob/master/gen/generator-config.json"
    click FilesList "https://github.com/muxinc/mux-php/tree/master/.openapi-generator/FILES"
    click Versioning "https://github.com/muxinc/mux-php/tree/master/.openapi-generator/VERSION"
    click DocsApi "https://github.com/muxinc/mux-php/tree/master/docs/Api/"
    click DocsModel "https://github.com/muxinc/mux-php/tree/master/docs/Model/"
    click Examples "https://github.com/muxinc/mux-php/tree/master/examples/video/"
    click Examples "https://github.com/muxinc/mux-php/tree/master/examples/data/"
    click Composer "https://github.com/muxinc/mux-php/blob/master/composer.json"
    click CI "https://github.com/muxinc/mux-php/blob/master/.github/workflows/ci.yaml"
    click PHPUnit "https://github.com/muxinc/mux-php/blob/master/phpunit.xml.dist"

    %% Styles
    classDef app fill:#e0f7e9,stroke:#2e7d32
    classDef sdk fill:#e3f2fd,stroke:#1565c0
    classDef external fill:#fff3e0,stroke:#ef6c00
    classDef service fill:#ffebee,stroke:#c62828
    classDef tool fill:#f3e5f5,stroke:#6a1b9a
    classDef docs fill:#eceff1,stroke:#607d8b
    classDef config fill:#e1f5fe,stroke:#0277bd
```
