You are here

class EnforcedResponse in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Form/EnforcedResponse.php \Drupal\Core\Form\EnforcedResponse
  2. 10 core/lib/Drupal/Core/Form/EnforcedResponse.php \Drupal\Core\Form\EnforcedResponse

A wrapper containing a response which is to be enforced upon delivery.

The FormBuilder throws an EnforcedResponseException whenever a form desires to explicitly set a response object. Exception handlers capable of setting the response should extract the response object of such an exception using EnforcedResponse::createFromException(). Then wrap it into an EnforcedResponse object and replace the original response with the wrapped response.

Hierarchy

  • class \Drupal\Core\Form\EnforcedResponse extends \Symfony\Component\HttpFoundation\Response

Expanded class hierarchy of EnforcedResponse

See also

Drupal\Core\EventSubscriber\EnforcedFormResponseSubscriber::onKernelException()

Drupal\Core\EventSubscriber\DefaultExceptionSubscriber::createHtmlResponse()

Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber::createResponse()

1 file declares its use of EnforcedResponse
EnforcedFormResponseSubscriber.php in core/lib/Drupal/Core/EventSubscriber/EnforcedFormResponseSubscriber.php

File

core/lib/Drupal/Core/Form/EnforcedResponse.php, line 21

Namespace

Drupal\Core\Form
View source
class EnforcedResponse extends Response {

  /**
   * The wrapped response object.
   *
   * @var \Symfony\Component\HttpFoundation\Response
   */
  protected $response;

  /**
   * Constructs a new enforced response from the given exception.
   *
   * Note that it is necessary to traverse the exception chain when searching
   * for an enforced response. Otherwise it would be impossible to find an
   * exception thrown from within a twig template.
   *
   * @param \Exception $e
   *   The exception where the enforced response is to be extracted from.
   *
   * @return static|null
   *   The enforced response or NULL if the exception chain does not contain a
   *   \Drupal\Core\Form\EnforcedResponseException exception.
   */
  public static function createFromException(\Exception $e) {
    while ($e) {
      if ($e instanceof EnforcedResponseException) {
        return new static($e
          ->getResponse());
      }
      $e = $e
        ->getPrevious();
    }
  }

  /**
   * Constructs an enforced response.
   *
   * Use EnforcedResponse::createFromException() instead.
   *
   * @param \Symfony\Component\HttpFoundation\Response $response
   *   The response to wrap.
   */
  public function __construct(Response $response) {
    parent::__construct('', 500);
    $this->response = $response;
  }

  /**
   * Returns the wrapped response.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The wrapped response.
   */
  public function getResponse() {
    return $this->response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EnforcedResponse::$response protected property The wrapped response object.
EnforcedResponse::createFromException public static function Constructs a new enforced response from the given exception.
EnforcedResponse::getResponse public function Returns the wrapped response.
EnforcedResponse::__construct public function Constructs an enforced response.