Automatically enable the routing annotation loader

This commit is contained in:
Guilhem Niot 2017-06-02 20:02:43 +02:00
parent c77d8067dd
commit c2f796fa15
8 changed files with 92 additions and 10 deletions

View File

@ -98,8 +98,7 @@
"symfony/phpunit-bridge": "~3.2", "symfony/phpunit-bridge": "~3.2",
"symfony/polyfill-apcu": "~1.1", "symfony/polyfill-apcu": "~1.1",
"symfony/security-acl": "~2.8|~3.0", "symfony/security-acl": "~2.8|~3.0",
"phpdocumentor/reflection-docblock": "^3.0", "phpdocumentor/reflection-docblock": "^3.0"
"sensio/framework-extra-bundle": "^3.0.2"
}, },
"conflict": { "conflict": {
"phpdocumentor/reflection-docblock": "<3.0", "phpdocumentor/reflection-docblock": "<3.0",

View File

@ -48,7 +48,7 @@ CHANGELOG
`Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass` instead `Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass` instead
* Deprecated `ValidateWorkflowsPass`, use * Deprecated `ValidateWorkflowsPass`, use
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` instead `Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` instead
* Deprecated `ConstraintValidatorFactory`, use * Deprecated `ConstraintValidatorFactory`, use
`Symfony\Component\Validator\ContainerConstraintValidatorFactory` instead. `Symfony\Component\Validator\ContainerConstraintValidatorFactory` instead.
3.2.0 3.2.0

View File

@ -15,6 +15,7 @@ use Doctrine\Common\Annotations\Reader;
use Symfony\Bridge\Monolog\Processor\DebugProcessor; use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
use Symfony\Component\Cache\Adapter\AdapterInterface; use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
@ -48,6 +49,8 @@ use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface; use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
use Symfony\Component\Serializer\Encoder\CsvEncoder; use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Encoder\DecoderInterface; use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface; use Symfony\Component\Serializer\Encoder\EncoderInterface;
@ -692,6 +695,29 @@ class FrameworkExtension extends Extension
$container->findDefinition('router.default')->getClass(), $container->findDefinition('router.default')->getClass(),
)); ));
} }
if ($this->annotationsConfigEnabled) {
$container->register('routing.loader.annotation', AnnotatedRouteControllerLoader::class)
->setPublic(false)
->addTag('routing.loader', array('priority' => -10))
->addArgument(new Reference('annotation_reader'));
$container->register('routing.loader.directory', AnnotationDirectoryLoader::class)
->setPublic(false)
->addTag('routing.loader', array('priority' => -10))
->setArguments(array(
new Reference('file_locator'),
new Reference('routing.loader.annotation'),
));
$container->register('routing.loader.file', AnnotationFileLoader::class)
->setPublic(false)
->addTag('routing.loader', array('priority' => -10))
->setArguments(array(
new Reference('file_locator'),
new Reference('routing.loader.annotation'),
));
}
} }
/** /**

View File

@ -0,0 +1,52 @@
<?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\Routing;
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
use Symfony\Component\Routing\Route;
/**
* AnnotatedRouteControllerLoader is an implementation of AnnotationClassLoader
* that sets the '_controller' default based on the class and method names.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class AnnotatedRouteControllerLoader extends AnnotationClassLoader
{
/**
* Configures the _controller default parameter of a given Route instance.
*
* @param mixed $annot The annotation class instance
*/
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
{
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
}
/**
* Makes the default route name more sane by removing common keywords.
*
* @return string
*/
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
{
return preg_replace(array(
'/(bundle|controller)_/',
'/action(_\d+)?$/',
'/__/',
), array(
'_',
'\\1',
'_',
), parent::getDefaultRouteName($class, $method));
}
}

View File

@ -11,10 +11,8 @@
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle; use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
return array( return array(
new FrameworkBundle(), new FrameworkBundle(),
new TestBundle(), new TestBundle(),
new SensioFrameworkExtraBundle(),
); );

View File

@ -28,7 +28,7 @@
"symfony/polyfill-mbstring": "~1.0", "symfony/polyfill-mbstring": "~1.0",
"symfony/filesystem": "~2.8|~3.0|~4.0", "symfony/filesystem": "~2.8|~3.0|~4.0",
"symfony/finder": "~2.8|~3.0|~4.0", "symfony/finder": "~2.8|~3.0|~4.0",
"symfony/routing": "~3.3|~4.0", "symfony/routing": "~3.4|~4.0",
"symfony/stopwatch": "~2.8|~3.0|~4.0", "symfony/stopwatch": "~2.8|~3.0|~4.0",
"doctrine/cache": "~1.0" "doctrine/cache": "~1.0"
}, },
@ -57,8 +57,7 @@
"symfony/web-link": "~3.3|~4.0", "symfony/web-link": "~3.3|~4.0",
"doctrine/annotations": "~1.0", "doctrine/annotations": "~1.0",
"phpdocumentor/reflection-docblock": "^3.0", "phpdocumentor/reflection-docblock": "^3.0",
"twig/twig": "~1.34|~2.4", "twig/twig": "~1.34|~2.4"
"sensio/framework-extra-bundle": "^3.0.2"
}, },
"conflict": { "conflict": {
"phpdocumentor/reflection-docblock": "<3.0", "phpdocumentor/reflection-docblock": "<3.0",

View File

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
3.4.0
-----
* Added support for prioritized routing loaders.
3.3.0 3.3.0
----- -----
@ -19,7 +24,7 @@ CHANGELOG
* Added support for `bool`, `int`, `float`, `string`, `list` and `map` defaults in XML configurations. * Added support for `bool`, `int`, `float`, `string`, `list` and `map` defaults in XML configurations.
* Added support for UTF-8 requirements * Added support for UTF-8 requirements
2.8.0 2.8.0
----- -----

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Routing\DependencyInjection;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
/** /**
* Adds tagged routing.loader services to routing.resolver service. * Adds tagged routing.loader services to routing.resolver service.
@ -22,6 +23,8 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
*/ */
class RoutingResolverPass implements CompilerPassInterface class RoutingResolverPass implements CompilerPassInterface
{ {
use PriorityTaggedServiceTrait;
private $resolverServiceId; private $resolverServiceId;
private $loaderTag; private $loaderTag;
@ -39,7 +42,7 @@ class RoutingResolverPass implements CompilerPassInterface
$definition = $container->getDefinition($this->resolverServiceId); $definition = $container->getDefinition($this->resolverServiceId);
foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) { foreach ($this->findAndSortTaggedServices($this->loaderTag, $container) as $id) {
$definition->addMethodCall('addLoader', array(new Reference($id))); $definition->addMethodCall('addLoader', array(new Reference($id)));
} }
} }