Parser.php in Subrequests 8
File
src/Blueprint/Parser.php
View source
<?php
namespace Drupal\subrequests\Blueprint;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Cache\CacheableResponseInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
class Parser {
protected $serializer;
protected $type;
public function __construct(SerializerInterface $serializer) {
$this->serializer = $serializer;
}
public function parseRequest(Request $request) {
$data = '';
if ($request
->getMethod() === Request::METHOD_POST) {
$data = $request
->getContent();
}
else {
if ($request
->getMethod() === Request::METHOD_GET) {
$data = $request->query
->get('query', '');
}
}
$tree = $this->serializer
->deserialize($data, RequestTree::class, $request
->getRequestFormat(), [
'master_request' => $request,
]);
$request->attributes
->set(RequestTree::SUBREQUEST_TREE, $tree);
$this->type = $request
->getMimeType($request
->getRequestFormat());
}
public function combineResponses(array $responses) {
$delimiter = md5(microtime());
$content_type = sprintf('multipart/related; boundary="%s", type=%s', $delimiter, $this->type);
$headers = [
'Content-Type' => $content_type,
];
$context = [
'delimiter' => $delimiter,
];
$content = $this->serializer
->normalize($responses, 'multipart-related', $context);
$response = CacheableResponse::create($content, 207, $headers);
$cacheable_responses = array_filter($responses, function ($response) {
return $response instanceof CacheableResponseInterface;
});
array_walk($cacheable_responses, function (CacheableResponseInterface $partial_response) use ($response) {
$response
->addCacheableDependency($partial_response
->getCacheableMetadata());
});
return $response;
}
public static function isValidSubrequest(array $data) {
return (bool) $data;
}
}
Classes
Name |
Description |
Parser |
TODO: Change this comment. We'll use the serializer instead.
Base class for blueprint parsers. There may be slightly different blueprint
formats depending on the encoding. For instance, JSON encoded blueprints will
reference other properties in… |