getResourceTypeNameFromParameters example


class RouteEnhancer implements EnhancerInterface {

  /** * {@inheritdoc} */
  public function enhance(array $defaults, Request $request) {
    if (!Routes::isJsonApiRequest($defaults)) {
      return $defaults;
    }

    $resource_type = Routes::getResourceTypeNameFromParameters($defaults);
    $entity_type_id = $resource_type->getEntityTypeId();
    if (!isset($defaults[$entity_type_id]) || !($entity = $defaults[$entity_type_id])) {
      return $defaults;
    }
    $retrieved_bundle = $entity->bundle();
    $configured_bundle = $resource_type->getBundle();
    if ($retrieved_bundle != $configured_bundle) {
      // If the bundle in the loaded entity does not match the bundle in the       // route (which is set based on the corresponding ResourceType), then       // throw an exception.       throw new NotFoundHttpException(sprintf('The loaded entity bundle (%s) does not match the configured resource (%s).', $retrieved_bundle$configured_bundle));
    }

  protected function isJsonApiExceptionEvent(ExceptionEvent $exception_event) {
    $request = $exception_event->getRequest();
    $parameters = $request->attributes->all();
    return $request->getRequestFormat() === 'api_json' || (bool) Routes::getResourceTypeNameFromParameters($parameters);
  }

}

  public function __construct(VersionNegotiator $version_negotiator_manager) {
    $this->versionNegotiator = $version_negotiator_manager;
  }

  /** * {@inheritdoc} */
  public function enhance(array $defaults, Request $request) {
    if (!Routes::isJsonApiRequest($defaults) || !($resource_type = Routes::getResourceTypeNameFromParameters($defaults))) {
      return $defaults;
    }

    $has_version_param = $request->query->has(static::RESOURCE_VERSION_QUERY_PARAMETER);

    // If the resource type is not versionable, then nothing needs to be     // enhanced.     if (!$resource_type->isVersionable()) {
      // If the query parameter was provided but the resource type is not       // versionable, provide a helpful error.       if ($has_version_param) {
        

      return $entity;
    }
    return NULL;
  }

  /** * {@inheritdoc} */
  public function applies($definition$name, Route $route) {
    return (
      (bool) Routes::getResourceTypeNameFromParameters($route->getDefaults()) &&
      !empty($definition['type']) && str_starts_with($definition['type'], 'entity')
    );
  }

}
Home | Imprint | This part of the site doesn't use cookies.