class JsonSubrequestDenormalizer in Subrequests 8.2
Same name and namespace in other branches
- 8 src/Normalizer/JsonSubrequestDenormalizer.php \Drupal\subrequests\Normalizer\JsonSubrequestDenormalizer
- 3.x src/Normalizer/JsonSubrequestDenormalizer.php \Drupal\subrequests\Normalizer\JsonSubrequestDenormalizer
Creates a request object for each Subrequest.
Hierarchy
- class \Drupal\subrequests\Normalizer\JsonSubrequestDenormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
Expanded class hierarchy of JsonSubrequestDenormalizer
2 files declare their use of JsonSubrequestDenormalizer
- JsonSubrequestDenormalizerTest.php in tests/
src/ Unit/ Normalizer/ JsonSubrequestDenormalizerTest.php - SubrequestsManagerTest.php in tests/
src/ Unit/ SubrequestsManagerTest.php
1 string reference to 'JsonSubrequestDenormalizer'
1 service uses JsonSubrequestDenormalizer
File
- src/
Normalizer/ JsonSubrequestDenormalizer.php, line 13
Namespace
Drupal\subrequests\NormalizerView source
class JsonSubrequestDenormalizer implements DenormalizerInterface {
/**
* Denormalizes data back into an object of the given class.
*
* @param mixed $data
* data to restore.
* @param string $class
* the expected class to instantiate.
* @param string $format
* format the given data was extracted from.
* @param array $context
* options available to the denormalizer.
*
* @return object
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
/** @var \Drupal\subrequests\Subrequest $data */
$path = parse_url($data->uri, PHP_URL_PATH);
$query = parse_url($data->uri, PHP_URL_QUERY) ?: [];
if (isset($query) && !is_array($query)) {
$_query = [];
parse_str($query, $_query);
$query = $_query;
}
/** @var \Symfony\Component\HttpFoundation\Request $master_request */
$master_request = $context['master_request'];
$request = Request::create($path, static::getMethodFromAction($data->action), empty($data->body) ? $query : $data->body, $master_request->cookies ? $master_request->cookies
->all() : [], $master_request->files ? $master_request->files
->all() : [], [], empty($data->body) ? '' : Json::encode($data->body));
// Maintain the same session as in the master request.
$session = $master_request
->getSession();
$request
->setSession($session);
// Replace the headers by the ones in the subrequest.
foreach ($data->headers as $name => $value) {
$request->headers
->set($name, $value);
}
$this::fixBasicAuth($request);
// Add the content ID to the sub-request.
$content_id = empty($data->requestId) ? md5(serialize($data)) : $data->requestId;
$request->headers
->set('Content-ID', '<' . $content_id . '>');
return $request;
}
/**
* Checks whether the given class is supported for denormalization by this
* normalizer.
*
* @param mixed $data
* Data to denormalize from.
* @param string $type
* The class to which the data should be denormalized.
* @param string $format
* The format being deserialized from.
*
* @return bool
*/
public function supportsDenormalization($data, $type, $format = NULL) {
return $type === Request::class && $data instanceof Subrequest;
}
/**
* Gets the HTTP method from the list of allowed actions.
*
* @param string $action
* The action name.
*
* @return string
* The HTTP method.
*/
public static function getMethodFromAction($action) {
switch ($action) {
case 'create':
return Request::METHOD_POST;
case 'update':
return Request::METHOD_PATCH;
case 'replace':
return Request::METHOD_PUT;
case 'delete':
return Request::METHOD_DELETE;
case 'exists':
return Request::METHOD_HEAD;
case 'discover':
return Request::METHOD_OPTIONS;
default:
return Request::METHOD_GET;
}
}
/**
* Adds the decoded username and password headers for Basic Auth.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request to fix.
*/
protected static function fixBasicAuth(Request $request) {
// The server will not set the PHP_AUTH_USER and PHP_AUTH_PW for the
// subrequests if needed.
if ($request->headers
->has('Authorization')) {
$header = $request->headers
->get('Authorization');
if (strpos($header, 'Basic ') === 0) {
list($user, $pass) = explode(':', base64_decode(substr($header, 6)));
$request->headers
->set('PHP_AUTH_USER', $user);
$request->headers
->set('PHP_AUTH_PW', $pass);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
JsonSubrequestDenormalizer:: |
public | function | Denormalizes data back into an object of the given class. | |
JsonSubrequestDenormalizer:: |
protected static | function | Adds the decoded username and password headers for Basic Auth. | |
JsonSubrequestDenormalizer:: |
public static | function | Gets the HTTP method from the list of allowed actions. | |
JsonSubrequestDenormalizer:: |
public | function | Checks whether the given class is supported for denormalization by this normalizer. |