-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Listen kernel.response event to inject alertify into the response
- Loading branch information
1 parent
c8a1586
commit 5a8bc86
Showing
2 changed files
with
98 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Troopers\AlertifyBundle\EventListener; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Troopers\AlertifyBundle\Handler\AlertifySessionHandler; | ||
|
||
/** | ||
* AlertifyListener append the alertify views to the response. | ||
* | ||
* @author Leny Bernard <[email protected]> | ||
*/ | ||
class AlertifyListener implements EventSubscriberInterface | ||
{ | ||
protected $alertifySessionHandler; | ||
/** | ||
* @var Session | ||
*/ | ||
private $session; | ||
|
||
/** | ||
* AlertifyListener constructor. | ||
* | ||
* @param Session $session | ||
* @param AlertifySessionHandler $alertifySessionHandler | ||
*/ | ||
public function __construct(Session $session, AlertifySessionHandler $alertifySessionHandler) | ||
{ | ||
$this->alertifySessionHandler = $alertifySessionHandler; | ||
$this->session = $session; | ||
} | ||
|
||
public function onKernelResponse(FilterResponseEvent $event) | ||
{ | ||
$response = $event->getResponse(); | ||
$request = $event->getRequest(); | ||
|
||
$this->injectAlertify($response, $request); | ||
} | ||
|
||
/** | ||
* Injects the alertify view into the given Response just before </body> tag. | ||
*/ | ||
protected function injectAlertify(Response $response, Request $request) | ||
{ | ||
$content = $response->getContent(); | ||
$pos = strripos($content, '</body>'); | ||
|
||
if (false !== $pos) { | ||
$alertify = $this->alertifySessionHandler->handle($this->session); | ||
$content = substr($content, 0, $pos).$alertify.substr($content, $pos); | ||
$response->setContent($content); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
KernelEvents::RESPONSE => array('onKernelResponse'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters