Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / rest / src / RouteProcessor / RestResourceGetRouteProcessorBC.php
1 <?php
2
3 namespace Drupal\rest\RouteProcessor;
4
5 use Drupal\Core\Render\BubbleableMetadata;
6 use Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface;
7 use Drupal\Core\Routing\RouteProviderInterface;
8 use Symfony\Component\Routing\Route;
9
10 /**
11  * Processes the BC REST routes, to ensure old route names continue to work.
12  */
13 class RestResourceGetRouteProcessorBC implements OutboundRouteProcessorInterface {
14
15   /**
16    * The available serialization formats.
17    *
18    * @var string[]
19    */
20   protected $serializerFormats = [];
21
22   /**
23    * The route provider.
24    *
25    * @var \Drupal\Core\Routing\RouteProviderInterface
26    */
27   protected $routeProvider;
28
29   /**
30    * Constructs a RestResourceGetRouteProcessorBC object.
31    *
32    * @param string[] $serializer_formats
33    *   The available serialization formats.
34    * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
35    *   The route provider.
36    */
37   public function __construct(array $serializer_formats, RouteProviderInterface $route_provider) {
38     $this->serializerFormats = $serializer_formats;
39     $this->routeProvider = $route_provider;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
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
50     // processor.
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));
57     }
58   }
59
60   /**
61    * Overwrites one route's metadata with the other's.
62    *
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.
67    *
68    * @see \Symfony\Component\Routing\Route
69    */
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());
78   }
79
80 }