3 namespace Drupal\rest\RouteProcessor;
5 use Drupal\Core\Render\BubbleableMetadata;
6 use Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface;
7 use Drupal\Core\Routing\RouteProviderInterface;
8 use Symfony\Component\Routing\Route;
11 * Processes the BC REST routes, to ensure old route names continue to work.
13 class RestResourceGetRouteProcessorBC implements OutboundRouteProcessorInterface {
16 * The available serialization formats.
20 protected $serializerFormats = [];
25 * @var \Drupal\Core\Routing\RouteProviderInterface
27 protected $routeProvider;
30 * Constructs a RestResourceGetRouteProcessorBC object.
32 * @param string[] $serializer_formats
33 * The available serialization formats.
34 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
37 public function __construct(array $serializer_formats, RouteProviderInterface $route_provider) {
38 $this->serializerFormats = $serializer_formats;
39 $this->routeProvider = $route_provider;
45 public function processOutbound($route_name, Route $route, array &$parameters, BubbleableMetadata $bubbleable_metadata = NULL) {
46 $route_name_parts = explode('.', $route_name);
47 // BC: the REST module originally created per-format GET routes, instead
48 // of a single route. To minimize the surface of this BC layer, this uses
49 // route definitions that are as empty as possible, plus an outbound route
51 // @see \Drupal\rest\Plugin\ResourceBase::routes()
52 if ($route_name_parts[0] === 'rest' && $route_name_parts[count($route_name_parts) - 2] === 'GET' && in_array($route_name_parts[count($route_name_parts) - 1], $this->serializerFormats, TRUE)) {
53 array_pop($route_name_parts);
54 $redirected_route_name = implode('.', $route_name_parts);
55 @trigger_error(sprintf("The '%s' route is deprecated since version 8.5.x and will be removed in 9.0.0. Use the '%s' route instead.", $route_name, $redirected_route_name), E_USER_DEPRECATED);
56 static::overwriteRoute($route, $this->routeProvider->getRouteByName($redirected_route_name));
61 * Overwrites one route's metadata with the other's.
63 * @param \Symfony\Component\Routing\Route $target_route
64 * The route whose metadata to overwrite.
65 * @param \Symfony\Component\Routing\Route $source_route
66 * The route whose metadata to read from.
68 * @see \Symfony\Component\Routing\Route
70 protected static function overwriteRoute(Route $target_route, Route $source_route) {
71 $target_route->setPath($source_route->getPath());
72 $target_route->setDefaults($source_route->getDefaults());
73 $target_route->setRequirements($source_route->getRequirements());
74 $target_route->setOptions($source_route->getOptions());
75 $target_route->setHost($source_route->getHost());
76 $target_route->setSchemes($source_route->getSchemes());
77 $target_route->setMethods($source_route->getMethods());