8000 [FrameworkBundle] Support autowiring for RouterInterface by hason · Pull Request #19972 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Support autowiring for RouterInterface #19972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\RouterInterface;

class RoutingPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->hasAlias('router')) {
$definition = $container->findDefinition('router');
$definition->addAutowiringType(UrlGeneratorInterface::class);
$definition->addAutowiringType(UrlMatcherInterface::class);
$definition->addAutowiringType(RequestMatcherInterface::class);
$definition->addAutowiringType(RouterInterface::class);
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ControllerArgumentValueResolverPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
Expand Down Expand Up @@ -71,6 +72,7 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new RoutingResolverPass());
$container->addCompilerPass(new RoutingPass());
$container->addCompilerPass(new ProfilerPass());
// must be registered before removing private services as some might be listeners/subscribers
// but as late as possible to get resolved parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Templating\EngineInterface as ComponentEngineInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as FrameworkBundleEngineInterface;

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

public function testRouterAutowiring()
{
static::bootKernel();
$container = static::$kernel->getContainer();

$autowiredServices = $container->get('test.autowiring_types.autowired_services');
$this->assertInstanceOf(RouterInterface::class, $autowiredServices->getRouter());
}

protected static function createKernel(array $options = array())
{
return parent::createKernel(array('test_case' => 'AutowiringTypes') + $options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@

use Doctrine\Common\Annotations\Reader;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as FrameworkBundleEngineInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Templating\EngineInterface;

class AutowiredServices
{
private $annotationReader;
private $frameworkBundleEngine;
private $engine;
private $router;

public function __construct(Reader $annotationReader = null, FrameworkBundleEngineInterface $frameworkBundleEngine, EngineInterface $engine)
public function __construct(Reader $annotationReader = null, FrameworkBundleEngineInterface $frameworkBundleEngine, EngineInterface $engine, RouterInterface $router)
{
$this->annotationReader = $annotationReader;
$this->frameworkBundleEngine = $frameworkBundleEngine;
$this->engine = $engine;
$this->router = $router;
}

public function getAnnotationReader()
Expand All @@ -42,4 +45,9 @@ public function getEngine()
{
return $this->engine;
}

public function getRouter()
{
return $this->router;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ services:
test.autowiring_types.autowired_services:
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\AutowiringTypes\AutowiredServices
autowire: true

another_router:
class: Symfony\Bundle\FrameworkBundle\Routing\Router
arguments:
- "@service_container"

framework:
templating:
engines: ['php']
0