diff --git a/src/Controller/EntryPointController.php b/src/Controller/EntryPointController.php index 4a3a7d1..49fbf32 100644 --- a/src/Controller/EntryPointController.php +++ b/src/Controller/EntryPointController.php @@ -2,6 +2,7 @@ namespace Drupal\jsonapi\Controller; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Controller\ControllerBase; use Drupal\jsonapi\Plugin\JsonApiResourceManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -13,6 +14,13 @@ use Symfony\Component\HttpFoundation\JsonResponse; class EntryPointController extends ControllerBase { /** + * The configuration factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** * The resource manager interface. * * @var \Drupal\jsonapi\Plugin\JsonApiResourceManager @@ -25,8 +33,9 @@ class EntryPointController extends ControllerBase { * @param \Drupal\jsonapi\Plugin\JsonApiResourceManager $resource_plugin_manager * The resource manager. */ - public function __construct(JsonApiResourceManager $resource_plugin_manager) { + public function __construct(JsonApiResourceManager $resource_plugin_manager, ConfigFactoryInterface $config_factory) { $this->resourcePluginManager = $resource_plugin_manager; + $this->configFactory = $config_factory; } /** @@ -34,7 +43,9 @@ class EntryPointController extends ControllerBase { */ public static function create(ContainerInterface $container) { $resource_plugin_manager = $container->get('plugin.manager.resource.processor'); - return new static($resource_plugin_manager); + $config_factory = $container->get('config.factory'); + + return new static($resource_plugin_manager, $config_factory); } /** @@ -42,14 +53,16 @@ class EntryPointController extends ControllerBase { */ public function entryPoint() { $links = [ - 'self' => '/' . \Drupal::config('jsonapi.resource_info')->get('prefix'), + 'self' => '/' . $this->configFactory->get('jsonapi.resource_info')->get('prefix'), ]; + foreach ($this->resourcePluginManager->getDefinitions() as $plugin_id => $plugin_definition) { if (empty($plugin_definition['enabled'])) { continue; } $links[$plugin_definition['type']] = $plugin_definition['data']['partialPath']; } + $data = [ 'links' => $links, 'meta' => [ diff --git a/src/Routing/Routes.php b/src/Routing/Routes.php index d668e8f..b38556b 100644 --- a/src/Routing/Routes.php +++ b/src/Routing/Routes.php @@ -3,6 +3,7 @@ namespace Drupal\jsonapi\Routing; use Drupal\Core\Authentication\AuthenticationCollectorInterface; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Field\EntityReferenceFieldItemList; use Drupal\jsonapi\Plugin\JsonApiResourceManager; @@ -41,6 +42,13 @@ class Routes implements ContainerInjectionInterface { protected $authCollector; /** + * The configuration factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** * List of providers. * * @var string[] @@ -55,9 +63,10 @@ class Routes implements ContainerInjectionInterface { * @param \Drupal\Core\Authentication\AuthenticationCollectorInterface $auth_collector * The resource manager. */ - public function __construct(JsonApiResourceManager $resource_plugin_manager, AuthenticationCollectorInterface $auth_collector) { + public function __construct(JsonApiResourceManager $resource_plugin_manager, AuthenticationCollectorInterface $auth_collector, ConfigFactoryInterface $config_factory) { $this->resourcePluginManager = $resource_plugin_manager; $this->authCollector = $auth_collector; + $this->configFactory = $config_factory; } /** @@ -68,7 +77,9 @@ class Routes implements ContainerInjectionInterface { $resource_plugin_manager = $container->get('plugin.manager.resource.processor'); /* @var \Drupal\Core\Authentication\AuthenticationCollectorInterface $auth_collector */ $auth_collector = $container->get('authentication_collector'); - return new static($resource_plugin_manager, $auth_collector); + /* @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory*/ + $config_factory = $container->get('config.factory'); + return new static($resource_plugin_manager, $auth_collector, $config_factory); } /** @@ -76,15 +87,17 @@ class Routes implements ContainerInjectionInterface { */ public function routes() { $collection = new RouteCollection(); - $api_base_path = \Drupal::config('jsonapi.resource_info')->get('prefix'); + $api_base_path = $this->configFactory->get('jsonapi.resource_info')->get('prefix'); $route_collection = (new Route($api_base_path)) ->setRequirement('_format', 'api_json') ->setRequirement('_permission', 'access jsonapi resource list') ->setDefaults([ '_controller' => '\Drupal\jsonapi\Controller\EntryPointController::entryPoint', ]) + ->addOptions(['_is_jsonapi' => TRUE]) + ->setOption('_auth', $this->authProviderList()) ->setMethods(['GET']); - $collection->add('jsonapi:entry-point', $route_collection); + $collection->add('jsonapi.entry_point', $route_collection); foreach ($this->resourcePluginManager->getDefinitions() as $plugin_id => $plugin_definition) { if (empty($plugin_definition['enabled'])) { diff --git a/tests/src/Unit/Routing/RoutesTest.php b/tests/src/Unit/Routing/RoutesTest.php index 78c7a0a..8996444 100644 --- a/tests/src/Unit/Routing/RoutesTest.php +++ b/tests/src/Unit/Routing/RoutesTest.php @@ -3,6 +3,8 @@ namespace Drupal\Tests\jsonapi\Unit\Routing; use Drupal\Core\Authentication\AuthenticationCollectorInterface; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\jsonapi\Plugin\JsonApiResourceManager; use Drupal\jsonapi\Routing\Routes; use Drupal\Tests\UnitTestCase; @@ -74,6 +76,14 @@ class RoutesTest extends UnitTestCase { ]); $container = $this->prophesize(ContainerInterface::class); $container->get('plugin.manager.resource.processor')->willReturn($resource_plugin_manager->reveal()); + + $config_entity = $this->prophesize(ConfigEntityInterface::class); + $config_entity->get('prefix')->willReturn('api'); + + $config_factory = $this->prophesize(ConfigFactoryInterface::class); + $config_factory->get('jsonapi.resource_info')->willReturn($config_entity->reveal()); + $container->get('config.factory')->willReturn($config_factory->reveal()); + $auth_collector = $this->prophesize(AuthenticationCollectorInterface::class); $auth_collector->getSortedProviders()->willReturn([ 'lorem' => [], @@ -84,7 +94,6 @@ class RoutesTest extends UnitTestCase { $this->routes['ok'] = Routes::create($container->reveal()); } - /** * @covers ::routes */ @@ -92,8 +101,9 @@ class RoutesTest extends UnitTestCase { // Get the route collection and start making assertions. $routes = $this->routes['ok']->routes(); - // Make sure that there are 4 routes for each resource. - $this->assertEquals(6, $routes->count()); + // Make sure that there are 6 routes for each resource, plus the entry + // point. + $this->assertEquals(7, $routes->count()); $iterator = $routes->getIterator(); // Check the collection route.