Commit 096ecb49 authored by catch's avatar catch

Issue #2935783 by larowlan, Wim Leers: RequestHandler's handle method...

Issue #2935783 by larowlan, Wim Leers: RequestHandler's handle method interacts with the Route object but should be able to just declare arguments
parent 35986f56
......@@ -7,7 +7,6 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\rest\Plugin\ResourceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -25,13 +24,6 @@
*/
class RequestHandler implements ContainerInjectionInterface {
/**
* The resource configuration storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $resourceStorage;
/**
* The config factory.
*
......@@ -49,15 +41,12 @@ class RequestHandler implements ContainerInjectionInterface {
/**
* Creates a new RequestHandler instance.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The resource configuration storage.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Symfony\Component\Serializer\SerializerInterface|\Symfony\Component\Serializer\Encoder\DecoderInterface $serializer
* The serializer.
*/
public function __construct(EntityStorageInterface $entity_storage, ConfigFactoryInterface $config_factory, SerializerInterface $serializer) {
$this->resourceStorage = $entity_storage;
public function __construct(ConfigFactoryInterface $config_factory, SerializerInterface $serializer) {
$this->configFactory = $config_factory;
$this->serializer = $serializer;
}
......@@ -67,7 +56,6 @@ public function __construct(EntityStorageInterface $entity_storage, ConfigFactor
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')->getStorage('rest_resource_config'),
$container->get('config.factory'),
$container->get('serializer')
);
......@@ -80,19 +68,17 @@ public static function create(ContainerInterface $container) {
* The route match.
* @param \Symfony\Component\HttpFoundation\Request $request
* The HTTP request object.
* @param \Drupal\rest\RestResourceConfigInterface $_rest_resource_config
* REST resource config entity ID.
*
* @return \Symfony\Component\HttpFoundation\Response|\Drupal\rest\ResourceResponseInterface
* @return \Drupal\rest\ResourceResponseInterface|\Symfony\Component\HttpFoundation\Response
* The REST resource response.
*/
public function handle(RouteMatchInterface $route_match, Request $request) {
$resource_config_id = $route_match->getRouteObject()->getDefault('_rest_resource_config');
/** @var \Drupal\rest\RestResourceConfigInterface $resource_config */
$resource_config = $this->resourceStorage->load($resource_config_id);
$response = $this->delegateToRestResourcePlugin($route_match, $request, $resource_config->getResourcePlugin());
public function handle(RouteMatchInterface $route_match, Request $request, RestResourceConfigInterface $_rest_resource_config) {
$response = $this->delegateToRestResourcePlugin($route_match, $request, $_rest_resource_config->getResourcePlugin());
if ($response instanceof CacheableResponseInterface) {
$response->addCacheableDependency($resource_config);
$response->addCacheableDependency($_rest_resource_config);
// Add global rest settings config's cache tag, for BC flags.
// @see \Drupal\rest\Plugin\rest\resource\EntityResource::permissions()
// @see \Drupal\rest\EventSubscriber\RestConfigSubscriber
......
......@@ -133,6 +133,12 @@ protected function getRoutesForResourceConfig(RestResourceConfigInterface $rest_
}
$route->setOption('_auth', $rest_resource_config->getAuthenticationProviders($method));
$route->setDefault('_rest_resource_config', $rest_resource_config->id());
$parameters = $route->getOption('parameters') ?: [];
$route->setOption('parameters', $parameters + [
'_rest_resource_config' => [
'type' => 'entity:' . $rest_resource_config->getEntityTypeId(),
],
]);
$collection->add("rest.$name", $route);
}
......
......@@ -4,7 +4,6 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\KernelTests\KernelTestBase;
use Drupal\rest\Plugin\ResourceBase;
......@@ -42,12 +41,11 @@ class RequestHandlerTest extends KernelTestBase {
*/
public function setUp() {
parent::setUp();
$this->entityStorage = $this->prophesize(EntityStorageInterface::class);
$config_factory = $this->prophesize(ConfigFactoryInterface::class);
$config_factory->get('rest.settings')
->willReturn($this->prophesize(ImmutableConfig::class)->reveal());
$serializer = $this->prophesize(SerializerInterface::class);
$this->requestHandler = new RequestHandler($this->entityStorage->reveal(), $config_factory->reveal(), $serializer->reveal());
$this->requestHandler = new RequestHandler($config_factory->reveal(), $serializer->reveal());
}
/**
......@@ -67,18 +65,17 @@ public function testHandle() {
$config->getCacheContexts()->willReturn([]);
$config->getCacheTags()->willReturn([]);
$config->getCacheMaxAge()->willReturn(12);
$this->entityStorage->load('restplugin')->willReturn($config->reveal());
// Response returns NULL this time because response from plugin is not
// a ResourceResponse so it is passed through directly.
$response = $this->requestHandler->handle($route_match, $request);
$response = $this->requestHandler->handle($route_match, $request, $config->reveal());
$this->assertEquals(NULL, $response);
// Response will return a ResourceResponse this time.
$response = new ResourceResponse([]);
$resource->get(NULL, $request)
->willReturn($response);
$handler_response = $this->requestHandler->handle($route_match, $request);
$handler_response = $this->requestHandler->handle($route_match, $request, $config->reveal());
$this->assertEquals($response, $handler_response);
// We will call the patch method this time.
......@@ -88,7 +85,7 @@ public function testHandle() {
$resource->patch(NULL, $request)
->shouldBeCalledTimes(1)
->willReturn($response);
$handler_response = $this->requestHandler->handle($route_match, $request);
$handler_response = $this->requestHandler->handle($route_match, $request, $config->reveal());
$this->assertEquals($response, $handler_response);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment