comparison core/modules/rest/src/Routing/ResourceRoutes.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
90 90
91 foreach ($plugin->routes() as $name => $route) { 91 foreach ($plugin->routes() as $name => $route) {
92 /** @var \Symfony\Component\Routing\Route $route */ 92 /** @var \Symfony\Component\Routing\Route $route */
93 // @todo: Are multiple methods possible here? 93 // @todo: Are multiple methods possible here?
94 $methods = $route->getMethods(); 94 $methods = $route->getMethods();
95 // Only expose routes where the method is enabled in the configuration. 95 // Only expose routes
96 if ($methods && ($method = $methods[0]) && $supported_formats = $rest_resource_config->getFormats($method)) { 96 // - that have an explicit method and allow >=1 format for that method
97 // - that exist for BC
98 // @see \Drupal\rest\RouteProcessor\RestResourceGetRouteProcessorBC
99 if (($methods && ($method = $methods[0]) && $supported_formats = $rest_resource_config->getFormats($method)) || $route->hasOption('bc_route')) {
97 $route->setRequirement('_csrf_request_header_token', 'TRUE'); 100 $route->setRequirement('_csrf_request_header_token', 'TRUE');
98 101
99 // Check that authentication providers are defined. 102 // Check that authentication providers are defined.
100 if (empty($rest_resource_config->getAuthenticationProviders($method))) { 103 if (empty($rest_resource_config->getAuthenticationProviders($method))) {
101 $this->logger->error('At least one authentication provider must be defined for resource @id', [':id' => $rest_resource_config->id()]); 104 $this->logger->error('At least one authentication provider must be defined for resource @id', [':id' => $rest_resource_config->id()]);
106 if (empty($rest_resource_config->getFormats($method))) { 109 if (empty($rest_resource_config->getFormats($method))) {
107 $this->logger->error('At least one format must be defined for resource @id', [':id' => $rest_resource_config->id()]); 110 $this->logger->error('At least one format must be defined for resource @id', [':id' => $rest_resource_config->id()]);
108 continue; 111 continue;
109 } 112 }
110 113
111 // If the route has a format requirement, then verify that the 114 // Remove BC routes for unsupported formats.
112 // resource has it. 115 if ($route->getOption('bc_route') === TRUE) {
113 $format_requirement = $route->getRequirement('_format'); 116 $format_requirement = $route->getRequirement('_format');
114 if ($format_requirement && !in_array($format_requirement, $rest_resource_config->getFormats($method))) { 117 if ($format_requirement && !in_array($format_requirement, $rest_resource_config->getFormats($method))) {
115 continue; 118 continue;
119 }
116 } 120 }
117 121
118 // The configuration has been validated, so we update the route to: 122 // The configuration has been validated, so we update the route to:
123 // - set the allowed response body content types/formats for methods
124 // that may send response bodies
119 // - set the allowed request body content types/formats for methods that 125 // - set the allowed request body content types/formats for methods that
120 // allow request bodies to be sent 126 // allow request bodies to be sent
121 // - set the allowed authentication providers 127 // - set the allowed authentication providers
128 if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'PATCH'], TRUE)) {
129 $route->addRequirements(['_format' => implode('|', $rest_resource_config->getFormats($method))]);
130 }
122 if (in_array($method, ['POST', 'PATCH', 'PUT'], TRUE)) { 131 if (in_array($method, ['POST', 'PATCH', 'PUT'], TRUE)) {
123 // Restrict the incoming HTTP Content-type header to the allowed
124 // formats.
125 $route->addRequirements(['_content_type_format' => implode('|', $rest_resource_config->getFormats($method))]); 132 $route->addRequirements(['_content_type_format' => implode('|', $rest_resource_config->getFormats($method))]);
126 } 133 }
127 $route->setOption('_auth', $rest_resource_config->getAuthenticationProviders($method)); 134 $route->setOption('_auth', $rest_resource_config->getAuthenticationProviders($method));
128 $route->setDefault('_rest_resource_config', $rest_resource_config->id()); 135 $route->setDefault('_rest_resource_config', $rest_resource_config->id());
136 $parameters = $route->getOption('parameters') ?: [];
137 $route->setOption('parameters', $parameters + [
138 '_rest_resource_config' => [
139 'type' => 'entity:' . $rest_resource_config->getEntityTypeId(),
140 ],
141 ]);
129 $collection->add("rest.$name", $route); 142 $collection->add("rest.$name", $route);
130 } 143 }
131 144
132 } 145 }
133 return $collection; 146 return $collection;