You are here

class PageNotFoundEventSubscriber in Apigee API Catalog 8.2

Same name and namespace in other branches
  1. 8 src/EventSubscriber/PageNotFoundEventSubscriber.php \Drupal\apigee_api_catalog\EventSubscriber\PageNotFoundEventSubscriber

Handles not found exceptions for apidoc entities.

@package Drupal\apigee_api_catalog\EventSubscriber

Hierarchy

  • class \Drupal\apigee_api_catalog\EventSubscriber\PageNotFoundEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PageNotFoundEventSubscriber

1 string reference to 'PageNotFoundEventSubscriber'
apigee_api_catalog.services.yml in ./apigee_api_catalog.services.yml
apigee_api_catalog.services.yml
1 service uses PageNotFoundEventSubscriber
apigee_api_catalog.page_not_found_subscriber in ./apigee_api_catalog.services.yml
Drupal\apigee_api_catalog\EventSubscriber\PageNotFoundEventSubscriber

File

src/EventSubscriber/PageNotFoundEventSubscriber.php, line 36

Namespace

Drupal\apigee_api_catalog\EventSubscriber
View source
class PageNotFoundEventSubscriber implements EventSubscriberInterface {

  /**
   * The path validator service.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * The patch matcher service.
   *
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  protected $pathMatcher;

  /**
   * PageNotFoundEventSubscriber constructor.
   *
   * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
   *   The patch matcher service.
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The path validator service.
   */
  public function __construct(PathMatcherInterface $path_matcher, PathValidatorInterface $path_validator) {
    $this->pathValidator = $path_validator;
    $this->pathMatcher = $path_matcher;
  }

  /**
   * Redirects to the apidoc canonical route if we have a not found exception.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The exception event.
   */
  public function onNotFoundException(GetResponseForExceptionEvent $event) {

    // Check if the request uri matches an apidoc canonical route.
    // Also check for apidoc valid path.
    if ($event
      ->getException() instanceof NotFoundHttpException && ($uri = $event
      ->getRequest()
      ->getRequestUri()) && $this->pathMatcher
      ->matchPath($uri, '/api/*/*') && (list(, , $id) = explode('/', $uri)) && ($path = "/api/{$id}") && $this->pathValidator
      ->isValid($path)) {

      // Redirect to the apidoc.
      $event
        ->setResponse(new RedirectResponse($path));
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::EXCEPTION][] = [
      'onNotFoundException',
      0,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PageNotFoundEventSubscriber::$pathMatcher protected property The patch matcher service.
PageNotFoundEventSubscriber::$pathValidator protected property The path validator service.
PageNotFoundEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
PageNotFoundEventSubscriber::onNotFoundException public function Redirects to the apidoc canonical route if we have a not found exception.
PageNotFoundEventSubscriber::__construct public function PageNotFoundEventSubscriber constructor.