Skip to content

Commit d051741

Browse files
committed
feature #23044 Automatically enable the routing annotation loader (GuilhemN)
This PR was merged into the 3.4 branch. Discussion ---------- Automatically enable the routing annotation loader | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | there's probably one but I didn't find it | License | MIT | Doc PR | Thanks to fqcn services, most of the time, we don't need the SensioFrameworkExtraBundle to use `@Route`. So I suggest to automatically enable it when annotations are enabled. This way we could simplify https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/3.3/etc/routing.yaml#L5. Note: I added priority support for routing loaders to make sure sensio loaders are executed before ours. Commits ------- c2f796fa15 Automatically enable the routing annotation loader
2 parents 9346dff + 280ff0c commit d051741

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ CHANGELOG
4848
`Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass` instead
4949
* Deprecated `ValidateWorkflowsPass`, use
5050
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` instead
51-
* Deprecated `ConstraintValidatorFactory`, use
51+
* Deprecated `ConstraintValidatorFactory`, use
5252
`Symfony\Component\Validator\ContainerConstraintValidatorFactory` instead.
5353

5454
3.2.0

DependencyInjection/FrameworkExtension.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
1616
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1717
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18+
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
1819
use Symfony\Component\Cache\Adapter\AdapterInterface;
1920
use Symfony\Component\Cache\Adapter\ArrayAdapter;
2021
use Symfony\Component\Config\FileLocator;
@@ -48,6 +49,8 @@
4849
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
4950
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
5051
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
52+
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
53+
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
5154
use Symfony\Component\Serializer\Encoder\CsvEncoder;
5255
use Symfony\Component\Serializer\Encoder\DecoderInterface;
5356
use Symfony\Component\Serializer\Encoder\EncoderInterface;
@@ -695,6 +698,29 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
695698
$container->findDefinition('router.default')->getClass(),
696699
));
697700
}
701+
702+
if ($this->annotationsConfigEnabled) {
703+
$container->register('routing.loader.annotation', AnnotatedRouteControllerLoader::class)
704+
->setPublic(false)
705+
->addTag('routing.loader', array('priority' => -10))
706+
->addArgument(new Reference('annotation_reader'));
707+
708+
$container->register('routing.loader.directory', AnnotationDirectoryLoader::class)
709+
->setPublic(false)
710+
->addTag('routing.loader', array('priority' => -10))
711+
->setArguments(array(
712+
new Reference('file_locator'),
713+
new Reference('routing.loader.annotation'),
714+
));
715+
716+
$container->register('routing.loader.file', AnnotationFileLoader::class)
717+
->setPublic(false)
718+
->addTag('routing.loader', array('priority' => -10))
719+
->setArguments(array(
720+
new Reference('file_locator'),
721+
new Reference('routing.loader.annotation'),
722+
));
723+
}
698724
}
699725

700726
/**
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Routing;
13+
14+
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
15+
use Symfony\Component\Routing\Route;
16+
17+
/**
18+
* AnnotatedRouteControllerLoader is an implementation of AnnotationClassLoader
19+
* that sets the '_controller' default based on the class and method names.
20+
*
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
class AnnotatedRouteControllerLoader extends AnnotationClassLoader
24+
{
25+
/**
26+
* Configures the _controller default parameter of a given Route instance.
27+
*
28+
* @param mixed $annot The annotation class instance
29+
*/
30+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
31+
{
32+
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
33+
}
34+
35+
/**
36+
* Makes the default route name more sane by removing common keywords.
37+
*
38+
* @return string
39+
*/
40+
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
41+
{
42+
return preg_replace(array(
43+
'/(bundle|controller)_/',
44+
'/action(_\d+)?$/',
45+
'/__/',
46+
), array(
47+
'_',
48+
'\\1',
49+
'_',
50+
), parent::getDefaultRouteName($class, $method));
51+
}
52+
}

Tests/Functional/app/AnnotatedController/bundles.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111

1212
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
1313
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
14-
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
1514

1615
return array(
1716
new FrameworkBundle(),
1817
new TestBundle(),
19-
new SensioFrameworkExtraBundle(),
2018
);

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"symfony/polyfill-mbstring": "~1.0",
2929
"symfony/filesystem": "~2.8|~3.0|~4.0",
3030
"symfony/finder": "~2.8|~3.0|~4.0",
31-
"symfony/routing": "~3.3|~4.0",
31+
"symfony/routing": "~3.4|~4.0",
3232
"symfony/stopwatch": "~2.8|~3.0|~4.0",
3333
"doctrine/cache": "~1.0"
3434
},
@@ -57,8 +57,7 @@
5757
"symfony/web-link": "~3.3|~4.0",
5858
"doctrine/annotations": "~1.0",
5959
"phpdocumentor/reflection-docblock": "^3.0",
60-
"twig/twig": "~1.34|~2.4",
61-
"sensio/framework-extra-bundle": "^3.0.2"
60+
"twig/twig": "~1.34|~2.4"
6261
},
6362
"conflict": {
6463
"phpdocumentor/reflection-docblock": "<3.0",

0 commit comments

Comments
 (0)