philipphermes/symfony-layered-architecture

Provides a Twig compiler pass and routing loaders that enable support for a layered architecture.

Installs: 104

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/philipphermes/symfony-layered-architecture

1.0.1 2026-01-01 13:37 UTC

This package is auto-updated.

Last update: 2026-01-01 13:37:47 UTC


README

Provides a Twig compiler pass and routing loaders that enable support for a layered architecture.

CI PHP

Table of Contents

  1. Installation
    1. twig compiler pass
    2. services
    3. routes
    4. doctrine
    5. migrations
    6. install deptrac
    7. configure deptrac
  2. Code Quality
    1. deptrac
    2. phpstan
  3. Test
    1. phpunit

Installation

composer require philipphermes/symfony-layered-architecture

Register Twig Compiler Pass

<?php

namespace App;

use PhilippHermes\Symfony\LayeredArchitecture\Shared\Twig\TwigPathCompilerPass;
use PhilippHermes\Symfony\LayeredArchitecture\Shared\Twig\TwigPathRegistrator\Plugin\BackendTwigRegistratorPlugin;
use PhilippHermes\Symfony\LayeredArchitecture\Shared\Twig\TwigPathRegistrator\Plugin\FrontendTwigRegistratorPlugin;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    /**
     * @inheritDoc
     */
    protected function build(ContainerBuilder $container): void
    {
        $twigPathCompilerPass = new TwigPathCompilerPass([
            //TwigRegistratorPlugins
            new BackendTwigRegistratorPlugin(),
            new FrontendTwigRegistratorPlugin(),
            //you can add your own ones e.g. when you have an additional layer like for merchants
        ]);
    
        $container->addCompilerPass($twigPathCompilerPass);
    }
}

Update services.yaml

parameters:

services:
  _defaults:
    autowire: true
    autoconfigure: true

  App\:
    resource: '../src/'
    exclude:
      - '../src/DependencyInjection/'
      - '../src/Kernel.php'
      - '../src/Backend/.+/Persistence/Entity/'
      - '../src/Generated/'

  PhilippHermes\Symfony\LayeredArchitecture\Shared\Routing\ControllerLoader:
    arguments:
      $classLoader: '@routing.loader.attribute'
      $projectDir: '%kernel.project_dir%'
    tags:
      - { name: routing.loader }

Update routes.yaml

modular_controllers:
    resource: .
    type: modular

Update doctrine.yaml

doctrine:
    ...
    orm:
        validate_xml_mapping: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        identity_generation_preferences:
            Doctrine\DBAL\Platforms\PostgreSQLPlatform: identity
        auto_mapping: true
        mappings:
            App:
                type: attribute
                is_bundle: false
                dir: '%kernel.project_dir%/src/Backend'
                prefix: 'App\Backend'
                alias: Backend
    ...

Update doctrine_migrations.yaml

doctrine_migrations:
    migrations_paths:
        'DoctrineMigrations': '%kernel.project_dir%/src/Generated/Migrations'
    enable_profiler: false

install deptrac:

composer require --dev deptrac/deptrac

Add deptrac.yaml to your project root:

deptrac:
    paths:
        - ./src
    exclude_files:
        - '#.*test.*#i'
    layers:
        -   name: Backend_Business
            collectors:
                -   type: directory
                    value: src/Backend/.+/Business/.*
        -   name: Backend_Persistence
            collectors:
                -   type: directory
                    value: src/Backend/.+/Persistence/.*
        -   name: Backend_Communication
            collectors:
                -   type: directory
                    value: src/Backend/.+/Communication/.*
        -   name: Backend_Presentation
            collectors:
                -   type: directory
                    value: src/Backend/.+/Presentation/.*
        -   name: Frontend_Controller
            collectors:
                -   type: directory
                    value: src/Frontend/.+/Controller/.*
        -   name: Frontend_Theme
            collectors:
                -   type: directory
                    value: src/Frontend/.+/Theme/.*
        -   name: Client
            collectors:
                -   type: directory
                    value: src/Client/.+/*
        -   name: Service
            collectors:
                -   type: directory
                    value: src/Service/.+/*
        -   name: Shared
            collectors:
                -   type: directory
                    value: src/Shared/.+/*
        -   name: Generated
            collectors:
                -   type: directory
                    value: src/Generated/*
        -   name: Doctrine
            collectors:
                -   type: classLike
                    value: ^Doctrine\\.*
        -   name: Vendor
            collectors:
                -   type: classLike
                    value: ^(Symfony|Psr|Monolog|Ramsey|League)\\.* #need to find a better solution

    ruleset:
        Backend_Business:
            - Backend_Business
            - Backend_Persistence
            - Service
            - Shared
            - Generated
            - Vendor
        Backend_Persistence:
            - Backend_Persistence
            - Service
            - Shared
            - Generated
            - Doctrine
            - Vendor
        Backend_Communication:
            - Backend_Communication
            - Backend_Business
            - Service
            - Shared
            - Generated
            - Vendor
        Backend_Presentation:
            - Backend_Presentation
        Frontend_Controller:
            - Frontend_Controller
            - Client
            - Service
            - Shared
            - Generated
            - Vendor
        Frontend_Theme:
            - Frontend_Theme
        Client:
            - Backend_Business
            - Client
            - Service
            - Shared
            - Generated
            - Vendor
        Service:
            - Service
            - Shared
            - Generated
            - Vendor
        Shared:
            - Shared
            - Generated
            - Vendor
        Generated:
            - Generated
            - Vendor
            - Doctrine # need this because of migrations

Code Quality

Deptrac

vendor/bin/deptrac

Phpstan

vendor/bin/phpstan analyse --memory-limit=1G

Test

Phpunit

vendor/bin/phpunit

# With coverage
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html coverage-report