<commit_before><?php

/*
 * This file is part of the Rollerworks RouteAutowiringBundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Rollerworks\Bundle\RouteAutowiringBundle\DependencyInjection\Compiler;

use Rollerworks\Bundle\RouteAutowiringBundle\RouteResource;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Routing\RouteCollectionBuilder;

/**
 * RouteAutowiringPass registers the all tagged route-resource definitions
 * as resolved RouteCollections for the RouteSlotLoader service.
 *
 * @author <NAME> <<EMAIL>>
 */
final class RouteAutowiringPass implements CompilerPassInterface
{
    const TAG_NAME = 'rollerworks_route_autowiring.route_resource';

    /**
     * You can modify the container here before it is dumped to PHP code.
     *
     * @param ContainerBuilder $container
     */
    public function process(ContainerBuilder $container)
    {
        if (!$container->has('rollerworks_route_autowiring.route_loader') || !$container->has('routing.loader')) {
            return;
        }

        $slotsServiceIds = $container->findTaggedServiceIds(self::TAG_NAME);
        $slotsToServiceIds = [];

        /** @var ParameterBag $parameterBag */
        $parameterBag = $container->getParameterBag();
        /** @var Definition[] $slotServices */
        $slotServices = [];

        foreach ($slotsServiceIds as $id => list($tag)) {
            $slot = $parameterBag->resolveString($tag['slot']);

            if (!isset($slotServices[$slot])) {
                $slotServiceId = 'rollerworks_route_autowiring.routing_slot.'.$slot;
                $slotServices[$slot] = $container->setDefinition($slotServiceId, $this->createCollectionBuilder());
                $slotsToServiceIds[$slot] = $slotServiceId;
            }

            /** @var RouteResource $resource */
            $resource = $container->get($id);
            $slotServices[$slot]->addMethodCall('import', [$resource->getResource(), '/', $resource->getType()]);
        }

        $container->getDefinition('rollerworks_route_autowiring.route_loader')->replaceArgument(1, $slotsToServiceIds);
    }

    private function createCollectionBuilder()
    {
        return (new Definition(RouteCollectionBuilder::class))
            ->setArguments([new Reference('rollerworks_route_autowiring.resource_loader')])
            // Route collections are loaded lazily to circumvent the container
            // circular dependency of on this collection.
            ->setPublic(true)
        ;
    }
}<commit_msg>bug #9 Remove resource after registration (sstok)<commit_after><?php

/*
 * This file is part of the Rollerworks RouteAutowiringBundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Rollerworks\Bundle\RouteAutowiringBundle\DependencyInjection\Compiler;

use Rollerworks\Bundle\RouteAutowiringBundle\RouteResource;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Routing\RouteCollectionBuilder;

/**
 * RouteAutowiringPass registers the all tagged route-resource definitions
 * as resolved RouteCollections for the RouteSlotLoader service.
 *
 * @author <NAME> <<EMAIL>>
 */
final class RouteAutowiringPass implements CompilerPassInterface
{
    const TAG_NAME = 'rollerworks_route_autowiring.route_resource';

    /**
     * You can modify the container here before it is dumped to PHP code.
     *
     * @param ContainerBuilder $container
     */
    public function process(ContainerBuilder $container)
    {
        if (!$container->has('rollerworks_route_autowiring.route_loader') || !$container->has('routing.loader')) {
            return;
        }

        $slotsServiceIds = $container->findTaggedServiceIds(self::TAG_NAME);
        $slotsToServiceIds = [];

        /** @var ParameterBag $parameterBag */
        $parameterBag = $container->getParameterBag();
        /** @var Definition[] $slotServices */
        $slotServices = [];

        foreach ($slotsServiceIds as $id => list($tag)) {
            $slot = $parameterBag->resolveString($tag['slot']);

            if (!isset($slotServices[$slot])) {
                $slotServiceId = 'rollerworks_route_autowiring.routing_slot.'.$slot;
                $slotServices[$slot] = $container->setDefinition($slotServiceId, $this->createCollectionBuilder());
                $slotsToServiceIds[$slot] = $slotServiceId;
            }

            /** @var RouteResource $resource */
            $resource = $container->get($id);
            $slotServices[$slot]->addMethodCall('import', [$resource->getResource(), '/', $resource->getType()]);
            $container->removeDefinition($id);
        }

        $container->getDefinition('rollerworks_route_autowiring.route_loader')->replaceArgument(1, $slotsToServiceIds);
    }

    private function createCollectionBuilder()
    {
        return (new Definition(RouteCollectionBuilder::class))
            ->setArguments([new Reference('rollerworks_route_autowiring.resource_loader')])
            // Route collections are loaded lazily to circumvent the container
            // circular dependency of on this collection.
            ->setPublic(true)
        ;
    }
}
{'fraction_non_alphanumeric': 0.11392620302669614, 'fraction_numerical': 0.0005101173269852066, 'mean_word_length': 3.4730038022813687, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4820451', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1503, 'n_words': 327}