# アーキテクチャ構成図

## システム全体概要図

```mermaid
flowchart TB
    subgraph Client["Client Layer"]
        Browser[Web Browser]
        Mobile[Mobile App]
        CLI[CLI / Artisan]
    end

    subgraph Laravel["Laravel Framework"]
        subgraph Entry["Entry Points"]
            PublicIndex[public/index.php]
            Artisan[artisan]
        end

        subgraph Kernel["Kernel Layer"]
            HttpKernel[HTTP Kernel]
            ConsoleKernel[Console Kernel]
        end

        subgraph Core["Core Components"]
            Application[Application Container]
            ServiceProviders[Service Providers]
            Facades[Facades]
        end

        subgraph Web["Web Layer"]
            Router[Router]
            Middleware[Middleware Stack]
            Controllers[Controllers]
        end

        subgraph Business["Business Layer"]
            Models[Eloquent Models]
            Events[Events / Listeners]
            Jobs[Queue Jobs]
            Notifications[Notifications]
            Mail[Mail]
        end

        subgraph Data["Data Layer"]
            QueryBuilder[Query Builder]
            Eloquent[Eloquent ORM]
            Cache[Cache Manager]
            Session[Session Manager]
        end
    end

    subgraph External["External Services"]
        Database[(Database)]
        Redis[(Redis)]
        Queue[(Message Queue)]
        Storage[(File Storage)]
        MailServer[Mail Server]
    end

    Browser --> PublicIndex
    Mobile --> PublicIndex
    CLI --> Artisan

    PublicIndex --> HttpKernel
    Artisan --> ConsoleKernel

    HttpKernel --> Application
    ConsoleKernel --> Application

    Application --> ServiceProviders
    ServiceProviders --> Facades

    HttpKernel --> Middleware
    Middleware --> Router
    Router --> Controllers

    Controllers --> Models
    Controllers --> Events
    Controllers --> Jobs
    Controllers --> Notifications
    Controllers --> Mail

    Models --> Eloquent
    Eloquent --> QueryBuilder
    QueryBuilder --> Database

    Cache --> Redis
    Session --> Redis
    Jobs --> Queue
    Mail --> MailServer
    Models --> Storage
```

## サービスコンテナとサービスプロバイダ

```mermaid
flowchart LR
    subgraph Container["Service Container (IoC)"]
        Bindings[Bindings Registry]
        Instances[Singleton Instances]
        Aliases[Aliases]
    end

    subgraph Providers["Service Providers"]
        EventSP[EventServiceProvider]
        RoutingSP[RoutingServiceProvider]
        DatabaseSP[DatabaseServiceProvider]
        CacheSP[CacheServiceProvider]
        QueueSP[QueueServiceProvider]
        MailSP[MailServiceProvider]
        AuthSP[AuthServiceProvider]
        LogSP[LogServiceProvider]
        CustomSP[Custom Providers...]
    end

    subgraph Services["Resolved Services"]
        EventDispatcher[Event Dispatcher]
        RouterService[Router]
        DBManager[Database Manager]
        CacheManager[Cache Manager]
        QueueManager[Queue Manager]
        Mailer[Mailer]
        AuthManager[Auth Manager]
        Logger[Logger]
    end

    EventSP -->|register| Container
    RoutingSP -->|register| Container
    DatabaseSP -->|register| Container
    CacheSP -->|register| Container
    QueueSP -->|register| Container
    MailSP -->|register| Container
    AuthSP -->|register| Container
    LogSP -->|register| Container
    CustomSP -->|register| Container

    Container -->|resolve| EventDispatcher
    Container -->|resolve| RouterService
    Container -->|resolve| DBManager
    Container -->|resolve| CacheManager
    Container -->|resolve| QueueManager
    Container -->|resolve| Mailer
    Container -->|resolve| AuthManager
    Container -->|resolve| Logger
```

## HTTPリクエスト処理フロー

```mermaid
sequenceDiagram
    participant Client
    participant Index as public/index.php
    participant Kernel as HTTP Kernel
    participant Bootstrap
    participant Pipeline as Middleware Pipeline
    participant Router
    participant Controller
    participant Model as Eloquent Model
    participant DB as Database

    Client->>Index: HTTP Request
    Index->>Kernel: handle(Request)

    Kernel->>Bootstrap: bootstrap()
    activate Bootstrap
    Bootstrap->>Bootstrap: LoadEnvironmentVariables
    Bootstrap->>Bootstrap: LoadConfiguration
    Bootstrap->>Bootstrap: HandleExceptions
    Bootstrap->>Bootstrap: RegisterFacades
    Bootstrap->>Bootstrap: RegisterProviders
    Bootstrap->>Bootstrap: BootProviders
    deactivate Bootstrap

    Kernel->>Pipeline: sendThroughPipeline()
    activate Pipeline
    Pipeline->>Pipeline: Global Middleware
    Pipeline->>Router: dispatch()

    Router->>Router: findRoute()
    Router->>Pipeline: Route Middleware
    Pipeline->>Controller: callAction()
    deactivate Pipeline

    Controller->>Model: query()
    Model->>DB: SQL Query
    DB-->>Model: Results
    Model-->>Controller: Collection/Model

    Controller-->>Router: Response
    Router-->>Kernel: Response
    Kernel->>Kernel: terminate()
    Kernel-->>Index: Response
    Index-->>Client: HTTP Response
```

## コンポーネント間依存関係

```mermaid
flowchart TB
    subgraph Foundation
        Application
        HttpKernel[Http Kernel]
        ConsoleKernel[Console Kernel]
    end

    subgraph Container
        ContainerClass[Container]
        BoundMethod
    end

    subgraph Contracts
        ContainerInterface[Container Interface]
        ApplicationInterface[Application Interface]
        KernelInterface[Kernel Interface]
    end

    subgraph Support
        ServiceProvider
        Facade
        Collection
        Helpers
    end

    subgraph Http
        Request
        Response
        HttpMiddleware[Middleware]
    end

    subgraph Routing
        Router
        Route
        UrlGenerator
    end

    subgraph Database
        DatabaseManager
        Connection
        QueryBuilder[Query Builder]
    end

    subgraph Eloquent
        Model
        EloquentBuilder[Builder]
        Relations
    end

    Application -->|extends| ContainerClass
    Application -->|implements| ApplicationInterface
    HttpKernel -->|implements| KernelInterface
    ContainerClass -->|implements| ContainerInterface

    HttpKernel --> Application
    HttpKernel --> Router
    HttpKernel --> HttpMiddleware

    Router --> Route
    Router --> UrlGenerator

    Eloquent --> Database
    Model --> EloquentBuilder
    EloquentBuilder --> QueryBuilder
    QueryBuilder --> Connection
    Connection --> DatabaseManager

    ServiceProvider --> Application
    Facade --> Application
```

## ミドルウェアパイプライン

```mermaid
flowchart LR
    Request[HTTP Request]

    subgraph GlobalMiddleware["Global Middleware"]
        TrustProxies
        HandleCors
        PreventMaintenance[Prevent Maintenance]
        ValidatePostSize
        TrimStrings
        ConvertEmptyStrings[Convert Empty Strings]
    end

    subgraph RouteMiddleware["Route Middleware"]
        EncryptCookies
        AddQueuedCookies[Add Queued Cookies]
        StartSession
        ShareErrors[Share Errors from Session]
        VerifyCsrf[Verify CSRF Token]
        SubstituteBindings
        Throttle
        Authenticate
        Authorize
    end

    Controller[Controller Action]
    Response[HTTP Response]

    Request --> TrustProxies
    TrustProxies --> HandleCors
    HandleCors --> PreventMaintenance
    PreventMaintenance --> ValidatePostSize
    ValidatePostSize --> TrimStrings
    TrimStrings --> ConvertEmptyStrings

    ConvertEmptyStrings --> EncryptCookies
    EncryptCookies --> AddQueuedCookies
    AddQueuedCookies --> StartSession
    StartSession --> ShareErrors
    ShareErrors --> VerifyCsrf
    VerifyCsrf --> SubstituteBindings
    SubstituteBindings --> Throttle
    Throttle --> Authenticate
    Authenticate --> Authorize

    Authorize --> Controller
    Controller --> Response
```

## キュー処理フロー

```mermaid
sequenceDiagram
    participant App as Application
    participant Bus as Bus Dispatcher
    participant Queue as Queue Manager
    participant Store as Queue Store
    participant Worker as Queue Worker
    participant Job

    App->>Bus: dispatch(Job)
    Bus->>Queue: push(Job)
    Queue->>Store: serialize & store

    loop Worker Daemon
        Worker->>Store: pop()
        Store-->>Worker: Serialized Job
        Worker->>Job: fire()

        alt Success
            Job-->>Worker: Complete
            Worker->>Store: delete()
        else Failure
            Job-->>Worker: Exception
            Worker->>Store: release() / fail()
        end
    end
```

## データベース接続アーキテクチャ

```mermaid
flowchart TB
    subgraph Application
        EloquentModel[Eloquent Model]
        EloquentBuilder[Eloquent Builder]
        QueryBuilder[Query Builder]
    end

    subgraph DatabaseLayer["Database Layer"]
        DatabaseManager[Database Manager]

        subgraph Connections
            SQLiteConn[SQLite Connection]
            MySQLConn[MySQL Connection]
            PostgresConn[PostgreSQL Connection]
            SQLServerConn[SQL Server Connection]
            MariaDBConn[MariaDB Connection]
        end

        subgraph Connectors
            SQLiteConnector[SQLite Connector]
            MySQLConnector[MySQL Connector]
            PostgresConnector[PostgreSQL Connector]
            SQLServerConnector[SQL Server Connector]
        end
    end

    subgraph Databases["Databases"]
        SQLite[(SQLite)]
        MySQL[(MySQL)]
        PostgreSQL[(PostgreSQL)]
        SQLServer[(SQL Server)]
        MariaDB[(MariaDB)]
    end

    EloquentModel --> EloquentBuilder
    EloquentBuilder --> QueryBuilder
    QueryBuilder --> DatabaseManager

    DatabaseManager --> SQLiteConn
    DatabaseManager --> MySQLConn
    DatabaseManager --> PostgresConn
    DatabaseManager --> SQLServerConn
    DatabaseManager --> MariaDBConn

    SQLiteConn --> SQLiteConnector
    MySQLConn --> MySQLConnector
    PostgresConn --> PostgresConnector
    SQLServerConn --> SQLServerConnector
    MariaDBConn --> MySQLConnector

    SQLiteConnector --> SQLite
    MySQLConnector --> MySQL
    PostgresConnector --> PostgreSQL
    SQLServerConnector --> SQLServer
    MySQLConnector --> MariaDB
```

## キャッシュ・セッション構成

```mermaid
flowchart TB
    subgraph Managers["Manager Classes"]
        CacheManager[Cache Manager]
        SessionManager[Session Manager]
    end

    subgraph Stores["Cache Stores"]
        ArrayStore[Array Store]
        FileStore[File Store]
        DatabaseStore[Database Store]
        RedisStore[Redis Store]
        MemcachedStore[Memcached Store]
        DynamoDBStore[DynamoDB Store]
    end

    subgraph SessionHandlers["Session Handlers"]
        FileHandler[File Handler]
        DatabaseHandler[Database Handler]
        CacheHandler[Cache Handler]
        CookieHandler[Cookie Handler]
    end

    subgraph Backend["Backend Storage"]
        FileSystem[(File System)]
        DB[(Database)]
        Redis[(Redis)]
        Memcached[(Memcached)]
        DynamoDB[(DynamoDB)]
    end

    CacheManager --> ArrayStore
    CacheManager --> FileStore
    CacheManager --> DatabaseStore
    CacheManager --> RedisStore
    CacheManager --> MemcachedStore
    CacheManager --> DynamoDBStore

    SessionManager --> FileHandler
    SessionManager --> DatabaseHandler
    SessionManager --> CacheHandler
    SessionManager --> CookieHandler

    FileStore --> FileSystem
    DatabaseStore --> DB
    RedisStore --> Redis
    MemcachedStore --> Memcached
    DynamoDBStore --> DynamoDB

    FileHandler --> FileSystem
    DatabaseHandler --> DB
    CacheHandler --> RedisStore
```

## イベントシステム

```mermaid
flowchart LR
    subgraph EventSources["Event Sources"]
        ModelEvents[Model Events]
        AuthEvents[Auth Events]
        QueueEvents[Queue Events]
        CustomEvents[Custom Events]
    end

    subgraph Dispatcher["Event Dispatcher"]
        Listen[listen()]
        Dispatch[dispatch()]
        Subscribe[subscribe()]
    end

    subgraph Handlers["Event Handlers"]
        Listeners[Listeners]
        Subscribers[Subscribers]
        Closures[Closures]
        QueuedListeners[Queued Listeners]
    end

    subgraph Actions["Resulting Actions"]
        Notification[Send Notification]
        Logging[Write Log]
        Cache[Update Cache]
        Broadcast[Broadcast]
        Custom[Custom Logic]
    end

    ModelEvents --> Dispatch
    AuthEvents --> Dispatch
    QueueEvents --> Dispatch
    CustomEvents --> Dispatch

    Listen --> Listeners
    Listen --> Closures
    Subscribe --> Subscribers

    Dispatch --> Listeners
    Dispatch --> Subscribers
    Dispatch --> Closures
    Dispatch --> QueuedListeners

    Listeners --> Notification
    Listeners --> Logging
    Subscribers --> Cache
    QueuedListeners --> Broadcast
    Closures --> Custom
```

## 認証・認可フロー

```mermaid
flowchart TB
    subgraph Request["Incoming Request"]
        Credentials[Credentials]
        Token[API Token]
        SessionCookie[Session Cookie]
    end

    subgraph AuthManager["Auth Manager"]
        Guard[Guard]
        SessionGuard[Session Guard]
        TokenGuard[Token Guard]
    end

    subgraph UserProvider["User Provider"]
        EloquentProvider[Eloquent Provider]
        DatabaseProvider[Database Provider]
    end

    subgraph Authorization["Authorization"]
        Gate[Gate]
        Policies[Policies]
        Abilities[Abilities]
    end

    subgraph User["Authenticated User"]
        UserModel[User Model]
    end

    Credentials --> SessionGuard
    Token --> TokenGuard
    SessionCookie --> SessionGuard

    Guard --> SessionGuard
    Guard --> TokenGuard

    SessionGuard --> EloquentProvider
    TokenGuard --> EloquentProvider
    SessionGuard --> DatabaseProvider

    EloquentProvider --> UserModel
    DatabaseProvider --> UserModel

    UserModel --> Gate
    Gate --> Policies
    Gate --> Abilities
```

## パッケージ構成図

```mermaid
flowchart TB
    subgraph illuminate["illuminate/* packages"]
        subgraph core["Core"]
            container[container]
            contracts[contracts]
            support[support]
        end

        subgraph foundation["Foundation"]
            foundationPkg[foundation]
            config[config]
        end

        subgraph web["Web"]
            http[http]
            routing[routing]
            session[session]
            cookie[cookie]
            view[view]
        end

        subgraph data["Data"]
            database[database]
            cache[cache]
            redis[redis]
        end

        subgraph auth["Auth"]
            authPkg[auth]
            hashing[hashing]
            encryption[encryption]
        end

        subgraph async["Async"]
            queue[queue]
            bus[bus]
            events[events]
            broadcasting[broadcasting]
        end

        subgraph comm["Communication"]
            mail[mail]
            notifications[notifications]
        end

        subgraph cli["CLI"]
            console[console]
            process[process]
        end

        subgraph utils["Utilities"]
            validation[validation]
            translation[translation]
            pagination[pagination]
            filesystem[filesystem]
            log[log]
            collections[collections]
            pipeline[pipeline]
        end
    end

    foundationPkg --> container
    foundationPkg --> contracts
    foundationPkg --> support

    http --> routing
    http --> session
    routing --> container

    database --> container
    database --> events
    cache --> contracts

    authPkg --> session
    authPkg --> hashing

    queue --> bus
    queue --> events
    broadcasting --> events

    mail --> view
    notifications --> mail
    notifications --> queue

    console --> container
    console --> events
```
