<?php

namespace AutoAdmin\Bundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class TwigPathsCompilerPass implements CompilerPassInterface
{
    const PATHS = [
        __DIR__.'/../../Content/Resources/views' => 'AutoAdminContent',
        __DIR__.'/../../Admin/Resources/views' => 'AutoAdminAdmin',
        __DIR__.'/../../BrickBuilder/Resources/views' => 'AutoAdminBrickBuilder',
        __DIR__.'/../../Menu/Resources/views' => 'AutoAdminMenu',
        __DIR__.'/../../Media/Resources/views' => 'AutoAdminMedia',
    ];

    public function process(ContainerBuilder $container)
    {
        if (!$container->hasDefinition('twig.loader.native_filesystem')) {
            return;
        }

        $loaderDefinition = $container->getDefinition('twig.loader.native_filesystem');

        foreach (self::PATHS as $path => $namespace) {
            // registers both the overridable (`@Namespace`) and non-overridable (`@!Namespace`) namespaces
            // for the same physical path, which the `twig.paths` config array can't express since it's
            $loaderDefinition->addMethodCall('addPath', [$path, $namespace]);
            $loaderDefinition->addMethodCall('addPath', [$path, '!'.$namespace]);
        }
    }
}
