Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 12 |
EnforcedResponse | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 12 |
createFromException | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 7 |
|||
__construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
getResponse | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
<?php | |
/** | |
* @file | |
* Contains \Drupal\Core\Form\EnforcedResponse. | |
*/ | |
namespace Drupal\Core\Form; | |
use Symfony\Component\HttpFoundation\Response; | |
/** | |
* 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. | |
* | |
* @see Drupal\Core\EventSubscriber\EnforcedFormResponseSubscriber::onKernelException() | |
* @see Drupal\Core\EventSubscriber\DefaultExceptionSubscriber::createHtmlResponse() | |
* @see Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber::createResponse() | |
*/ | |
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 \Drupal\Core\Form\EnforcedResponse|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; | |
} | |
} |