Skip to content

Commit

Permalink
Merge pull request #72 from adpdsr/feature/New-Header-Allowed-Inject-…
Browse files Browse the repository at this point in the history
…Alertify

handle new X-Inject-Alertify header in AlertifyListener
  • Loading branch information
lenybernard authored Nov 30, 2017
2 parents 6583174 + 2b4daea commit 24fe2ff
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
21 changes: 15 additions & 6 deletions EventListener/AlertifyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,31 @@ public function onKernelResponse(FilterResponseEvent $event)

/**
* Injects the alertify view into the given Response just before </body> 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, '</body>');
$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);
}
}
Expand Down
21 changes: 21 additions & 0 deletions Exception/IncompatibleStatusCodeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Troopers\AlertifyBundle\Exception;

/**
* Class IncompatibleStatusCodeException.
*/
class IncompatibleStatusCodeException extends \Exception
{
/**
* IncompatibleStatusCodeException constructor.
*
* @param string $message
* @param int $code
* @param null $previous
*/
public function __construct($message = "Status code 204 can't have content, please choose another one.", $code = 544, $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ With Alertify, give the final user a unique and harmnized view f your notificati
- [Installation](https://github.com/Troopers/TroopersAlertifyBundle/blob/master/Resources/doc/installation.md)
- [Configuration](https://github.com/Troopers/TroopersAlertifyBundle/blob/master/Resources/doc/configuration.md)
- [Getting started](https://github.com/Troopers/TroopersAlertifyBundle/blob/master/Resources/doc/getting_started.md)
- [Advanced (modal, confirmation modal)](https://github.com/Troopers/TroopersAlertifyBundle/blob/master/Resources/doc/advanced.md)
- [Advanced (modal, confirmation modal, custom header)](https://github.com/Troopers/TroopersAlertifyBundle/blob/master/Resources/doc/advanced.md)
- [Pro tips](https://github.com/Troopers/TroopersAlertifyBundle/blob/master/Resources/doc/pro_tips.md)
12 changes: 12 additions & 0 deletions Resources/doc/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ Instead of that, the js we'll be ran.
```html
<a href="/your_url" class="btn btn-mini btn-danger" data-toggle="confirm" data-title="Are you sure ?" data-body="Kittens will suffer ! Do you confirm ?" data-cancel-button-class="cancel" data-confirm-button-class="danger">Burn some cats</a>
```

## Custom header

If you want to force the trigger of an alert, after an ajax call for example, you need to send through the response a custom header : `X-Inject-Alertify`. When its value is set to `true`, then the alert append at the end of the response content, even if it is empty. Note that the `204` status code (No content) is incompatible with this header, as the 204 response MUST NOT include a message-body.

## Example :

```php
return new Response(null, 200, [
'X-Inject-Alertify' => true
]);
```

0 comments on commit 24fe2ff

Please sign in to comment.