JsonapiServiceProvider.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Drupal\jsonapi;
  3. use Drupal\Core\DependencyInjection\ContainerBuilder;
  4. use Drupal\Core\DependencyInjection\ServiceModifierInterface;
  5. use Drupal\Core\DependencyInjection\ServiceProviderInterface;
  6. use Drupal\Core\StackMiddleware\NegotiationMiddleware;
  7. use Drupal\jsonapi\DependencyInjection\Compiler\RegisterSerializationClassesCompilerPass;
  8. /**
  9. * Adds 'api_json' as known format and prevents its use in the REST module.
  10. *
  11. * @internal JSON:API maintains no PHP API since its API is the HTTP API. This
  12. * class may change at any time and this will break any dependencies on it.
  13. *
  14. * @see https://www.drupal.org/project/drupal/issues/3032787
  15. * @see jsonapi.api.php
  16. */
  17. class JsonapiServiceProvider implements ServiceModifierInterface, ServiceProviderInterface {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function alter(ContainerBuilder $container) {
  22. if ($container->has('http_middleware.negotiation') && is_a($container->getDefinition('http_middleware.negotiation')->getClass(), NegotiationMiddleware::class, TRUE)) {
  23. // @see http://www.iana.org/assignments/media-types/application/vnd.api+json
  24. $container->getDefinition('http_middleware.negotiation')
  25. ->addMethodCall('registerFormat', [
  26. 'api_json',
  27. ['application/vnd.api+json'],
  28. ]);
  29. }
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function register(ContainerBuilder $container) {
  35. $container->addCompilerPass(new RegisterSerializationClassesCompilerPass());
  36. }
  37. }