diff --git a/core/modules/rest/src/RequestHandler.php b/core/modules/rest/src/RequestHandler.php index 7624fab..2f51703 100644 --- a/core/modules/rest/src/RequestHandler.php +++ b/core/modules/rest/src/RequestHandler.php @@ -2,11 +2,13 @@ namespace Drupal\rest; +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Render\RenderContext; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\rest\Entity\RestResourceConfig; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -17,11 +19,35 @@ /** * Acts as intermediate request forwarder for resource plugins. */ -class RequestHandler implements ContainerAwareInterface { +class RequestHandler implements ContainerAwareInterface, ContainerInjectionInterface { use ContainerAwareTrait; /** + * The resource configuration storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $resourceStorage; + + /** + * Creates a new RequestHandler instance. + * + * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage + * The resource configuration storage. + */ + public function __construct(EntityStorageInterface $entity_storage) { + $this->resourceStorage = $entity_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static($container->get('entity_type.manager')->get('rest_resource_config')); + } + + /** * Handles a web API request. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match @@ -37,7 +63,7 @@ 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 = RestResourceConfig::load($resource_config_id); + $resource_config = $this->resourceStorage->load($resource_config_id); $resource = $resource_config->getResourcePlugin(); // Deserialize incoming data if available. diff --git a/core/modules/rest/tests/src/Kernel/RequestHandlerTest.php b/core/modules/rest/tests/src/Kernel/RequestHandlerTest.php index 36a482c..2747bd3 100644 --- a/core/modules/rest/tests/src/Kernel/RequestHandlerTest.php +++ b/core/modules/rest/tests/src/Kernel/RequestHandlerTest.php @@ -1,18 +1,15 @@ requestHandler = new RequestHandler(); + $this->entityStorage = $this->prophesize(EntityStorageInterface::class); + $this->requestHandler = new RequestHandler($this->entityStorage->reveal()); $this->requestHandler->setContainer($this->container); } @@ -47,17 +52,19 @@ public function setUp() { */ public function testBaseHandler() { $request = new Request(); - $route_match = new RouteMatch('test', new Route('/rest/test', ['_plugin' => 'restplugin', '_format' => 'json'])); + $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => 'restplugin', '_format' => 'json'])); $resource = $this->prophesize(StubRequestHandlerResourcePlugin::class); $resource->get(NULL, $request) ->shouldBeCalled(); - // Setup stub plugin manager that will return our plugin. - $stub = $this->prophesize(ResourcePluginManager::class); - $stub->createInstance('restplugin') - ->willReturn($resource->reveal()); - $this->container->set('plugin.manager.rest', $stub->reveal()); + // Setup the configuration. + $config = $this->prophesize(RestResourceConfigInterface::class); + $config->getResourcePlugin()->willReturn($resource->reveal()); + $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. @@ -89,15 +96,17 @@ public function testBaseHandler() { */ public function testSerialization($data) { $request = new Request(); - $route_match = new RouteMatch('test', new Route('/rest/test', ['_plugin' => 'restplugin', '_format' => 'json'])); + $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => 'restplugin', '_format' => 'json'])); $resource = $this->prophesize(StubRequestHandlerResourcePlugin::class); - // Setup stub plugin manager that will return our plugin. - $stub = $this->prophesize(ResourcePluginManager::class); - $stub->createInstance('restplugin') - ->willReturn($resource->reveal()); - $this->container->set('plugin.manager.rest', $stub->reveal()); + // Setup the configuration. + $config = $this->prophesize(RestResourceConfigInterface::class); + $config->getResourcePlugin()->willReturn($resource->reveal()); + $config->getCacheContexts()->willReturn([]); + $config->getCacheTags()->willReturn([]); + $config->getCacheMaxAge()->willReturn(12); + $this->entityStorage->load('restplugin')->willReturn($config->reveal()); $response = new ResourceResponse($data); $resource->get(NULL, $request)