10000 [FrameworkBundle][Routing] Deprecate annotations by alexandre-daubois · Pull Request #51483 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle][Routing] Deprecate annotations #51483

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

Merged
Merged
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
418 changes: 209 additions & 209 deletions .github/expected-missing-return-types.diff

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions UPGRADE-6.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ FrameworkBundle
| `framework.validation.email_validation_mode` | `'loose'` | `'html5'` |
* Deprecate `framework.validation.enable_annotations`, use `framework.validation.enable_attributes` instead
* Deprecate `framework.serializer.enable_annotations`, use `framework.serializer.enable_attributes` instead
* Deprecate the `routing.loader.annotation` service, use the `routing.loader.attribute` service instead
* Deprecate the `routing.loader.annotation.directory` service, use the `routing.loader.attribute.directory` service instead
* Deprecate the `routing.loader.annotation.file` service, use the `routing.loader.attribute.file` service instead
* Deprecate `AnnotatedRouteControllerLoader`, use `AttributeRouteControllerLoader` instead

HttpFoundation
--------------
Expand Down Expand Up @@ -181,6 +185,9 @@ Routing
* [BC break] Add native return type to `AnnotationClassLoader::setResolver()`
* Deprecate Doctrine annotations support in favor of native attributes
* Deprecate passing an annotation reader as first argument to `AnnotationClassLoader` (new signature: `__construct(?string $env = null)`)
* Deprecate `AnnotationClassLoader`, use `AttributeClassLoader` instead
* Deprecate `AnnotationDirectoryLoader`, use `AttributeDirectoryLoader` instead
* Deprecate `AnnotationFileLoader`, use `AttributeFileLoader` instead

Security
--------
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ CHANGELOG
* Change `framework.asset_mapper.importmap_polyfill` from a URL to the name of an item in the importmap
* Provide `$buildDir` when running `CacheWarmer` to build read-only resources
* Add the global `--profile` option to the console to enable profiling commands
* Deprecate the `routing.loader.annotation` service, use the `routing.loader.attribute` service instead
* Deprecate the `routing.loader.annotation.directory` service, use the `routing.loader.attribute.directory` service instead
* Deprecate the `routing.loader.annotation.file` service, use the `routing.loader.attribute.file` service instead
* Deprecate `AnnotatedRouteControllerLoader`, use `AttributeRouteControllerLoader` instead

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
use Symfony\Component\RateLimiter\Storage\CacheStorage;
use Symfony\Component\RemoteEvent\Attribute\AsRemoteEventConsumer;
use Symfony\Component\RemoteEvent\RemoteEvent;
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
use Symfony\Component\Routing\Loader\AttributeClassLoader;
use Symfony\Component\Scheduler\Attribute\AsCronTask;
use Symfony\Component\Scheduler\Attribute\AsPeriodicTask;
use Symfony\Component\Scheduler\Attribute\AsSchedule;
Expand Down Expand Up @@ -1226,8 +1226,8 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
->replaceArgument(0, $config['default_uri']);
}

if ($this->isInitializedConfigEnabled('annotations') && (new \ReflectionClass(AnnotationClassLoader::class))->hasProperty('reader')) {
$container->getDefinition('routing.loader.annotation')->setArguments([
if ($this->isInitializedConfigEnabled('annotations') && (new \ReflectionClass(AttributeClassLoader::class))->hasProperty('reader')) {
$container->getDefinition('routing.loader.attribute')->setArguments([
new Reference('annotation_reader'),
'%kernel.environment%',
]);
Expand Down
9E7A
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function configureRoutes(RoutingConfigurator $routes): void
}

if (false !== ($fileName = (new \ReflectionObject($this))->getFileName())) {
$routes->import($fileName, 'annotation');
$routes->import($fileName, 'attribute');
}
}

Expand Down
25 changes: 17 additions & 8 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
use Symfony\Bundle\FrameworkBundle\Routing\AttributeRouteControllerLoader;
use Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader;
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
Expand All @@ -24,8 +24,8 @@
use Symfony\Component\Routing\Generator\CompiledUrlGenerator;
use Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
use Symfony\Component\Routing\Loader\AttributeDirectoryLoader;
use Symfony\Component\Routing\Loader\AttributeFileLoader;
use Symfony\Component\Routing\Loader\ContainerLoader;
use Symfony\Component\Routing\Loader\DirectoryLoader;
use Symfony\Component\Routing\Loader\GlobFileLoader;
Expand Down Expand Up @@ -92,26 +92,35 @@
])
->tag('routing.loader')

->set('routing.loader.annotation', AnnotatedRouteControllerLoader::class)
->set('routing.loader.attribute', AttributeRouteControllerLoader::class)
->args([
'%kernel.environment%',
])
->tag('routing.loader', ['priority' => -10])

->set('routing.loader.annotation.directory', AnnotationDirectoryLoader::class)
->alias('routing.loader.annotation', 'routing.loader.attribute')
->deprecate('symfony/routing', '6.4', 'The "%alias_id%" service is deprecated, use the "routing.loader.attribute" service instead.')

->set('routing.loader.attribute.directory', AttributeDirectoryLoader::class)
->args([
service('file_locator'),
service('routing.loader.annotation'),
service('routing.loader.attribute'),
])
->tag('routing.loader', ['priority' => -10])

->set('routing.loader.annotation.file', AnnotationFileLoader::class)
->alias('routing.loader.annotation.directory', 'routing.loader.attribute.directory')
->deprecate('symfony/routing', '6.4', 'The "%alias_id%" service is deprecated, use the "routing.loader.attribute.directory" service instead.')

->set('routing.loader.attribute.file', AttributeFileLoader::class)
->args([
service('file_locator'),
service('routing.loader.annotation'),
service('routing.loader.attribute'),
])
->tag('routing.loader', ['priority' => -10])

->alias('routing.loader.annotation.file', 'routing.loader.attribute.file')
->deprecate('symfony/routing', '6.4', 'The "%alias_id%" service is deprecated, use the "routing.loader.attribute.file" service instead.')

->set('routing.loader.psr4', Psr4DirectoryLoader::class)
->args([
service('file_locator'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,15 @@

namespace Symfony\Bundle\FrameworkBundle\Routing;

use Symfony\Component\Routing\Loader\AnnotationClassLoader;
use Symfony\Component\Routing\Route;
trigger_deprecation('symfony/framework-bundle', '6.4', 'The "%s" class is deprecated, use "%s" instead.', AnnotatedRouteControllerLoader::class, AttributeRouteControllerLoader::class);

/**
* 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.
*
* @return void
*/
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot)
{
if ('__invoke' === $method->getName()) {
$route->setDefault('_controller', $class->getName());
} else {
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
}
}
class_exists(AttributeRouteControllerLoader::class);

if (false) {
/**
* Makes the default route name more sane by removing common keywords.
* @deprecated since Symfony 6.4, to be removed in 7.0, use {@link AttributeRouteControllerLoader} instead
*/
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method): string
class AnnotatedRouteControllerLoader
{
$name = preg_replace('/(bundle|controller)_/', '_', parent::getDefaultRouteName($class, $method));

if (str_ends_with($method->name, 'Action') || str_ends_with($method->name, '_action')) {
$name = preg_replace('/action(_\d+)?$/', '\\1', $name);
}

return str_replace('__', '_', $name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\AttributeClassLoader;
use Symfony\Component\Routing\Route;

/**
* AttributeRouteControllerLoader is an implementation of AttributeClassLoader
* that sets the '_controller' default based on the class and method names.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Alexandre Daubois <alex.daubois@gmail.com>
*/
class AttributeRouteControllerLoader extends AttributeClassLoader
{
/**
* Configures the _controller default parameter of a given Route instance.
*
* @return void
*/
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot)
{
if ('__invoke' === $method->getName()) {
$route->setDefault('_controller', $class->getName());
} else {
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
}
}

/**
* Makes the default route name more sane by removing common keywords.
*/
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method): string
{
$name = preg_replace('/(bundle|controller)_/', '_', parent::getDefaultRouteName($class, $method));

if (str_ends_with($method->name, 'Action') || str_ends_with($method->name, '_action')) {
$name = preg_replace('/action(_\d+)?$/', '\\1', $name);
}

return str_replace('__', '_', $name);
}
}

if (!class_exists(AnnotatedRouteControllerLoader::class, false)) {
class_alias(AttributeRouteControllerLoader::class, AnnotatedRouteControllerLoader::class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ http_client_call:

uid:
resource: "../../Controller/UidController.php"
type: "annotation"
type: "attribute"

send_notification:
path: /send_notification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
_routingconditionservice_bundle:
prefix: /
resource: "@RoutingConditionServiceBundle/Controller"
type: annotation
type: attribute
3 changes: 3 additions & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ CHANGELOG
* Add native return type to `AnnotationClassLoader::setResolver()`
* Deprecate Doctrine annotations support in favor of native attributes
* Change the constructor signature of `AnnotationClassLoader` to `__construct(?string $env = null)`, passing an annotation reader as first argument is deprecated
* Deprecate `AnnotationClassLoader`, use `AttributeClassLoader` instead
* Deprecate `AnnotationDirectoryLoader`, use `AttributeDirectoryLoader` instead
* Deprecate `AnnotationFileLoader`, use `AttributeFileLoader` instead

6.2
---
Expand Down
Loading
0