diff --git a/core/lib/Drupal/Core/Render/MetadataBubblingUrlGenerator.php b/core/lib/Drupal/Core/Render/MetadataBubblingUrlGenerator.php index bbdeaab0a1e61fcba481a0947d1f16f95d78f30e..482f6dffe7338f9952e905adc279ebebca3d324a 100644 --- a/core/lib/Drupal/Core/Render/MetadataBubblingUrlGenerator.php +++ b/core/lib/Drupal/Core/Render/MetadataBubblingUrlGenerator.php @@ -110,16 +110,42 @@ public function generateFromRoute($name, $parameters = [], $options = [], $colle } /** - * {@inheritdoc} + * Checks if route name is a string or route object. + * + * @param string|\Symfony\Component\Routing\Route $name + * The route "name" which may also be an object or anything. + * + * @return bool + * TRUE if the passed in value a valid route, FALSE otherwise. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Only string + * route names are supported. + * + * @see https://www.drupal.org/node/3172303 */ public function supports($name) { + @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Only string route names are supported. See https://www.drupal.org/node/3172303', E_USER_DEPRECATED); return $this->urlGenerator->supports($name); } /** - * {@inheritdoc} + * Gets either the route name or a string based on the route object. + * + * @param string|\Symfony\Component\Routing\Route $name + * The route "name" which may also be an object or anything. + * @param array $parameters + * Route parameters array. + * + * @return string + * Either the route name, or a string that uniquely identifies the route. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use + * the route name instead. + * + * @see https://www.drupal.org/node/3172303 */ public function getRouteDebugMessage($name, array $parameters = []) { + @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use the route name instead. See https://www.drupal.org/node/3172303', E_USER_DEPRECATED); return $this->urlGenerator->getRouteDebugMessage($name, $parameters); } diff --git a/core/lib/Drupal/Core/Routing/UrlGenerator.php b/core/lib/Drupal/Core/Routing/UrlGenerator.php index cf0f24b4085475f3a67214232ac6ba9377cd1168..e837eb963fd50fa490994d9050b6bb879fcbb305 100644 --- a/core/lib/Drupal/Core/Routing/UrlGenerator.php +++ b/core/lib/Drupal/Core/Routing/UrlGenerator.php @@ -127,7 +127,7 @@ public function isStrictRequirements() { */ public function getPathFromRoute($name, $parameters = []) { $route = $this->getRoute($name); - $name = $this->getRouteDebugMessage($name); + $name = $this->getRouteStringIdentifier($name); $this->processRoute($name, $route, $parameters); $path = $this->getInternalPathFromRoute($name, $route, $parameters); // Router-based paths may have a querystring on them but Drupal paths may @@ -287,7 +287,7 @@ public function generateFromRoute($name, $parameters = [], $options = [], $colle $options += $route->getOption('default_url_options') ?: []; $options += ['prefix' => '', 'path_processing' => TRUE]; - $name = $this->getRouteDebugMessage($name); + $name = $this->getRouteStringIdentifier($name); $this->processRoute($name, $route, $parameters, $generated_url); $path = $this->getInternalPathFromRoute($name, $route, $parameters, $options['query']); // Outbound path processors might need the route object for the path, e.g. @@ -429,17 +429,68 @@ protected function getRoute($name) { } /** - * {@inheritdoc} + * Gets either the route name or a string based on the route object. + * + * @param string|\Symfony\Component\Routing\Route $name + * A string route name, or a serializable object. + * + * @return string + * Either the route name, or a string that uniquely identifies the route. + * + * @todo Remove in https://www.drupal.org/i/3151019 + * + * @internal + */ + private function getRouteStringIdentifier(string|SymfonyRoute $name): string { + if (is_scalar($name)) { + return $name; + } + + if ($name instanceof SymfonyRoute) { + return 'Route with pattern ' . $name->getPath(); + } + + return serialize($name); + } + + /** + * Checks if route name is a string or route object. + * + * @param string|\Symfony\Component\Routing\Route $name + * The route "name" which may also be an object or anything. + * + * @return bool + * TRUE if the passed in value a valid route, FALSE otherwise. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Only string + * route names are supported. + * + * @see https://www.drupal.org/node/3172303 */ public function supports($name) { + @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Only string route names are supported. See https://www.drupal.org/node/3172303', E_USER_DEPRECATED); // Support a route object and any string as route name. return is_string($name) || $name instanceof SymfonyRoute; } /** - * {@inheritdoc} + * Gets either the route name or a string based on the route object. + * + * @param string|\Symfony\Component\Routing\Route $name + * The route "name" which may also be an object or anything. + * @param array $parameters + * Route parameters array. + * + * @return string + * Either the route name, or a string that uniquely identifies the route. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use + * the route name instead. + * + * @see https://www.drupal.org/node/3172303 */ public function getRouteDebugMessage($name, array $parameters = []) { + @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use the route name instead. See https://www.drupal.org/node/3172303', E_USER_DEPRECATED); if (is_scalar($name)) { return $name; } diff --git a/core/modules/help/tests/modules/help_test/src/SupernovaGenerator.php b/core/modules/help/tests/modules/help_test/src/SupernovaGenerator.php index 8c5b25ef08a850f8bfa6c6ab1cffd1711ad77904..2ab7f6484a6a9915ab9a9c2764e00f19a53b12cf 100644 --- a/core/modules/help/tests/modules/help_test/src/SupernovaGenerator.php +++ b/core/modules/help/tests/modules/help_test/src/SupernovaGenerator.php @@ -45,18 +45,4 @@ public function generateFromRoute($name, $parameters = [], $options = [], $colle throw new \Exception(); } - /** - * {@inheritdoc} - */ - public function supports($name) { - throw new \Exception(); - } - - /** - * {@inheritdoc} - */ - public function getRouteDebugMessage($name, array $parameters = []) { - throw new \Exception(); - } - } diff --git a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php index b796efea4502f465829fe61cfdd8438290784d9a..54d169f76ae11a42c06826a397e8f3d17e4b0a16 100644 --- a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php @@ -451,6 +451,18 @@ public function testBaseURLGeneration() { $this->assertGenerateFromRoute('test_1', ['zoo' => 5], $options, 'http://www.example.com:8888/hello/world?zoo=5#top', (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT)); } + /** + * Tests deprecated methods. + * + * @group legacy + */ + public function testDeprecatedMethods() { + $this->expectDeprecation('Drupal\Core\Routing\UrlGenerator::getRouteDebugMessage() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use the route name instead. See https://www.drupal.org/node/3172303'); + $this->assertSame('test', $this->generator->getRouteDebugMessage('test')); + $this->expectDeprecation('Drupal\Core\Routing\UrlGenerator::supports() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Only string route names are supported. See https://www.drupal.org/node/3172303'); + $this->assertTrue($this->generator->supports('test')); + } + /** * Tests that the 'scheme' route requirement is respected during URL * generation.