Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ae03be

Browse files
committed
[FrameworkBundle] Support autowiring for RouterInterface
1 parent 84229f8 commit 6ae03be

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17+
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
18+
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
19+
use Symfony\Component\Routing\RouterInterface;
20+
21+
class RoutingPass implements CompilerPassInterface
22+
{
23+
public function process(ContainerBuilder $container)
24+
{
25+
if ($container->hasAlias('router')) {
26+
$definition = $container->findDefinition('router');
27+
$definition->addAutowiringType(UrlGeneratorInterface::class);
28+
$definition->addAutowiringType(UrlMatcherInterface::class);
29+
$definition->addAutowiringType(RequestMatcherInterface::class);
30+
$definition->addAutowiringType(RouterInterface::class);
31+
}
32+
}
33+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ControllerArgumentValueResolverPass;
2020
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
2121
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass;
22+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingPass;
2223
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
2324
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
2425
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
@@ -71,6 +72,7 @@ public function build(ContainerBuilder $container)
7172
parent::build($container);
7273

7374
$container->addCompilerPass(new RoutingResolverPass());
75+
$container->addCompilerPass(new RoutingPass());
7476
$container->addCompilerPass(new ProfilerPass());
7577
// must be registered before removing private services as some might be listeners/subscribers
7678
// but as late as possible to get resolved parameters

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Doctrine\Common\Annotations\CachedReader;
16+
use Symfony\Component\Routing\RouterInterface;
1617
use Symfony\Component\Templating\EngineInterface as ComponentEngineInterface;
1718
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as FrameworkBundleEngineInterface;
1819

@@ -46,6 +47,15 @@ public function testTemplatingAutowiring()
4647
$this->assertInstanceOf(ComponentEngineInterface::class, $autowiredServices->getEngine());
4748
}
4849

50+
public function testRouterAutowiring()
51+
{
52+
static::bootKernel();
53+
$container = static::$kernel->getContainer();
54+
55+
$autowiredServices = $container->get('test.autowiring_types.autowired_services');
56+
$this->assertInstanceOf(RouterInterface::class, $autowiredServices->getRouter());
57+
}
58+
4959
protected static function createKernel(array $options = array())
5060
{
5161
return parent::createKernel(array('test_case' => 'AutowiringTypes') + $options);

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/AutowiringTypes/AutowiredServices.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@
1313

1414
use Doctrine\Common\Annotations\Reader;
1515
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as FrameworkBundleEngineInterface;
16+
use Symfony\Component\Routing\RouterInterface;
1617
use Symfony\Component\Templating\EngineInterface;
1718

1819
class AutowiredServices
1920
{
2021
private $annotationReader;
2122
private $frameworkBundleEngine;
2223
private $engine;
24+
private $router;
2325

24-
public function __construct(Reader $annotationReader = null, FrameworkBundleEngineInterface $frameworkBundleEngine, EngineInterface $engine)
26+
public function __construct(Reader $annotationReader = null, FrameworkBundleEngineInterface $frameworkBundleEngine, EngineInterface $engine, RouterInterface $router)
2527
{
2628
$this->annotationReader = $annotationReader;
2729
$this->frameworkBundleEngine = $frameworkBundleEngine;
2830
$this->engine = $engine;
31+
$this->router = $router;
2932
}
3033

3134
public function getAnnotationReader()
@@ -42,4 +45,9 @@ public function getEngine()
4245
{
4346
return $this->engine;
4447
}
48+
49+
public function getRouter()
50+
{
51+
return $this->router;
52+
}
4553
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/AutowiringTypes/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ services:
55
test.autowiring_types.autowired_services:
66
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\AutowiringTypes\AutowiredServices
77
autowire: true
8+
9+
another_router:
10+
class: Symfony\Bundle\FrameworkBundle\Routing\Router
11+
arguments:
12+
- "@service_container"
13+
814
framework:
915
templating:
1016
engines: ['php']

0 commit comments

Comments
 (0)