Author: Kévin Dunglas (dunglas@gmail.com)
Example #1
0
 /**
  * Validates data returned by the controller if applicable.
  *
  * @param GetResponseForControllerResultEvent $event
  *
  * @throws ValidationException
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     try {
         $attributes = RequestAttributesExtractor::extractAttributes($request);
     } catch (RuntimeException $e) {
         return;
     }
     if ($request->isMethodSafe() || $request->isMethod(Request::METHOD_DELETE)) {
         return;
     }
     $data = $event->getControllerResult();
     $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
     if (isset($attributes['collection_operation_name'])) {
         $validationGroups = $resourceMetadata->getCollectionOperationAttribute($attributes['collection_operation_name'], 'validation_groups');
     } else {
         $validationGroups = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], 'validation_groups');
     }
     if (!$validationGroups) {
         // Fallback to the resource
         $validationGroups = $resourceMetadata->getAttributes()['validation_groups'] ?? null;
     }
     if (is_callable($validationGroups)) {
         $validationGroups = call_user_func_array($validationGroups, [$data]);
     }
     $violations = $this->validator->validate($data, null, $validationGroups);
     if (0 !== count($violations)) {
         throw new ValidationException($violations);
     }
 }
Example #2
0
 /**
  * Calls the data provider and sets the data attribute.
  *
  * @param GetResponseEvent $event
  *
  * @throws NotFoundHttpException
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     try {
         $attributes = RequestAttributesExtractor::extractAttributes($request);
     } catch (RuntimeException $e) {
         return;
     }
     if (isset($attributes['collection_operation_name'])) {
         $data = $this->getCollectionData($request, $attributes);
     } else {
         $data = $this->getItemData($request, $attributes);
     }
     $request->attributes->set('data', $data);
 }
Example #3
0
 /**
  * Serializes the data to the requested format.
  *
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $controllerResult = $event->getControllerResult();
     $request = $event->getRequest();
     if ($controllerResult instanceof Response) {
         return;
     }
     try {
         $attributes = RequestAttributesExtractor::extractAttributes($request);
     } catch (RuntimeException $e) {
         $this->serializeRawData($event, $request, $controllerResult);
         return;
     }
     $context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
     $request->attributes->set('_api_respond', true);
     $event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context));
 }
Example #4
0
 /**
  * Deserializes the data sent in the requested format.
  *
  * @param GetResponseEvent $event
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if ($request->isMethodSafe() || $request->isMethod(Request::METHOD_DELETE)) {
         return;
     }
     try {
         $attributes = RequestAttributesExtractor::extractAttributes($request);
     } catch (RuntimeException $e) {
         return;
     }
     $format = $this->getFormat($request);
     $context = $this->serializerContextBuilder->createFromRequest($request, false, $attributes);
     $data = $request->attributes->get('data');
     if (null !== $data) {
         $context['object_to_populate'] = $data;
     }
     $request->attributes->set('data', $this->serializer->deserialize($request->getContent(), $attributes['resource_class'], $format, $context));
 }
 /**
  * {@inheritdoc}
  */
 public function createFromRequest(Request $request, bool $normalization, array $attributes = null) : array
 {
     if (null === $attributes) {
         $attributes = RequestAttributesExtractor::extractAttributes($request);
     }
     $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
     $key = $normalization ? 'normalization_context' : 'denormalization_context';
     if (isset($attributes['collection_operation_name'])) {
         $context = $resourceMetadata->getCollectionOperationAttribute($attributes['collection_operation_name'], $key, [], true);
         $context['collection_operation_name'] = $attributes['collection_operation_name'];
     } else {
         $context = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], $key, [], true);
         $context['item_operation_name'] = $attributes['item_operation_name'];
     }
     if (!$normalization && !isset($context['api_allow_update'])) {
         $context['api_allow_update'] = Request::METHOD_PUT === $request->getMethod();
     }
     $context['resource_class'] = $attributes['resource_class'];
     $context['request_uri'] = $request->getRequestUri();
     return $context;
 }
Example #6
0
 /**
  * Persists, updates or delete data return by the controller if applicable.
  *
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     try {
         RequestAttributesExtractor::extractAttributes($request);
     } catch (RuntimeException $e) {
         return;
     }
     $user = $event->getControllerResult();
     if (!$user instanceof UserInterface || $request->isMethodSafe(false)) {
         return;
     }
     switch ($request->getMethod()) {
         case Request::METHOD_DELETE:
             $this->userManager->deleteUser($user);
             $event->setControllerResult(null);
             break;
         default:
             $this->userManager->updateUser($user);
             break;
     }
 }
 /**
  * @expectedException \ApiPlatform\Core\Exception\RuntimeException
  * @expectedExceptionMessage One of the request attribute "_api_collection_operation_name" or "_api_item_operation_name" must be defined.
  */
 public function testOperationNotSet()
 {
     RequestAttributesExtractor::extractAttributes(new Request([], [], ['_api_resource_class' => 'Foo']));
 }
Example #8
0
 public function __invoke(Request $request, $id)
 {
     $attributes = RequestAttributesExtractor::extractAttributes($request);
     return $this->dataProvider->getItem($attributes['resource_class'], $id);
 }