From 0fb2a86b67f0ec295344cc5f77e47172a2ed2e1a Mon Sep 17 00:00:00 2001 From: "arthur@troopers.email" Date: Wed, 29 Nov 2017 17:51:21 +0100 Subject: [PATCH] handle new X-Inject-Alertify header in AlertifyListener --- EventListener/AlertifyListener.php | 21 +++++++++++++------ Exception/IncompatibleStatusCodeException.php | 21 +++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 Exception/IncompatibleStatusCodeException.php diff --git a/EventListener/AlertifyListener.php b/EventListener/AlertifyListener.php index 4133444..b24ecdd 100644 --- a/EventListener/AlertifyListener.php +++ b/EventListener/AlertifyListener.php @@ -57,22 +57,31 @@ public function onKernelResponse(FilterResponseEvent $event) /** * Injects the alertify view into the given Response just before tag. + * + * @param Response $response + * @param Request $request + * + * @throws IncompatibleStatusCodeException */ protected function injectAlertify(Response $response, Request $request) { - if ($response instanceof RedirectResponse) { - return; - } - $content = $response->getContent(); $endBodyPos = strripos($content, ''); $hasBody = false !== $endBodyPos; $hasMetaRefresh = false !== strripos($content, 'http-equiv="refresh"'); + $forceInject = $response->headers->get('X-Inject-Alertify', false); $isRedirectResponse = $response instanceof RedirectResponse; - if ($hasBody && !$hasMetaRefresh && !$isRedirectResponse) { + if ($hasBody && !$hasMetaRefresh && !$isRedirectResponse || $forceInject) { + if ($response->getStatusCode() === 204) { + throw new IncompatibleStatusCodeException(); + } $alertify = $this->alertifySessionHandler->handle($this->session, $this->twig); - $content = substr($content, 0, $endBodyPos).$alertify.substr($content, $endBodyPos); + if ($endBodyPos) { + $content = substr($content, 0, $endBodyPos).$alertify.substr($content, $endBodyPos); + } else { + $content .= $alertify; + } $response->setContent($content); } } diff --git a/Exception/IncompatibleStatusCodeException.php b/Exception/IncompatibleStatusCodeException.php new file mode 100644 index 0000000..22cd3ec --- /dev/null +++ b/Exception/IncompatibleStatusCodeException.php @@ -0,0 +1,21 @@ +