core/modules/rest/src/Plugin/ResourceBase.php | 19 ++++++++++--------- .../rest/src/Plugin/rest/resource/EntityResource.php | 12 +++++++++++- core/modules/rest/src/RestServiceProvider.php | 2 +- .../Plugin/rest/resource/UserRegistrationResource.php | 8 ++++++-- .../tests/src/Unit/UserRegistrationResourceTest.php | 14 +++++++++++--- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/core/modules/rest/src/Plugin/ResourceBase.php b/core/modules/rest/src/Plugin/ResourceBase.php index c0ae970..3e803a00 100644 --- a/core/modules/rest/src/Plugin/ResourceBase.php +++ b/core/modules/rest/src/Plugin/ResourceBase.php @@ -3,6 +3,7 @@ namespace Drupal\rest\Plugin; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginBase; use Psr\Log\LoggerInterface; @@ -42,11 +43,11 @@ protected $logger; /** - * The config factory. + * REST settings config instance. * - * @var \Drupal\Core\Config\ConfigFactoryInterface + * @var \Drupal\Core\Config\ImmutableConfig */ - protected $configFactory; + protected $restSettings; /** * Constructs a Drupal\rest\Plugin\ResourceBase object. @@ -61,14 +62,14 @@ * The available serialization formats. * @param \Psr\Log\LoggerInterface $logger * A logger instance. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory - * The config factory. + * @param \Drupal\Core\Config\ImmutableConfig $rest_settings + * A REST settings config instance. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, ConfigFactoryInterface $config_factory) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, ImmutableConfig $rest_settings) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->serializerFormats = $serializer_formats; $this->logger = $logger; - $this->configFactory = $config_factory; + $this->restSettings = $rest_settings; } /** @@ -81,7 +82,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->getParameter('serializer.formats'), $container->get('logger.factory')->get('rest'), - $container->get('config.factory') + $container->get('config.factory')->get('rest.settings') ); } @@ -122,7 +123,7 @@ public function routes() { $create_path = $definition['uri_paths']['https://www.drupal.org/link-relations/create']; } - $rest_bc_settings = $this->configFactory->get('rest.settings')->get('bc'); + $rest_bc_settings = $this->restSettings->get('bc'); $route_name = strtr($this->pluginId, ':', '.'); diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index 1d26daa..f5162ee 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -59,6 +59,13 @@ class EntityResource extends ResourceBase implements DependentPluginInterface { protected $linkRelationTypeManager; /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** * Constructs a Drupal\rest\Plugin\rest\resource\EntityResource object. * * @param array $configuration @@ -73,12 +80,15 @@ class EntityResource extends ResourceBase implements DependentPluginInterface { * The available serialization formats. * @param \Psr\Log\LoggerInterface $logger * A logger instance. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. * @param \Drupal\Component\Plugin\PluginManagerInterface $link_relation_type_manager * The link relation type manager. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, $serializer_formats, LoggerInterface $logger, ConfigFactoryInterface $config_factory, PluginManagerInterface $link_relation_type_manager) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger, $config_factory); + parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger, $config_factory->get('rest.settings')); $this->entityType = $entity_type_manager->getDefinition($plugin_definition['entity_type']); + $this->configFactory = $config_factory; $this->linkRelationTypeManager = $link_relation_type_manager; } diff --git a/core/modules/rest/src/RestServiceProvider.php b/core/modules/rest/src/RestServiceProvider.php index a3997f6..0de4477 100644 --- a/core/modules/rest/src/RestServiceProvider.php +++ b/core/modules/rest/src/RestServiceProvider.php @@ -52,7 +52,7 @@ public function register(ContainerBuilder $container) { // BC: outbound route processor for format-specific GET routes. Not added // in rest.services.yml because its presence depends on configuration. $settings = BootstrapConfigStorageFactory::get()->read('rest.settings'); - if ($settings['bc']['format_specific_get_routes']) { + if (!empty($settings['bc']['format_specific_get_routes'])){ $container->register('rest.route_processor_get_bc', RestResourceGetRouteProcessorBC::class) ->addArgument(new Parameter('serializer.formats')) ->addArgument(new Reference('router.route_provider')) diff --git a/core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php b/core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php index 6a243c3..8c82a87 100644 --- a/core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php +++ b/core/modules/user/src/Plugin/rest/resource/UserRegistrationResource.php @@ -2,6 +2,7 @@ namespace Drupal\user\Plugin\rest\resource; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Session\AccountInterface; use Drupal\rest\ModifiedResourceResponse; @@ -59,13 +60,15 @@ class UserRegistrationResource extends ResourceBase { * The available serialization formats. * @param \Psr\Log\LoggerInterface $logger * A logger instance. + * @param \Drupal\Core\Config\ImmutableConfig $rest_settings + * A REST settings config instance. * @param \Drupal\Core\Config\ImmutableConfig $user_settings * A user settings config instance. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, ImmutableConfig $user_settings, AccountInterface $current_user) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); + public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, ImmutableConfig $rest_settings, ImmutableConfig $user_settings, AccountInterface $current_user) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger, $rest_settings); $this->userSettings = $user_settings; $this->currentUser = $current_user; } @@ -80,6 +83,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->getParameter('serializer.formats'), $container->get('logger.factory')->get('rest'), + $container->get('config.factory')->get('rest.settings'), $container->get('config.factory')->get('user.settings'), $container->get('current_user') ); diff --git a/core/modules/user/tests/src/Unit/UserRegistrationResourceTest.php b/core/modules/user/tests/src/Unit/UserRegistrationResourceTest.php index 142685c..a66d77d 100644 --- a/core/modules/user/tests/src/Unit/UserRegistrationResourceTest.php +++ b/core/modules/user/tests/src/Unit/UserRegistrationResourceTest.php @@ -58,6 +58,13 @@ class UserRegistrationResourceTest extends UnitTestCase { protected $reflection; /** + * A REST settings config instance. + * + * @var \Drupal\Core\Config\ImmutableConfig|\PHPUnit_Framework_MockObject_MockObject + */ + protected $restSettings; + + /** * A user settings config instance. * * @var \Drupal\Core\Config\ImmutableConfig|\PHPUnit_Framework_MockObject_MockObject @@ -86,11 +93,12 @@ protected function setUp() { $this->logger = $this->prophesize(LoggerInterface::class)->reveal(); + $this->restSettings = $this->prophesize(ImmutableConfig::class); $this->userSettings = $this->prophesize(ImmutableConfig::class); $this->currentUser = $this->prophesize(AccountInterface::class); - $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); + $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->restSettings->reveal(), $this->userSettings->reveal(), $this->currentUser->reveal()); $this->reflection = new \ReflectionClass($this->testClass); } @@ -122,7 +130,7 @@ public function testRegistrationAdminOnlyPost() { $this->currentUser->isAnonymous()->willReturn(TRUE); - $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); + $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->restSettings->reveal(), $this->userSettings->reveal(), $this->currentUser->reveal()); $entity = $this->prophesize(User::class); $entity->isNew()->willReturn(TRUE); @@ -138,7 +146,7 @@ public function testRegistrationAdminOnlyPost() { public function testRegistrationAnonymousOnlyPost() { $this->currentUser->isAnonymous()->willReturn(FALSE); - $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->userSettings->reveal(), $this->currentUser->reveal()); + $this->testClass = new UserRegistrationResource([], 'plugin_id', '', [], $this->logger, $this->restSettings->reveal(), $this->userSettings->reveal(), $this->currentUser->reveal()); $entity = $this->prophesize(User::class); $entity->isNew()->willReturn(TRUE);