diff --git a/core/modules/rest/rest.services.yml b/core/modules/rest/rest.services.yml index 8f53334..5ebc39a 100644 --- a/core/modules/rest/rest.services.yml +++ b/core/modules/rest/rest.services.yml @@ -14,9 +14,6 @@ services: arguments: ['@session_configuration'] tags: - { name: access_check } - rest.endpoint_repository: - class: Drupal\rest\RestEndpointRepository - arguments: ['@entity.manager'] rest.link_manager: class: Drupal\rest\LinkManager\LinkManager arguments: ['@rest.link_manager.type', '@rest.link_manager.relation'] @@ -28,7 +25,7 @@ services: arguments: ['@cache.default', '@entity.manager', '@unrouted_url_assembler'] rest.resource_routes: class: Drupal\rest\Routing\ResourceRoutes - arguments: ['@plugin.manager.rest', '@rest.endpoint_repository', '@logger.channel.rest'] + arguments: ['@plugin.manager.rest', '@entity.manager', '@logger.channel.rest'] tags: - { name: 'event_subscriber' } logger.channel.rest: diff --git a/core/modules/rest/src/Entity/RestEndpoint.php b/core/modules/rest/src/Entity/RestEndpoint.php index 1aa4636..28d3c16 100644 --- a/core/modules/rest/src/Entity/RestEndpoint.php +++ b/core/modules/rest/src/Entity/RestEndpoint.php @@ -43,7 +43,15 @@ protected function getLabelFromPlugin() { /** * {@inheritdoc} */ - public function getConfiguration() { + public function getSettings() { return $this->configuration; } -} \ No newline at end of file + + /** + * @param array $settings + */ + public function setSettings($settings) { + $this->configuration = $settings; + } + +} diff --git a/core/modules/rest/src/RequestHandler.php b/core/modules/rest/src/RequestHandler.php index 15cdcf5..4079b2f 100644 --- a/core/modules/rest/src/RequestHandler.php +++ b/core/modules/rest/src/RequestHandler.php @@ -9,6 +9,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\rest\Entity\RestEndpoint; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\HttpFoundation\Request; @@ -47,9 +48,7 @@ public function handle(RouteMatchInterface $route_match, Request $request) { ->getInstance(array('id' => $plugin)); /** @var \Drupal\rest\RestEndpointInterface $endpoint */ - $endpoint = $this->container - ->get('rest.endpoint_repository') - ->getConfiguredEndpoint($plugin); + $endpoint = RestEndpoint::load($plugin); // Deserialize incoming data if available. $serializer = $this->container->get('serializer'); @@ -62,7 +61,7 @@ public function handle(RouteMatchInterface $route_match, Request $request) { // formats are configured allow all and hope that the serializer knows the // format. If the serializer cannot handle it an exception will be thrown // that bubbles up to the client. - $config = $endpoint->getConfiguration(); + $config = $endpoint->getSettings(); $method_settings = $config[$request->getMethod()]; if (empty($method_settings['supported_formats']) || in_array($format, $method_settings['supported_formats'])) { $definition = $resource->getPluginDefinition(); diff --git a/core/modules/rest/src/RestEndpointInterface.php b/core/modules/rest/src/RestEndpointInterface.php index 598598c..cf03733 100644 --- a/core/modules/rest/src/RestEndpointInterface.php +++ b/core/modules/rest/src/RestEndpointInterface.php @@ -8,5 +8,10 @@ /** * @return array */ - public function getConfiguration(); -} \ No newline at end of file + public function getSettings(); + + /** + * @param array $settings + */ + public function setSettings($settings); +} diff --git a/core/modules/rest/src/RestEndpointRepository.php b/core/modules/rest/src/RestEndpointRepository.php deleted file mode 100644 index 2d186f8..0000000 --- a/core/modules/rest/src/RestEndpointRepository.php +++ /dev/null @@ -1,40 +0,0 @@ -storage = $entity_manager->getStorage('rest_endpoint'); - } - - /** - * {@inheritdoc} - */ - public function getConfiguredEndpoints() { - $ids = $this->storage->getQuery()->execute(); - return $this->storage->loadMultiple($ids); - } - - /** - * {@inheritdoc} - */ - public function getConfiguredEndpoint($id) { - return $this->storage->load($id); - } -} \ No newline at end of file diff --git a/core/modules/rest/src/RestEndpointRepositoryInterface.php b/core/modules/rest/src/RestEndpointRepositoryInterface.php deleted file mode 100644 index 722b211..0000000 --- a/core/modules/rest/src/RestEndpointRepositoryInterface.php +++ /dev/null @@ -1,18 +0,0 @@ -restPluginManager = $rest_plugin_manager; - $this->endpoint_repository = $endpoint_repository; + $this->endpoint_storage = $entity_manager->getStorage('rest_endpoint'); } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static($container->get('plugin.manager.rest'), $container->get('rest.endpoint_repository')); + return new static($container->get('plugin.manager.rest'), $container->get('entity.manager')); } /** @@ -58,11 +58,11 @@ public static function create(ContainerInterface $container) { */ public function permissions() { $permissions = []; - $endpoints = $this->endpoint_repository->getConfiguredEndpoints(); + /** @var \Drupal\rest\RestEndpointInterface[] $endpoints */ + $endpoints = $this->endpoint_storage->loadMultiple(); foreach ($endpoints as $endpoint) { - $plugin_id = $endpoint->getPluginId(); - if ($this->restPluginManager->getDefinition($plugin_id, FALSE)) { - $plugin = $this->restPluginManager->getInstance(['id' => $plugin_id]); + if ($this->restPluginManager->getDefinition($endpoint->id(), FALSE)) { + $plugin = $this->restPluginManager->getInstance(['id' => $endpoint->id()]); $permissions = array_merge($permissions, $plugin->permissions()); } } diff --git a/core/modules/rest/src/Routing/ResourceRoutes.php b/core/modules/rest/src/Routing/ResourceRoutes.php index 906b638..e932c55 100644 --- a/core/modules/rest/src/Routing/ResourceRoutes.php +++ b/core/modules/rest/src/Routing/ResourceRoutes.php @@ -9,6 +9,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Routing\RouteSubscriberBase; use Drupal\rest\Plugin\Type\ResourcePluginManager; use Drupal\rest\RestEndpointRepositoryInterface; @@ -30,11 +31,11 @@ class ResourceRoutes extends RouteSubscriberBase{ protected $manager; /** - * The REST endpoint repository. + * The REST endpoint storage. * - * @var \Drupal\rest\RestEndpointRepositoryInterface + * @var \Drupal\Core\Entity\EntityManagerInterface */ - protected $endpoint_repository; + protected $endpoint_storage; /** * A logger instance. @@ -53,9 +54,9 @@ class ResourceRoutes extends RouteSubscriberBase{ * @param \Psr\Log\LoggerInterface $logger * A logger instance. */ - public function __construct(ResourcePluginManager $manager, RestEndpointRepositoryInterface $endpoint_repository, LoggerInterface $logger) { + public function __construct(ResourcePluginManager $manager, EntityManagerInterface $entity_manager, LoggerInterface $logger) { $this->manager = $manager; - $this->endpoint_repository = $endpoint_repository; + $this->endpoint_storage = $entity_manager->getStorage('rest_endpoint'); $this->logger = $logger; } @@ -70,9 +71,11 @@ protected function alterRoutes(RouteCollection $collection) { $routes = array(); // Iterate over all enabled resource plugins. - foreach ($this->endpoint_repository->getConfiguredEndpoints() as $plugin_id => $rest_endpoint) { - $plugin = $this->manager->getInstance(array('id' => $plugin_id)); - $enabled_methods = $rest_endpoint->getConfiguration(); + /** @var \Drupal\rest\RestEndpointInterface[] $endpoints */ + $endpoints = $this->endpoint_storage->loadMultiple(); + foreach ($endpoints as $endpoint) { + $plugin = $this->manager->getInstance(array('id' => $endpoint->id())); + $enabled_methods = $endpoint->getSettings(); foreach ($plugin->routes() as $name => $route) { $method = $route->getRequirement('_method'); @@ -82,13 +85,13 @@ protected function alterRoutes(RouteCollection $collection) { // Check that authentication providers are defined. if (empty($enabled_methods[$method]['supported_auth']) || !is_array($enabled_methods[$method]['supported_auth'])) { - $this->logger->error('At least one authentication provider must be defined for resource @id', array(':id' => $plugin_id)); + $this->logger->error('At least one authentication provider must be defined for resource @id', array(':id' => $endpoint->id())); continue; } // Check that formats are defined. if (empty($enabled_methods[$method]['supported_formats']) || !is_array($enabled_methods[$method]['supported_formats'])) { - $this->logger->error('At least one format must be defined for resource @id', array(':id' => $plugin_id)); + $this->logger->error('At least one format must be defined for resource @id', array(':id' => $endpoint->id())); continue; } diff --git a/core/modules/rest/src/Tests/PageCacheTest.php b/core/modules/rest/src/Tests/PageCacheTest.php index 9cf2859..6696bf7 100644 --- a/core/modules/rest/src/Tests/PageCacheTest.php +++ b/core/modules/rest/src/Tests/PageCacheTest.php @@ -25,7 +25,8 @@ class PageCacheTest extends RESTTestBase { * Tests that configuration changes also clear the page cache. */ public function testConfigChangePageCache() { - $this->enableService('entity:entity_test', 'GET'); + $endpoint_id = 'entity:entity_test'; + $this->enableService($endpoint_id, 'GET'); // Allow anonymous users to issue GET requests. $permissions = $this->entityPermissions('entity_test', 'view'); $permissions[] = 'restful get entity:entity_test'; @@ -38,23 +39,23 @@ public function testConfigChangePageCache() { $this->httpRequest($entity->urlInfo(), 'GET', NULL, $this->defaultMimeType); $this->assertResponse(200, 'HTTP response code is correct.'); $this->assertHeader('x-drupal-cache', 'MISS'); - $this->assertCacheTag('config:rest.settings'); + $this->assertCacheTag('config:entity:entity_test'); $this->assertCacheTag('entity_test:1'); // Read it again, should be page-cached now. $this->httpRequest($entity->urlInfo(), 'GET', NULL, $this->defaultMimeType); $this->assertResponse(200, 'HTTP response code is correct.'); $this->assertHeader('x-drupal-cache', 'HIT'); - $this->assertCacheTag('config:rest.settings'); + $this->assertCacheTag('config:entity:entity_test'); $this->assertCacheTag('entity_test:1'); - // Trigger a config save which should clear the page cache, so we should get + // Trigger an endpoint save which should clear the page cache, so we should get // a cache miss now for the same request. - $this->config('rest.settings')->save(); + $this->endpoint_storage->load($endpoint_id)->save(); $this->httpRequest($entity->urlInfo(), 'GET', NULL, $this->defaultMimeType); $this->assertResponse(200, 'HTTP response code is correct.'); $this->assertHeader('x-drupal-cache', 'MISS'); - $this->assertCacheTag('config:rest.settings'); + $this->assertCacheTag('config:entity:entity_test'); $this->assertCacheTag('entity_test:1'); } diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index ff382d5..157efb6 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -19,6 +19,13 @@ abstract class RESTTestBase extends WebTestBase { /** + * The REST endpoint storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $endpoint_storage; + + /** * The default serialization format to use for testing REST operations. * * @var string @@ -233,7 +240,8 @@ protected function entityValues($entity_type) { */ protected function enableService($resource_type, $method = 'GET', $format = NULL, $auth = NULL) { // Enable REST API for this entity type. - $config = $this->config('rest.settings'); + /** @var \Drupal\rest\RestEndpointInterface $endpoint */ + $endpoint = $this->endpoint_storage->create(['id' => $resource_type]); $settings = array(); if ($resource_type) { @@ -247,8 +255,8 @@ protected function enableService($resource_type, $method = 'GET', $format = NULL } $settings[$resource_type][$method]['supported_auth'] = $auth; } - $config->set('resources', $settings); - $config->save(); + $endpoint->setSettings($settings); + $endpoint->save(); $this->rebuildCache(); } diff --git a/core/modules/rest/src/Tests/ResourceTest.php b/core/modules/rest/src/Tests/ResourceTest.php index be02617..bd57036 100644 --- a/core/modules/rest/src/Tests/ResourceTest.php +++ b/core/modules/rest/src/Tests/ResourceTest.php @@ -7,8 +7,6 @@ namespace Drupal\rest\Tests; -use Drupal\rest\Tests\RESTTestBase; - /** * Tests the structure of a REST resource. * @@ -28,7 +26,7 @@ class ResourceTest extends RESTTestBase { */ protected function setUp() { parent::setUp(); - $this->config = $this->config('rest.settings'); + $this->endpoint_storage = $this->container->get('entity.manager')->getStorage('rest_endpoint')->create(); // Create an entity programmatically. $this->entity = $this->entityCreate('entity_test'); @@ -39,19 +37,17 @@ protected function setUp() { * Tests that a resource without formats cannot be enabled. */ public function testFormats() { - $settings = array( - 'entity:entity_test' => array( - 'GET' => array( - 'supported_auth' => array( - 'basic_auth', - ), - ), - ), - ); - + /** @var \Drupal\rest\RestEndpointInterface $endpoint */ + $endpoint = $this->endpoint_storage->create(['id' => 'entity:entity_test']); + $endpoint->setSettings([ + 'GET' => [ + 'supported_auth' => [ + 'basic_auth', + ], + ], + ]); // Attempt to enable the resource. - $this->config->set('resources', $settings); - $this->config->save(); + $endpoint->save(); $this->rebuildCache(); // Verify that accessing the resource returns 401. @@ -68,19 +64,17 @@ public function testFormats() { * Tests that a resource without authentication cannot be enabled. */ public function testAuthentication() { - $settings = array( - 'entity:entity_test' => array( - 'GET' => array( - 'supported_formats' => array( - 'hal_json', - ), - ), - ), - ); - + /** @var \Drupal\rest\RestEndpointInterface $endpoint */ + $endpoint = $this->endpoint_storage->create(['id' => 'entity:entity_test']); + $endpoint->setSettings([ + 'GET' => [ + 'supported_formats' => [ + 'hal_json', + ], + ], + ]); // Attempt to enable the resource. - $this->config->set('resources', $settings); - $this->config->save(); + $endpoint->save(); $this->rebuildCache(); // Verify that accessing the resource returns 401.