|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony CMF package. |
| 5 | + * |
| 6 | + * (c) 2011-2014 Symfony CMF |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | +namespace Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\Compiler; |
| 14 | + |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 17 | +use Symfony\Component\DependencyInjection\Reference; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Daniel Leech <[email protected]> |
| 21 | + */ |
| 22 | +class AdapterPass implements CompilerPassInterface |
| 23 | +{ |
| 24 | + public function process(ContainerBuilder $container) |
| 25 | + { |
| 26 | + if (!$container->hasDefinition( |
| 27 | + 'cmf_routing_auto.auto_route_manager' |
| 28 | + )) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + $adapter = $container->getParameter('cmf_routing_auto.adapter_name'); |
| 33 | + $adapterId = null; |
| 34 | + $adapterNames = array(); |
| 35 | + $ids = $container->findTaggedServiceIds('cmf_routing_auto.adapter'); |
| 36 | + |
| 37 | + foreach ($ids as $id => $attributes) { |
| 38 | + if (!isset($attributes[0]['alias'])) { |
| 39 | + throw new \InvalidArgumentException(sprintf( |
| 40 | + 'No "name" specified for auto route adapter "%s"', |
| 41 | + $id |
| 42 | + )); |
| 43 | + } |
| 44 | + |
| 45 | + $alias = $attributes[0]['alias']; |
| 46 | + $adapterNames[] = $alias; |
| 47 | + if ($adapter === $alias) { |
| 48 | + $adapterId = $id; |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (null === $adapterId) { |
| 54 | + throw new \RuntimeException(sprintf( |
| 55 | + 'Could not find configured adapter "%s", available adapters: "%s"', |
| 56 | + $adapter, |
| 57 | + implode('", "', $adapterNames) |
| 58 | + )); |
| 59 | + } |
| 60 | + |
| 61 | + $managerDef = $container->getDefinition('cmf_routing_auto.auto_route_manager'); |
| 62 | + $container->setAlias('cmf_routing_auto.adapter', $adapterId); |
| 63 | + } |
| 64 | +} |
0 commit comments