arkitect icon indicating copy to clipboard operation
arkitect copied to clipboard

Suggestion: support for modular architecture rules via auto-loaded files

Open pfazzi opened this issue 8 months ago • 0 comments

I recently implemented a simple pattern that allows us to split our architectural rules across multiple files, making our phparkitect.php setup easier to scale and maintain as our codebase grows.

Here’s a simplified version of what we’re doing:

<?php

declare(strict_types=1);

use Arkitect\ClassSet;
use Arkitect\CLI\Config;

// Auto-require all PHP files in the architecture folder
foreach (glob(__DIR__.'/architecture/*.php') as $file) {
    require_once $file;
}

return static function (Config $config): void {
    $classSet = ClassSet::fromDir(__DIR__.'/src');

    $allRules = [];

    foreach (get_defined_functions()['user'] as $fn) {
        if (str_ends_with($fn, 'architecturerules')) {
            foreach ($fn() as $rule) {
                $allRules[] = $rule;
            }
        }
    }

    $config->add($classSet, ...$allRules);
};

Each file inside the /architecture folder defines a single function (e.g. clockArchitectureRules, quotingArchitectureRules, etc.) that returns an iterable of rules.

I'm sharing this in case it could inspire a similar built-in feature, or if we'd be interested in documenting this pattern as a recommended practice.

Happy to hear your thoughts!

pfazzi avatar May 01 '25 08:05 pfazzi