<?php
/**
 * Compiler Pass for Routing Factories
 *
 * @author Visual Media
 */

namespace VisualM\TagManagerBundle\DependencyInjection\Compiler;

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

class TaggableManagerCompilerPass implements CompilerPassInterface
{
    /**
     * {@inheritDoc}
     */
    public function process(ContainerBuilder $container)
    {
        if (!$container->hasDefinition('visualm.tag_manager.util.taggable_manager_registry')) {
            return;
        }

        $registry = $container->getDefinition(
            'visualm.tag_manager.util.taggable_manager_registry'
        );

        $taggedManagers = $container->findTaggedServiceIds(
            'visualm.taggable.manager'
        );
        
        foreach ($taggedManagers as $id => $tagAttributes) {
            foreach ($tagAttributes as $attributes) {
                $registry->addMethodCall(
                    'registerManager',
                    array($attributes["content_name"], new Reference($id))
                );
            }
        }
        
    }
}