# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "External Clients"
        Browser["Browser (Customer)"]
        AdminBrowser["Browser (Admin)"]
        MobileApp["Mobile App"]
        ExternalAPI["External API Client"]
    end

    subgraph "Shopware Platform"
        subgraph "Presentation Layer"
            Storefront["Storefront<br/>(Twig + JS)"]
            Administration["Administration<br/>(Vue.js SPA)"]
            StoreAPI["Store API"]
            AdminAPI["Admin API"]
        end

        subgraph "Application Layer"
            Controller["Controllers"]
            PageLoader["PageLoaders"]
            Service["Services"]
            MessageHandler["MessageHandlers"]
            Flow["Flow/Automation"]
        end

        subgraph "Domain Layer"
            subgraph "Core Domains"
                Checkout["Checkout<br/>(Cart, Order, Payment)"]
                Content["Content<br/>(Product, Category, CMS)"]
                SystemDomain["System<br/>(SalesChannel, User)"]
            end
            EntityDef["EntityDefinitions"]
            Rule["Rules"]
        end

        subgraph "Infrastructure Layer"
            DAL["Data Abstraction Layer"]
            Cache["Cache Adapter"]
            Filesystem["Filesystem Adapter"]
            Messenger["Message Queue"]
            EventDispatcher["Event Dispatcher"]
        end
    end

    subgraph "External Systems"
        MariaDB[(MariaDB)]
        OpenSearch[(OpenSearch)]
        Redis[(Valkey/Redis)]
        SMTP["SMTP Server"]
        PaymentProvider["Payment Provider"]
    end

    Browser --> Storefront
    AdminBrowser --> Administration
    MobileApp --> StoreAPI
    ExternalAPI --> AdminAPI

    Storefront --> Controller
    Administration --> AdminAPI
    StoreAPI --> Controller
    AdminAPI --> Controller

    Controller --> PageLoader
    Controller --> Service
    PageLoader --> Service
    Service --> MessageHandler
    Service --> Flow

    Service --> Checkout
    Service --> Content
    Service --> SystemDomain
    Checkout --> EntityDef
    Content --> EntityDef
    SystemDomain --> EntityDef
    EntityDef --> Rule

    EntityDef --> DAL
    Service --> Cache
    Service --> Filesystem
    Service --> Messenger
    Service --> EventDispatcher

    DAL --> MariaDB
    DAL --> OpenSearch
    Cache --> Redis
    Messenger --> MariaDB
    Service --> SMTP
    Checkout --> PaymentProvider
```

## バンドル構成図

```mermaid
graph TB
    subgraph "Shopware Bundles"
        Kernel["Kernel"]

        subgraph "Core Bundles"
            Framework["Framework Bundle"]
            System["System Bundle"]
            ContentBundle["Content Bundle"]
            CheckoutBundle["Checkout Bundle"]
            DevOps["DevOps Bundle"]
            Maintenance["Maintenance Bundle"]
            ServiceBundle["Service Bundle"]
        end

        subgraph "Feature Bundles"
            StorefrontBundle["Storefront Bundle"]
            AdminBundle["Administration Bundle"]
            ESBundle["Elasticsearch Bundle"]
            Profiling["Profiling Bundle"]
        end

        subgraph "Symfony Bundles"
            FrameworkBundle["FrameworkBundle"]
            MonologBundle["MonologBundle"]
            TwigBundle["TwigBundle"]
            DebugBundle["DebugBundle"]
        end
    end

    Kernel --> Framework
    Kernel --> FrameworkBundle
    Kernel --> MonologBundle
    Kernel --> TwigBundle

    Framework --> System
    Framework --> ContentBundle
    Framework --> CheckoutBundle

    StorefrontBundle --> Framework
    StorefrontBundle --> ContentBundle
    StorefrontBundle --> CheckoutBundle

    AdminBundle --> Framework
    ESBundle --> Framework
    ESBundle --> ContentBundle
```

## データフロー図 (Storefront)

```mermaid
sequenceDiagram
    participant Browser
    participant Kernel
    participant Router
    participant ContextResolver
    participant Controller
    participant PageLoader
    participant DAL
    participant Database
    participant Twig

    Browser->>Kernel: HTTP Request
    Kernel->>Router: Route Matching
    Router->>ContextResolver: Resolve SalesChannel
    ContextResolver->>Kernel: SalesChannelContext
    Kernel->>Controller: Dispatch Request
    Controller->>PageLoader: Load Page Data
    PageLoader->>DAL: Query Entities
    DAL->>Database: SQL Query
    Database-->>DAL: Result Set
    DAL-->>PageLoader: Entity Collection
    PageLoader-->>Controller: Page Struct
    Controller->>Twig: Render Template
    Twig-->>Controller: HTML Response
    Controller-->>Browser: HTTP Response
```

## データフロー図 (Store API)

```mermaid
sequenceDiagram
    participant Client
    participant Kernel
    participant AuthMiddleware
    participant ContextResolver
    participant Controller
    participant Service
    participant DAL
    participant EventDispatcher
    participant Cache

    Client->>Kernel: HTTP Request + sw-access-key
    Kernel->>AuthMiddleware: Validate Access Key
    AuthMiddleware->>ContextResolver: Resolve Context Token
    ContextResolver->>Kernel: SalesChannelContext
    Kernel->>Controller: Dispatch Request
    Controller->>Cache: Check Cache
    alt Cache Hit
        Cache-->>Controller: Cached Response
    else Cache Miss
        Controller->>Service: Execute Business Logic
        Service->>DAL: CRUD Operations
        DAL->>EventDispatcher: Entity Events
        Service-->>Controller: Result
        Controller->>Cache: Store in Cache
    end
    Controller-->>Client: JSON Response
```

## 非同期処理フロー図

```mermaid
sequenceDiagram
    participant Application
    participant Messenger
    participant Transport
    participant Worker
    participant Handler
    participant Service

    Application->>Messenger: dispatch(Message)
    Messenger->>Transport: Send to Queue
    Transport-->>Messenger: Ack
    Messenger-->>Application: Envelope

    Note over Worker: Background Process
    Worker->>Transport: Consume Message
    Transport-->>Worker: Message
    Worker->>Handler: Handle Message
    Handler->>Service: Execute Logic
    Service-->>Handler: Result
    Handler-->>Worker: Success/Failure
    Worker->>Transport: Ack/Nack
```

## レイヤー依存関係図

```mermaid
graph TB
    subgraph "Presentation"
        StorefrontCtrl["Storefront Controllers"]
        AdminAPI["Admin API Controllers"]
        StoreAPI["Store API Controllers"]
    end

    subgraph "Application"
        Services["Services"]
        PageLoaders["Page Loaders"]
        MessageHandlers["Message Handlers"]
        FlowDispatching["Flow Dispatching"]
    end

    subgraph "Domain"
        Entities["Entity Definitions"]
        Rules["Business Rules"]
        Events["Domain Events"]
    end

    subgraph "Infrastructure"
        Repositories["Entity Repositories"]
        Adapters["Adapters (Cache, FS, DB)"]
        ExternalGateway["External Gateways"]
    end

    StorefrontCtrl --> PageLoaders
    StorefrontCtrl --> Services
    AdminAPI --> Services
    StoreAPI --> Services

    PageLoaders --> Repositories
    Services --> Repositories
    Services --> FlowDispatching
    MessageHandlers --> Services

    FlowDispatching --> Rules
    Services --> Entities
    Repositories --> Entities

    Entities --> Events
    Repositories --> Adapters
    Services --> ExternalGateway
```

## モジュール依存関係図

```mermaid
graph LR
    subgraph "Core"
        Framework
        System
        Content
        Checkout
        Service
    end

    subgraph "Features"
        Storefront
        Administration
        Elasticsearch
    end

    subgraph "Extensions"
        Plugins["Plugins"]
        Apps["Apps"]
    end

    Framework --> System
    Framework --> Content
    System --> Framework
    Content --> Framework
    Checkout --> Framework
    Checkout --> Content
    Checkout --> System
    Service --> Framework

    Storefront --> Framework
    Storefront --> Content
    Storefront --> Checkout
    Storefront --> System

    Administration --> Framework

    Elasticsearch --> Framework
    Elasticsearch --> Content

    Plugins --> Framework
    Apps --> Framework
```

## DAL (Data Abstraction Layer) 構造図

```mermaid
classDiagram
    class EntityRepository {
        +search(Criteria, Context)
        +create(data, Context)
        +update(data, Context)
        +delete(ids, Context)
        +clone(id, Context)
    }

    class EntityDefinition {
        +getEntityName()
        +getFields()
        +getCollectionClass()
    }

    class Entity {
        +id: string
        +getUniqueIdentifier()
    }

    class EntityCollection {
        +getElements()
        +add(Entity)
    }

    class Criteria {
        +addFilter(Filter)
        +addSorting(Sorting)
        +addAssociation(string)
        +setLimit(int)
    }

    class VersionManager {
        +insert()
        +update()
        +delete()
        +createVersion()
        +merge()
    }

    EntityRepository --> EntityDefinition
    EntityRepository --> VersionManager
    EntityRepository --> Criteria
    EntityDefinition --> Entity
    EntityCollection --> Entity
    EntityRepository --> EntityCollection
```

## 認証・認可フロー図

```mermaid
graph TB
    subgraph "Admin API Authentication"
        AdminReq["Admin Request"]
        OAuth["OAuth2 Server"]
        Bearer["Bearer Token Validation"]
        ACL["ACL Check"]
        AdminAccess["Granted Access"]
    end

    subgraph "Store API Authentication"
        StoreReq["Store API Request"]
        AccessKey["Access Key Validation"]
        ContextToken["Context Token Resolution"]
        CustomerAuth["Customer Authentication (Optional)"]
        StoreAccess["Granted Access"]
    end

    AdminReq --> OAuth
    OAuth --> Bearer
    Bearer --> ACL
    ACL --> AdminAccess

    StoreReq --> AccessKey
    AccessKey --> ContextToken
    ContextToken --> CustomerAuth
    CustomerAuth --> StoreAccess
```
