Skip to content

Commit 5a8bc86

Browse files
Leny BERNARDlenybernard
authored andcommitted
🎨 Listen kernel.response event to inject alertify into the response
1 parent c8a1586 commit 5a8bc86

File tree

2 files changed

+98
-35
lines changed

2 files changed

+98
-35
lines changed

EventListener/AlertifyListener.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Troopers\AlertifyBundle\EventListener;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
17+
use Symfony\Component\HttpFoundation\Session\Session;
18+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
19+
use Symfony\Component\HttpKernel\KernelEvents;
20+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22+
use Troopers\AlertifyBundle\Handler\AlertifySessionHandler;
23+
24+
/**
25+
* AlertifyListener append the alertify views to the response.
26+
*
27+
* @author Leny Bernard <leny@troopers.email>
28+
*/
29+
class AlertifyListener implements EventSubscriberInterface
30+
{
31+
protected $alertifySessionHandler;
32+
/**
33+
* @var Session
34+
*/
35+
private $session;
36+
37+
/**
38+
* AlertifyListener constructor.
39+
*
40+
* @param Session $session
41+
* @param AlertifySessionHandler $alertifySessionHandler
42+
*/
43+
public function __construct(Session $session, AlertifySessionHandler $alertifySessionHandler)
44+
{
45+
$this->alertifySessionHandler = $alertifySessionHandler;
46+
$this->session = $session;
47+
}
48+
49+
public function onKernelResponse(FilterResponseEvent $event)
50+
{
51+
$response = $event->getResponse();
52+
$request = $event->getRequest();
53+
54+
$this->injectAlertify($response, $request);
55+
}
56+
57+
/**
58+
* Injects the alertify view into the given Response just before </body> tag.
59+
*/
60+
protected function injectAlertify(Response $response, Request $request)
61+
{
62+
$content = $response->getContent();
63+
$pos = strripos($content, '</body>');
64+
65+
if (false !== $pos) {
66+
$alertify = $this->alertifySessionHandler->handle($this->session);
67+
$content = substr($content, 0, $pos).$alertify.substr($content, $pos);
68+
$response->setContent($content);
69+
}
70+
}
71+
72+
public static function getSubscribedEvents()
73+
{
74+
return [
75+
KernelEvents::RESPONSE => array('onKernelResponse'),
76+
];
77+
}
78+
}
Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,55 @@
11
<?php
22

3-
namespace Troopers\AlertifyBundle\Twig\Extension;
3+
namespace Troopers\AlertifyBundle\Handler;
44

55
use Symfony\Component\HttpFoundation\Session\Session;
66

77
/**
8-
* AlertifyExtension.
8+
* AlertifySessionHandler.
99
*/
10-
class AlertifyExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
10+
class AlertifySessionHandler
1111
{
12-
protected $environment;
13-
protected $defaultParameters;
14-
1512
/**
16-
* {@inheritdoc}
13+
* @var \Twig_Environment
1714
*/
18-
public function __construct($defaultParameters)
19-
{
20-
$this->defaultParameters = $defaultParameters;
21-
}
15+
protected $twig;
2216

2317
/**
24-
* {@inheritdoc}
18+
* @var array
2519
*/
26-
public function initRuntime(\Twig_Environment $environment)
27-
{
28-
$this->environment = $environment;
29-
}
20+
private $defaultParameters;
3021

3122
/**
32-
* {@inheritdoc}
33-
*/
34-
public function getName()
35-
{
36-
return 'alertify';
37-
}
38-
39-
/**
40-
* {@inheritdoc}
23+
* AlertifySessionHandler constructor.
24+
*
25+
* @param \Twig_Environment $twig
26+
* @param array $defaultParameters
4127
*/
42-
public function getFilters()
28+
public function __construct(\Twig_Environment $twig, array $defaultParameters)
4329
{
44-
return [
45-
new \Twig_SimpleFilter('alertify', [$this, 'alertifyFilter'], ['needs_environment' => true, 'is_safe' => ['html']]),
46-
];
30+
$this->twig = $twig;
31+
$this->defaultParameters = $defaultParameters;
4732
}
4833

4934
/**
50-
* Alertify filter.
35+
* Alertify .
5136
*
52-
* @param \Twig_Environment $environment
37+
* @param \Twig_Environment $this-twig
5338
* @param Session $session
5439
*
5540
* @return string
5641
*/
57-
public function alertifyFilter($environment, Session $session)
42+
public function handle($session)
5843
{
5944
$flashes = $session->getFlashBag()->all();
6045

6146
$renders = [];
6247
foreach ($flashes as $type => $flash) {
6348
if ($type == 'callback') {
6449
foreach ($flash as $key => $currentFlash) {
65-
$currentFlash['body'] .= $environment->render('TroopersAlertifyBundle::callback.html.twig', $currentFlash);
50+
$currentFlash['body'] .= $this->twig->render('TroopersAlertifyBundle::callback.html.twig', $currentFlash);
6651
$session->getFlashBag()->add($currentFlash['engine'], $currentFlash);
67-
$renders[$type.$key] = $this->alertifyFilter($environment, $session);
52+
$renders[$type.$key] = $this->handle($session);
6853
}
6954
} else {
7055
foreach ($flash as $key => $content) {
@@ -78,7 +63,7 @@ public function alertifyFilter($environment, Session $session)
7863
}
7964

8065
$parameters['type'] = $type;
81-
$renders[$type.$key] = $environment->render('TroopersAlertifyBundle::'.$parameters['engine'].'.html.twig', $parameters);
66+
$renders[$type.$key] = $this->twig->render('TroopersAlertifyBundle::'.$parameters['engine'].'.html.twig', $parameters);
8267
}
8368
}
8469
}

0 commit comments

Comments
 (0)