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

Skip to content

Commit bdd888f

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 ------- c2f796f Automatically enable the routing annotation loader
2 parents 25f1368 + c2f796f commit bdd888f

File tree

8 files changed

+92
-10
lines changed

8 files changed

+92
-10
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@
9898
"symfony/phpunit-bridge": "~3.2",
9999
"symfony/polyfill-apcu": "~1.1",
100100
"symfony/security-acl": "~2.8|~3.0",
101-
"phpdocumentor/reflection-docblock": "^3.0",
102-
"sensio/framework-extra-bundle": "^3.0.2"
101+
"phpdocumentor/reflection-docblock": "^3.0"
103102
},
104103
"conflict": {
105104
"phpdocumentor/reflection-docblock": "<3.0",

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/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+
}

src/Symfony/Bundle/FrameworkBundle/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
);

src/Symfony/Bundle/FrameworkBundle/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",

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.4.0
5+
-----
6+
7+
* Added support for prioritized routing loaders.
8+
49
3.3.0
510
-----
611

@@ -19,7 +24,7 @@ CHANGELOG
1924

2025
* Added support for `bool`, `int`, `float`, `string`, `list` and `map` defaults in XML configurations.
2126
* Added support for UTF-8 requirements
22-
27+
2328
2.8.0
2429
-----
2530

src/Symfony/Component/Routing/DependencyInjection/RoutingResolverPass.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Reference;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1718

1819
/**
1920
* Adds tagged routing.loader services to routing.resolver service.
@@ -22,6 +23,8 @@
2223
*/
2324
class RoutingResolverPass implements CompilerPassInterface
2425
{
26+
use PriorityTaggedServiceTrait;
27+
2528
private $resolverServiceId;
2629
private $loaderTag;
2730

@@ -39,7 +42,7 @@ public function process(ContainerBuilder $container)
3942

4043
$definition = $container->getDefinition($this->resolverServiceId);
4144

42-
foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) {
45+
foreach ($this->findAndSortTaggedServices($this->loaderTag, $container) as $id) {
4346
$definition->addMethodCall('addLoader', array(new Reference($id)));
4447
}
4548
}

0 commit comments

Comments
 (0)