You are here

final class ResourceEnhancer in JSON:API Resources 8

Route enhancer for JSON:API Resource routes.

Hierarchy

Expanded class hierarchy of ResourceEnhancer

1 file declares its use of ResourceEnhancer
ResourceEnhancerTest.php in tests/src/Kernel/Routing/ResourceEnhancerTest.php
1 string reference to 'ResourceEnhancer'
jsonapi_resources.services.yml in ./jsonapi_resources.services.yml
jsonapi_resources.services.yml
1 service uses ResourceEnhancer
route_enhancer.jsonapi_resource in ./jsonapi_resources.services.yml
Drupal\jsonapi_resources\Unstable\Routing\Enhancer\ResourceEnhancer

File

src/Unstable/Routing/Enhancer/ResourceEnhancer.php, line 14

Namespace

Drupal\jsonapi_resources\Unstable\Routing\Enhancer
View source
final class ResourceEnhancer implements EnhancerInterface {

  /**
   * A class resolver.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
   */
  protected $classResolver;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * ResourceEnhancer constructor.
   *
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
   *   A class resolver to load the appropriate JSON:API resource for the
   *   matched route.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route matched by the request.
   */
  public function __construct(ClassResolverInterface $class_resolver, RouteMatchInterface $route_match) {
    $this->classResolver = $class_resolver;
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public function enhance(array $defaults, Request $request) {
    if (!isset($defaults['_jsonapi_resource'])) {
      return $defaults;
    }
    $defaults['_controller'] = 'controller.jsonapi_resource:processRequest';
    if (!isset($defaults['resource_types'])) {
      $resource = $this->classResolver
        ->getInstanceFromDefinition($defaults['_jsonapi_resource']);
      assert($resource instanceof ResourceBase);
      $defaults['resource_types'] = $resource
        ->getRouteResourceTypes($defaults['_route_object'], $defaults['_route']);
    }
    return $defaults;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResourceEnhancer::$classResolver protected property A class resolver.
ResourceEnhancer::$routeMatch protected property The current route match.
ResourceEnhancer::enhance public function Update the defaults based on its own data and the request.
ResourceEnhancer::__construct public function ResourceEnhancer constructor.