You are here

class ConfigSubscriber in JSON:API Extras 8.3

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/ConfigSubscriber.php \Drupal\jsonapi_extras\EventSubscriber\ConfigSubscriber

Associates config cache tag and rebuilds container + routes when necessary.

Hierarchy

  • class \Drupal\jsonapi_extras\EventSubscriber\ConfigSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ConfigSubscriber

1 string reference to 'ConfigSubscriber'
jsonapi_extras.services.yml in ./jsonapi_extras.services.yml
jsonapi_extras.services.yml
1 service uses ConfigSubscriber
jsonapi_extras.config_subscriber in ./jsonapi_extras.services.yml
Drupal\jsonapi_extras\EventSubscriber\ConfigSubscriber

File

src/EventSubscriber/ConfigSubscriber.php, line 17

Namespace

Drupal\jsonapi_extras\EventSubscriber
View source
class ConfigSubscriber implements EventSubscriberInterface {

  /**
   * The Drupal kernel.
   *
   * @var \Drupal\Core\DrupalKernelInterface
   */
  protected $drupalKernel;

  /**
   * The route building service.
   *
   * @var \Drupal\Core\Routing\RouteBuilderInterface
   */
  protected $routeBuilder;

  /**
   * Constructs a ConfigSubscriber object.
   *
   * @param \Drupal\Core\DrupalKernelInterface $drupal_kernel
   *   The Drupal kernel.
   * @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
   *   The route building service.
   */
  public function __construct(DrupalKernelInterface $drupal_kernel, RouteBuilderInterface $route_builder) {
    $this->drupalKernel = $drupal_kernel;
    $this->routeBuilder = $route_builder;
  }

  /**
   * Rebuilds container and routes  when 'path_prefix' configuration is changed.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The Event to process.
   */
  public function onSave(ConfigCrudEvent $event) {
    $container = \Drupal::getContainer();

    // It is problematic to rebuild the container during the installation.
    $should_process = $container
      ->getParameter('kernel.environment') !== 'install' && $event
      ->getConfig()
      ->getName() === 'jsonapi_extras.settings';
    if ($should_process) {

      // @see \Drupal\jsonapi_extras\JsonapiExtrasServiceProvider::alter()
      if ($event
        ->isChanged('path_prefix')) {
        $this->drupalKernel
          ->rebuildContainer();

        // Because \Drupal\jsonapi\Routing\Routes::routes() uses a container
        // parameter, we need to ensure that it uses the freshly rebuilt
        // container. Due to that, it's impossible to use an injected route
        // builder service, at least until core updates it to support
        // \Drupal\Core\DrupalKernelInterface::CONTAINER_INITIALIZE_SUBREQUEST_FINISHED.
        $this->service = $container
          ->get('router.builder');
        $container
          ->get('router.builder')
          ->rebuild();
      }
    }
  }

  /**
   * Associates JSON:API Extras' config cache tag with all JSON:API responses.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The response event.
   */
  public function onResponse(FilterResponseEvent $event) {
    if ($event
      ->getRequest()
      ->getRequestFormat() !== 'api_json') {
      return;
    }
    $response = $event
      ->getResponse();
    if (!$response instanceof CacheableResponseInterface) {
      return;
    }
    $response
      ->getCacheableMetadata()
      ->addCacheTags([
      'config:jsonapi_extras.settings',
      'config:jsonapi_resource_config_list',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ConfigEvents::SAVE][] = [
      'onSave',
    ];

    // Run before
    // \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::onResponse()
    // (priority 128), so we can add JSON:API's config cache tag.
    $events[KernelEvents::RESPONSE][] = [
      'onResponse',
      150,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSubscriber::$drupalKernel protected property The Drupal kernel.
ConfigSubscriber::$routeBuilder protected property The route building service.
ConfigSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ConfigSubscriber::onResponse public function Associates JSON:API Extras' config cache tag with all JSON:API responses.
ConfigSubscriber::onSave public function Rebuilds container and routes when 'path_prefix' configuration is changed.
ConfigSubscriber::__construct public function Constructs a ConfigSubscriber object.