# iserranodev/encrypt-bundle architecture

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

- Interactive diagram: https://gitdiagram.com/iserranodev/encrypt-bundle
- Repository: https://github.com/iserranodev/encrypt-bundle

## Overview

##### Main Components
- Bundle Core: Represented by the main class (ISerranoDevEncryptBundle) that hooks the bundle into Symfony.
- Configuration & Dependency Injection: Include the configuration classes (Configuration.php, ISerranoDevEncryptExtension.php) and YAML configuration file (services.yaml) that set up service definitions and environment-based settings.
- Encryption Service: The EncryptService (with its associated encryption and decryption methods) that carries out encryption, decryption, hashing, and reverse operations.
- Attributes: The custom PHP attributes (Encrypted and Hashed) located in src/Attribute, which are used to annotate Doctrine entity properties.
- Event Listeners: Components like EncryptListener and HashListener that intercept Doctrine events to automatically encrypt/decrypt or hash/unhash data as specified.
- Command-line Utility: The GenerateKeyCommand found in src/Command which generates an encryption key file based on configuration.
- Doctrine Integration: Elements under src/Doctrine (e.g., the EncryptAwareMigrationInterface and the custom migration factory), showing how the bundle integrates with Doctrine migrations to support encryption.
- External Configuration File Integration: The project also leverages external configuration files, such as the .env and YAML recipe files, to customize environment settings and bundle behavior.

##### Relationships and Interactions
- The bundle is integrated into a Symfony application via Composer and enabled in the bundles configuration.
- When a Doctrine entity with the #[Encrypted] or #[Hashed] attribute is processed, the associated event listeners (EncryptListener and HashListener) intercept lifecycle events to invoke the EncryptService.
- The EncryptService uses defined encryption keys and environment configurations (from .env and YAML files) to conduct encryption or decryption.
- During database migrations, the bundle supports migrations via an interface (EncryptAwareMigrationInterface) to ensure that data remains integrated with encryption operations.
- The command GenerateKeyCommand ties into the overall system by producing the necessary encryption key file usually stored in the “encryption/” directory.

##### Architectural Patterns and Design Principles
- Modular Design: Each concern (attributes, event listeners, service, configuration, migrations) is separated by its responsibility.
- Dependency Injection: Core services and configurations are injected using Symfony’s DI container.
- Event-Driven Processing: Automatic encryption and decryption handling is triggered via Doctrine ORM’s event listeners.
- Extensibility: The use of custom attributes and interfaces allows developers to easily integrate and extend bundle functionalities.

##### Relevant Technologies/Frameworks
- Symfony Framework: The bundle is built specifically for Symfony.
- Doctrine ORM: Integration with entity lifecycle events for automatic encryption/hashing.
- Composer: For dependency management.
- YAML and Environment Variable Configuration if adjustments are needed.
- Optionally, external libraries like “paragonie/halite” for cryptographic implementations (as mentioned in the README for encryption methods).

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Symfony Application"]:::appStyle
    EConfig["External Configuration (.env, YAML)"]:::extConfigStyle
    Config["Configuration & Dependency Injection"]:::configStyle
    BundleCore["ISerranoDevEncryptBundle"]:::bundleStyle
    EncryptService["Encryption Service"]:::serviceStyle
    Attributes["Attributes (Encrypted, Hashed)"]:::attributeStyle
    EventListeners["Event Listeners (EncryptListener, HashListener)"]:::listenerStyle
    KeyCommand["Command-line Utility (GenerateKeyCommand)"]:::commandStyle
    DoctrineIntegration["Doctrine Integration (Migrations)"]:::doctrineStyle

    %% Vertical Flow
    A --> Config
    EConfig --> Config
    Config --> BundleCore
    BundleCore --> EncryptService
    EncryptService --> DoctrineIntegration

    %% Side Branch for Attributes and Event Listeners
    Attributes --> EventListeners
    EventListeners --> EncryptService

    %% Side Branch for Command-line Utility
    KeyCommand --> EncryptService

    %% Click Events
    click A "https://github.com/iserranodev/encrypt-bundle/tree/main/ "
    click EConfig "https://github.com/iserranodev/encrypt-bundle/blob/main/recipe/main/.env"
    click EConfig "https://github.com/iserranodev/encrypt-bundle/blob/main/recipe/main/config/packages/i_serrano_dev_encrypt.yaml"
    click Config "https://github.com/iserranodev/encrypt-bundle/blob/main/src/DependencyInjection/Configuration.php"
    click Config "https://github.com/iserranodev/encrypt-bundle/blob/main/src/DependencyInjection/ISerranoDevEncryptExtension.php"
    click Config "https://github.com/iserranodev/encrypt-bundle/blob/main/src/Resources/config/services.yaml"
    click BundleCore "https://github.com/iserranodev/encrypt-bundle/blob/main/src/ISerranoDevEncryptBundle.php"
    click EncryptService "https://github.com/iserranodev/encrypt-bundle/blob/main/src/Service/EncryptService.php"
    click Attributes "https://github.com/iserranodev/encrypt-bundle/blob/main/src/Attribute/Encrypted.php"
    click Attributes "https://github.com/iserranodev/encrypt-bundle/blob/main/src/Attribute/Hashed.php"
    click EventListeners "https://github.com/iserranodev/encrypt-bundle/blob/main/src/EventListener/EncryptListener.php"
    click EventListeners "https://github.com/iserranodev/encrypt-bundle/blob/main/src/EventListener/HashListener.php"
    click KeyCommand "https://github.com/iserranodev/encrypt-bundle/blob/main/src/Command/GenerateKeyCommand.php"
    click DoctrineIntegration "https://github.com/iserranodev/encrypt-bundle/blob/main/src/Doctrine/Migrations/EncryptAwareMigrationFactory.php"
    click DoctrineIntegration "https://github.com/iserranodev/encrypt-bundle/blob/main/src/Interface/EncryptAwareMigrationInterface.php"

    %% Styles
    classDef appStyle fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef extConfigStyle fill:#d1ecf1,stroke:#333,stroke-width:2px;
    classDef configStyle fill:#d1e7dd,stroke:#333,stroke-width:2px;
    classDef bundleStyle fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef serviceStyle fill:#d1c4e9,stroke:#333,stroke-width:2px;
    classDef attributeStyle fill:#fff3cd,stroke:#333,stroke-width:2px;
    classDef listenerStyle fill:#e2e3e5,stroke:#333,stroke-width:2px;
    classDef commandStyle fill:#e6f7ff,stroke:#333,stroke-width:2px;
    classDef doctrineStyle fill:#fef9c3,stroke:#333,stroke-width:2px;
```
