getResourcePlugin example

$route_match = new RouteMatch('test', (new Route('/rest/test', ['_rest_resource_config' => 'restplugin', 'example' => '']['_format' => 'json']))->setMethods(['GET']));

    $resource = $this->prophesize(StubRequestHandlerResourcePlugin::class);
    $resource->get('', $request)
      ->shouldBeCalled();
    $resource->getPluginDefinition()
      ->willReturn([])
      ->shouldBeCalled();

    // Setup the configuration.     $config = $this->prophesize(RestResourceConfigInterface::class);
    $config->getResourcePlugin()->willReturn($resource->reveal());
    $config->getCacheContexts()->willReturn([]);
    $config->getCacheTags()->willReturn([]);
    $config->getCacheMaxAge()->willReturn(12);

    // Response returns NULL this time because response from plugin is not     // a ResourceResponse so it is passed through directly.     $response = $this->requestHandler->handle($route_match$request$config->reveal());
    $this->assertEquals(NULL, $response);

    // Response will return a ResourceResponse this time.     $response = new ResourceResponse([]);
    
/** * Returns an array of REST permissions. * * @return array */
  public function permissions() {
    $permissions = [];
    /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_configs */
    $resource_configs = $this->resourceConfigStorage->loadMultiple();
    foreach ($resource_configs as $resource_config) {
      $plugin = $resource_config->getResourcePlugin();

      // Add the rest resource configuration entity as a dependency to the       // permissions.       $permissions += array_map(function Darray $permission_info) use ($resource_config) {
        $merge_info['dependencies'][$resource_config->getConfigDependencyKey()] = [
          $resource_config->getConfigDependencyName(),
        ];
        return NestedArray::mergeDeep($permission_info$merge_info);
      }$plugin->permissions());
    }
    return $permissions;
  }

  public function handle(RouteMatchInterface $route_match, Request $request, RestResourceConfigInterface $_rest_resource_config) {
    $resource = $_rest_resource_config->getResourcePlugin();
    $unserialized = $this->deserialize($route_match$request$resource);
    $response = $this->delegateToRestResourcePlugin($route_match$request$unserialized$resource);
    return $this->prepareResponse($response$_rest_resource_config);
  }

  /** * Handles a REST API request without deserializing the request body. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * @param \Symfony\Component\HttpFoundation\Request $request * The HTTP request object. * @param \Drupal\rest\RestResourceConfigInterface $_rest_resource_config * The REST resource config entity. * * @return \Symfony\Component\HttpFoundation\Response|\Drupal\rest\ResourceResponseInterface * The REST resource response. */

  protected function getRoutesForResourceConfig(RestResourceConfigInterface $rest_resource_config) {
    $plugin = $rest_resource_config->getResourcePlugin();
    $collection = new RouteCollection();

    foreach ($plugin->routes() as $name => $route) {
      /** @var \Symfony\Component\Routing\Route $route */
      // @todo: Are multiple methods possible here?       $methods = $route->getMethods();
      // Only expose routes that have an explicit method and allow >=1 format       // for that method.       if (($methods && ($method = $methods[0]) && $rest_resource_config->getFormats($method))) {
        $route->setRequirement('_csrf_request_header_token', 'TRUE');

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