Commit 4e8efa71 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch

Misc changes

parent 09ef18a4
......@@ -45,9 +45,10 @@ class Parser {
$tree = $this->serializer->deserialize(
$request->getContent(),
RequestTree::class,
$request->getRequestFormat()
$request->getRequestFormat(),
['master_request' => $request]
);
$request->attributes->add(RequestTree::SUBREQUEST_TREE, $tree);
$request->attributes->set(RequestTree::SUBREQUEST_TREE, $tree);
// It assumed that all subrequests use the same Mime-Type.
$this->type = $request->getMimeType($request->getRequestFormat());
}
......@@ -71,7 +72,7 @@ class Parser {
$headers = ['Content-Type' => $content_type];
$context = ['delimiter' => $delimiter];
$content = $this->serializer->serialize($responses, 'multipart-related', $context);
$content = $this->serializer->normalize($responses, 'multipart-related', $context);
return Response::create($content, 207, $headers);
}
......
......@@ -62,10 +62,9 @@ class FrontController extends ControllerBase {
}, []);
// Handle the requests for the trees at this level and gather the
// responses.
$level_responses = array_map(array(
$this->httpKernel,
'handle',
), $requests);
$level_responses = array_map(function (Request $request) {
return $this->httpKernel->handle($request, HttpKernelInterface::SUB_REQUEST);
}, $requests);
$responses = array_merge(
$responses,
$level_responses
......
......@@ -19,6 +19,12 @@ class SubresponseSubscriber implements EventSubscriberInterface {
public function onResponse(FilterResponseEvent $event) {
$request = $event->getRequest();
$request->attributes->set(RequestTree::SUBREQUEST_DONE, TRUE);
// Carry over the Content ID header from the request to the response.
$header_name = 'Content-ID';
$event->getResponse()->headers->set(
$header_name,
$request->headers->get($header_name)
);
}
/**
......@@ -26,7 +32,7 @@ class SubresponseSubscriber implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
// Run shortly before \Drupal\Core\EventSubscriber\FinishResponseSubscriber.
$events[KernelEvents::RESPONSE][] = ['onResponse', 5];
$events[KernelEvents::RESPONSE][] = ['onResponse'];
return $events;
}
......
......@@ -32,8 +32,8 @@ class JsonBlueprintDenormalizer implements DenormalizerInterface, SerializerAwar
*/
public function denormalize($data, $class, $format = NULL, array $context = array()) {
// The top level is an array of normalized requests.
$requests = array_map(function ($item) use ($format) {
return $this->serializer->denormalize($item, Request::class, $format);
$requests = array_map(function ($item) use ($format, $context) {
return $this->serializer->denormalize($item, Request::class, $format, $context);
}, $data);
return new RequestTree($requests);
}
......
......@@ -25,7 +25,7 @@ class JsonSubrequestDenormalizer implements DenormalizerInterface {
throw new \RuntimeException('The provided blueprint contains an invalid subrequest.');
}
$data['path'] = parse_url($data['path'], PHP_URL_PATH);
if (!is_array($data['query'])) {
if (isset($data['query']) && !is_array($data['query'])) {
$query = array();
parse_str($data['query'], $query);
$data['query'] = $query;
......@@ -43,10 +43,10 @@ class JsonSubrequestDenormalizer implements DenormalizerInterface {
$data['path'],
static::getMethodFromAction($data['action']),
empty($data['body']) ? $data['query'] : $data['body'],
$master_request->cookies,
$master_request->files,
$master_request->server,
NULL
$master_request->cookies ? (array) $master_request->cookies->getIterator() : [],
$master_request->files ? (array) $master_request->files->getIterator() : [],
$master_request->server ? (array) $master_request->server->getIterator() : [],
empty($data['body']) ? '' : $data['body']
);
// Maintain the same session as in the master request.
$request->setSession($master_request->getSession());
......@@ -57,7 +57,7 @@ class JsonSubrequestDenormalizer implements DenormalizerInterface {
$content_id = empty($data['requestId'])
? md5(serialize($data))
: $data['requestId'];
$request->headers->add(['Content-ID', ['<' . $content_id . '>']]);
$request->headers->set('Content-ID', '<' . $content_id . '>');
return $request;
}
......
......@@ -5,7 +5,6 @@ namespace Drupal\subrequests\Normalizer;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\scalar;
class MultiresponseNormalizer implements NormalizerInterface {
......@@ -17,8 +16,9 @@ class MultiresponseNormalizer implements NormalizerInterface {
$separator = sprintf("\r\n--%s\r\n", $delimiter);
// Join the content responses with the separator.
$content_items = array_map(function (Response $part_response) {
$part_response->headers->set('Status', $part_response->getStatusCode());
return sprintf(
"%s\r\n\r\n%s",
"%s\r \n%s",
$part_response->headers,
$part_response->getContent()
);
......@@ -30,7 +30,7 @@ class MultiresponseNormalizer implements NormalizerInterface {
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = NULL) {
if ($format !== 'multipart-response') {
if ($format !== 'multipart-related') {
return FALSE;
}
if (!is_array($data)) {
......
......@@ -11,6 +11,10 @@ services:
class: Drupal\subrequests\Normalizer\JsonBlueprintDenormalizer
tags:
- { name: normalizer, priority: 0 }
subrequests.denormalizer.subrequest.json:
class: Drupal\subrequests\Normalizer\JsonSubrequestDenormalizer
tags:
- { name: normalizer, priority: 0 }
subrequests.normalizer.multiresponse:
class: Drupal\subrequests\Normalizer\MultiresponseNormalizer
tags:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment