Skip to content
Snippets Groups Projects
Select Git revision
  • 4831e2ee7c9185a6f00b418ea4d88f8cf32f80b0
  • 11.x default protected
  • 10.5.x protected
  • 11.2.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.2 protected
  • 11.2.3 protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
41 results

Shortcut.php

  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    MimeTypeMatcher.php 1.73 KiB
    <?php
    
    /**
     * @file
     * Contains Drupal\Core\Routing\MimeTypeMatcher.
     */
    
    namespace Drupal\Core\Routing;
    
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
    use Symfony\Component\Routing\RouteCollection;
    use Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface;
    
    /**
     * This class filters routes based on the media type in HTTP Accept headers.
     */
    class MimeTypeMatcher implements RouteFilterInterface {
    
    
      /**
       * Implements \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface::filter()
       */
      public function filter(RouteCollection $collection, Request $request) {
        // Generates a list of Symfony formats matching the acceptable MIME types.
        // @todo replace by proper content negotiation library.
        $acceptable_mime_types = $request->getAcceptableContentTypes();
        $acceptable_formats = array_map(array($request, 'getFormat'), $acceptable_mime_types);
    
        $filtered_collection = new RouteCollection();
    
        foreach ($collection as $name => $route) {
          // _format could be a |-delimited list of supported formats.
          $supported_formats = array_filter(explode('|', $route->getRequirement('_format')));
          // The route partially matches if it doesn't care about format, if it
          // explicitly allows any format, or if one of its allowed formats is
          // in the request's list of acceptable formats.
          if (empty($supported_formats) || in_array('*/*', $acceptable_mime_types) || array_intersect($acceptable_formats, $supported_formats)) {
            $filtered_collection->add($name, $route);
          }
        }
    
        if (!count($filtered_collection)) {
          throw new UnsupportedMediaTypeHttpException();
        }
    
        return $filtered_collection;
      }
    
    }